import * as Ariakit from '@ariakit/react';
import { getClassNames } from '@websolutespa/bom-core';
import { forwardRef, useContext } from 'react';
import styled from 'styled-components';
import { getVariantKey } from '../../variants';
import { SelectContext } from './select.context';
import { ISelectVariants, selectVariants } from './select.variants';

const SelectOptionStyle = styled(Ariakit.SelectItem) <SelectOptionProps>`
  ${props => getVariantKey('option', props.variant, props.variants)}
`;

export type SelectOptionProps = Ariakit.SelectItemProps & {
  variant?: keyof typeof selectVariants | (string & {});
  variants?: ISelectVariants;
};

export const SelectOption = forwardRef<HTMLDivElement, SelectOptionProps>(
  function SelectOption({
    variant,
    variants,
    className,
    ...props
  }: SelectOptionProps, ref) {
    const context = useContext(SelectContext);
    if (!context) {
      return;
    }
    const classNames = getClassNames('select__option', className);
    return (
      <SelectOptionStyle
        ref={ref}
        className={classNames}
        variant={context.variant}
        variants={context.variants}
        {...props}
      />
    );
  });
