import { getClassNames } from '@websolutespa/bom-core';
import { 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 { IDrawerVariants, drawerVariants } from './drawer.variants';

const DrawerContentStyle = styled.div<DrawerContentProps>`
  ${props => getVariantKey('content', props.variant, props.variants)}
  ${props => getCssResponsive(props)}
`;

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

export const DrawerContent = ({ children, className, variant, variants, ...props }: DrawerContentProps) => {
  const classNames = getClassNames('drawer__content', className);
  const context = useContext(DrawerContext);
  if (!context) {
    return;
  }
  return (
    <DrawerContentStyle
      className={classNames}
      variant={variant || context.variant}
      variants={variants || context.variants}
      {...props}
    >{children}</DrawerContentStyle>
  );
};

DrawerContent.displayName = 'DrawerContent';
