import * as Ariakit from '@ariakit/react';
import { IconX } from '@websolutespa/bom-mixer-icons';
import styled from 'styled-components';
import { Button } from '../button/button';

import { getClassNames } from '@websolutespa/bom-core';
import { useLabel } from '@websolutespa/bom-mixer-hooks';
import { ReactElement, ReactNode, useContext } from 'react';
import { getVariantKey } from '../../variants';
import { getCssResponsive } from '../utils';
import { DrawerContext } from './drawer.context';
import { IDrawerVariants, drawerVariants } from './drawer.variants';

const DrawerHeaderStyle = styled.div<DrawerHeaderProps>`
  ${props => getVariantKey('header', props.variant, props.variants)}
  ${props => getCssResponsive(props)}
`;

export type DrawerHeaderProps = {
  className?: string;
  variant?: keyof typeof drawerVariants | (string & {});
  variants?: IDrawerVariants;
  children?: ReactNode;
  close?: ReactElement;
};

export const DrawerHeader = ({ children, close, className, variant, variants, ...props }: DrawerHeaderProps) => {
  const label = useLabel();
  const classNames = getClassNames('drawer__header', className);
  const context = useContext(DrawerContext);
  if (!context) {
    return;
  }
  return (
    <DrawerHeaderStyle
      className={classNames}
      variant={variant || context.variant}
      variants={variants || context.variants}
      {...props}
    >{children}
      <Ariakit.DialogDismiss render={close || (
        <Button variant="circle" aria-label={label('close')}><IconX /></Button>
      )} />
    </DrawerHeaderStyle>
  );
};

DrawerHeader.displayName = 'DrawerHeader';
