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

const ComboboxCancelStyle = styled(Ariakit.ComboboxCancel) <ComboboxCancelProps>`
  ${props => getVariantKey('cancel', props.variant, props.variants)}
`;

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

export const ComboboxCancel = forwardRef<HTMLButtonElement, ComboboxCancelProps>(
  function ComboboxCancel({
    variant,
    variants,
    className,
    children,
    ...props
  }: ComboboxCancelProps, ref) {
    const context = useContext(ComboboxContext);
    if (!context) {
      return;
    }
    const classNames = getClassNames('combobox__cancel', className);
    return (
      <ComboboxCancelStyle
        ref={ref}
        className={classNames}
        variant={variant || context.variant}
        variants={variants || context.variants}
        {...props}
      />
    );
  });
