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 { Role } from '../role/role';
import { UIStyledComponentProps } from '../types';
import { getCssResponsive } from '../utils';
import { ModalContext } from './modal.context';
import { IModalVariants, modalVariants } from './modal.variants';

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

export type ModalTitleProps = Ariakit.DialogHeadingProps & UIStyledComponentProps<{
  className?: string;
  variant?: keyof typeof modalVariants | (string & {});
  variants?: IModalVariants;
  children?: ReactNode;
  asChild?: boolean;
}>;

export const ModalTitle = forwardRef<HTMLHeadingElement, ModalTitleProps>(
  function ModalTitle({
    asChild,
    className,
    variant,
    variants,
    ...props
  }, ref) {
    const classNames = getClassNames('modal__title', className);
    const context = useContext(ModalContext);
    return (
      <ModalTitleStyle
        className={classNames}
        variant={variant || context.variant}
        variants={variants || context.variants}
        ref={ref}
        {...props}
        render={asChild ? <Role /> : undefined}
      />
    );
  }
);
