import * as Ariakit from '@ariakit/react';
import { getClassNames } from '@websolutespa/bom-core';
import { forwardRef, ReactNode, useContext } from 'react';
import styled from 'styled-components';
import { getVariantKey } from '../../variants';
import { UIStyledComponentProps } from '../types';
import { getCssResponsive } from '../utils';
import { DrawerContext } from './drawer.context';
import { drawerVariants, IDrawerVariants } from './drawer.variants';

const DrawerTitleStyle = styled(Ariakit.DialogHeading) <DrawerTitleProps>`
  ${props => getVariantKey('title', props.variant, props.variants)}
  ${props => getCssResponsive(props)}
`;

export type DrawerTitleProps = Ariakit.DialogHeadingProps & UIStyledComponentProps<{
  className?: string;
  variant?: keyof typeof drawerVariants | (string & {});
  variants?: IDrawerVariants;
  children?: ReactNode;
}>;

export const DrawerTitle = forwardRef<HTMLHeadingElement, DrawerTitleProps>(
  function DrawerTitle({
    children,
    className,
    variant,
    variants,
    ...props }: DrawerTitleProps, ref) {
    const classNames = getClassNames('drawer__title', className);
    const context = useContext(DrawerContext);
    if (!context) {
      return;
    }
    return (
      <DrawerTitleStyle
        ref={ref}
        className={classNames}
        variant={variant || context.variant}
        variants={variants || context.variants}
        {...props}
      >{children}</DrawerTitleStyle>
    );
  });
