import { getClassNames } from '@websolutespa/bom-core';
import { forwardRef, 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 StyledBackdrop = styled.div <DrawerBackdropProps>`
  ${props => getVariantKey('backdrop', props.variant, props.variants)}
  ${props => getCssResponsive(props)}
`;

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

export const DrawerBackdrop = forwardRef<HTMLDivElement, DrawerBackdropProps>(
  function DrawerBackdrop({
    className,
    variant,
    variants,
    ...props
  }, ref) {
    const classNames = getClassNames('drawer__backdrop', className);
    const context = useContext(DrawerContext);
    if (!context) {
      return;
    }
    return (
      <StyledBackdrop
        className={classNames}
        variant={variant || context.variant}
        variants={variants || context.variants}
        ref={ref}
        {...props}
      />
    );
  });
