'use client';
import { FunctionComponent } from 'react';
import Box from '@mui/material/Box';
import Chip from '@mui/material/Chip';

export interface ConditionOption {
  /** Stable code stored on the patient (e.g. `elderly`). */
  code: string;
  /** Translated display label. */
  label: string;
}

export interface ConditionChipsProps {
  options: ConditionOption[];
  /** Selected codes. */
  value: string[];
  onChange: (value: string[]) => void;
  disabled?: boolean;
}

/**
 * Multi-select condition chips (A4). Toggles a stable code in/out of the selected set;
 * selection is optional. Labels are translated by the caller.
 * @component ConditionChips
 */
const ConditionChips: FunctionComponent<ConditionChipsProps> = ({ options, value, onChange, disabled = false }) => {
  const toggle = (code: string) => {
    onChange(value.includes(code) ? value.filter((item) => item !== code) : [...value, code]);
  };

  return (
    <Box sx={{ display: 'flex', flexWrap: 'wrap', gap: 1 }}>
      {options.map((option) => {
        const selected = value.includes(option.code);
        return (
          <Chip
            key={option.code}
            label={option.label}
            data-code={option.code}
            aria-pressed={selected}
            clickable
            disabled={disabled}
            color={selected ? 'primary' : 'default'}
            variant={selected ? 'filled' : 'outlined'}
            onClick={() => toggle(option.code)}
          />
        );
      })}
    </Box>
  );
};

export default ConditionChips;
