import * as Ariakit from '@ariakit/react';
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 { TabsContext } from './tabs.context';
import { ITabsVariants, tabsVariants } from './tabs.variants';

const TabsContentStyle = styled(Ariakit.TabPanel) <TabsContentProps>`
  ${props => getVariantKey('content', props.variant, props.variants)}
  ${props => getCssResponsive(props)}
`;

export type TabsContentProps = Ariakit.TabPanelProps & UIStyledComponentProps<{
  className?: string;
  variant?: keyof typeof tabsVariants | (string & {});
  variants?: ITabsVariants;
}>;

export const TabsContent = forwardRef<HTMLDivElement, TabsContentProps>(
  function TabsContent({ className, children, variant, variants, ...props }, ref) {
    const classNames = getClassNames('tabs__content', className);
    const context = useContext(TabsContext);
    return (
      <TabsContentStyle
        className={classNames}
        variant={variant || context.variant}
        variants={variants || context.variants}
        ref={ref}
        {...props}
      >
        <div className="tabs__clipped">
          <div className="tabs__inner">
            {children}
          </div>
        </div>
      </TabsContentStyle>
    );
  }
);
