import { type ComponentPropsWithoutRef } from 'react';
import { Select as RSelect } from 'radix-ui';
import { Check, CaretDown } from '@phosphor-icons/react';

type SelectProps = ComponentPropsWithoutRef<typeof RSelect.Root> & {
  placeholder?: string;
  items: Array<{ value: string; label: string }>;
  'aria-label'?: string;
};

export const Select = ({ placeholder, items, ...rest }: SelectProps) => (
  <RSelect.Root {...rest}>
    <RSelect.Trigger className="mta-select" aria-label={rest['aria-label']}>
      <RSelect.Value placeholder={placeholder} />
      <RSelect.Icon className="mta-select__icon">
        <CaretDown size={12} weight="bold" />
      </RSelect.Icon>
    </RSelect.Trigger>
    <RSelect.Portal>
      <RSelect.Content
        className="mta-select__content"
        position="popper"
        sideOffset={6}
      >
        <RSelect.Viewport>
          {items.map((it) => (
            <RSelect.Item
              key={it.value}
              value={it.value}
              className="mta-select__item"
            >
              <RSelect.ItemText>{it.label}</RSelect.ItemText>
              <RSelect.ItemIndicator className="mta-select__item-indicator">
                <Check size={14} weight="bold" />
              </RSelect.ItemIndicator>
            </RSelect.Item>
          ))}
        </RSelect.Viewport>
      </RSelect.Content>
    </RSelect.Portal>
  </RSelect.Root>
);
