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 SelectPopoverStyle = styled(Ariakit.SelectPopover) <SelectPopoverProps>`
  ${props => getVariantKey('popover', props.variant, props.variants)}
`;

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

export const SelectPopover = forwardRef<HTMLDivElement, SelectPopoverProps>(
  function SelectPopover({
    variant,
    variants,
    className,
    ...props
  }: SelectPopoverProps, ref) {
    const context = useContext(SelectContext);
    if (!context) {
      return;
    }
    const classNames = getClassNames('select__popover', className);
    return (
      <SelectPopoverStyle
        ref={ref}
        className={classNames}
        variant={context.variant}
        variants={context.variants}
        {...props}
      />
    );
  });
