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 { ComboboxContext } from './combobox.context';
import { comboboxVariants, IComboboxVariants } from './combobox.variants';

const ComboboxOptionValueStyle = styled(Ariakit.ComboboxItemValue) <ComboboxOptionValueProps>`
  ${props => getVariantKey('optionValue', props.variant, props.variants)}
`;

export type ComboboxOptionValueProps = Ariakit.ComboboxItemValueProps & {
  variant?: keyof typeof comboboxVariants | (string & {});
  variants?: IComboboxVariants;
};

export const ComboboxOptionValue = forwardRef<HTMLDivElement, ComboboxOptionValueProps>(
  function ComboboxOptionValue({
    variant,
    variants,
    className,
    ...props
  }: ComboboxOptionValueProps, ref) {
    const context = useContext(ComboboxContext);
    if (!context) {
      return;
    }
    const classNames = getClassNames('combobox__optionValue', className);
    return (
      <ComboboxOptionValueStyle
        ref={ref}
        className={classNames}
        variant={variant || context.variant}
        variants={variants || context.variants}
        {...props}
      />
    );
  });
