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 ComboboxOptionCheckStyle = styled(Ariakit.ComboboxItemCheck) <ComboboxOptionCheckProps>`
  ${props => getVariantKey('optionCheck', props.variant, props.variants)}
`;

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

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