import PropTypes from 'prop-types'
import ViewButton from './ViewButton'
import { getModelByName } from '../../../database/ObjectModels'

const ColumnViewButton = ({
  type,
  disabled = false,
  visibleState = {},
  updateVisibleState = () => {},
  ...buttonProps
}) => {
  // Get the model by name
  const model = getModelByName(type)

  // Get the properties that correspond to the model's columns
  const columnProperties =
    model.columns
      ?.map((columnName) => {
        const property = model.properties.find(
          (prop) => prop.name === columnName
        )
        if (property) {
          return {
            key: property.name,
            label: property.label
          }
        }
      })
      .filter(Boolean) || []

  return (
    <ViewButton
      disabled={disabled}
      items={columnProperties}
      visibleState={visibleState}
      updateVisibleState={updateVisibleState}
      {...buttonProps}
    />
  )
}

ColumnViewButton.propTypes = {
  type: PropTypes.string.isRequired,
  disabled: PropTypes.bool,
  visibleState: PropTypes.object,
  updateVisibleState: PropTypes.func
}

export default ColumnViewButton
