import * as Ariakit from '@ariakit/react';
import { getClassNames } from '@websolutespa/bom-core';
import { forwardRef, useContext } from 'react';
import styled from 'styled-components';
import { UIStyledComponentPropsWithRef } from '../../components';
import { getVariantKey, useVariants } from '../../variants';
import { RadioContext } from './radio.context';
import { IRadioVariants, radioVariants } from './radio.variants';

export const StyledRadioGroupLabel = styled(Ariakit.GroupLabel) <RadioGroupLabelProps>`
  ${props => getVariantKey('groupLabel', props.variant, props.variants)}
`;

type Props = {
  className?: string;
  variant?: keyof typeof radioVariants | (string & {});
  variants?: IRadioVariants;
};

export type RadioGroupLabelProps = Ariakit.GroupLabelProps & UIStyledComponentPropsWithRef<'div', Props>;

export const RadioGroupLabel = forwardRef(
  function Radio({
    className,
    variant = 'default',
    variants,
    ...props
  }: RadioGroupLabelProps, ref: RadioGroupLabelProps['ref']) {
    const context = useContext(RadioContext);
    const classNames = getClassNames('radio__groupLabel', className);
    const globalVariants = useVariants('radio', radioVariants);
    variants = variants || globalVariants;
    return (
      <StyledRadioGroupLabel
        ref={ref}
        className={classNames}
        variant={variant || context?.variant}
        variants={variants || context?.variants}
        {...props}
      />
    );
  }
);

