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 { TabsContext } from './tabs.context';
import { ITabsVariants, tabsVariants } from './tabs.variants';

const TabsContentsStyle = styled.div<TabsContentsProps>`
  ${props => getVariantKey('contents', props.variant, props.variants)}
  ${props => getCssResponsive(props)}
`;

export type TabsContentsProps = UIStyledComponentProps<{
  className?: string;
  variant?: keyof typeof tabsVariants | (string & {});
  variants?: ITabsVariants;
  children?: ReactNode;
}>;

export const TabsContents = forwardRef<HTMLDivElement, TabsContentsProps>(
  function TabsContents({
    className,
    variant,
    variants,
    children,
    ...props
  }, ref) {
    const classNames = getClassNames('tabs__contents', className);
    const context = useContext(TabsContext);
    return (
      <TabsContentsStyle
        className={classNames}
        variant={variant || context.variant}
        variants={variants || context.variants}
        ref={ref}
        {...props}
      >{children}</TabsContentsStyle>
    );
  }
);
