import { isEqual, isNil } from 'lodash-es';
import { useState } from 'react';
import { Tab } from '../../components/Tab';
import { SchemeContent } from '../../models';
import { Q2SchemeStorage } from '../../q-2-scheme';
import { isNilOrEmpty } from '../../utls';
import { SchemeExport } from './Export';

const tabOptions = [
  { title: 'Overview', id: 'overview' },
  { title: 'Builder', id: 'builder' },
  { title: 'Exports', id: 'export' },
];

type Q2SchemeProps = {
  scheme: SchemeContent<Q2SchemeStorage>;
};

export function Q2Scheme({ scheme }: Q2SchemeProps) {
  const [activeTab, setActiveTab] = useState<(typeof tabOptions)[number]['id']>(() =>
    isNil(scheme.schemeStorage.scheme) ? 'builder' : 'overview',
  );

  return (
    <>
      <Tab
        tabs={tabOptions}
        activeTab={activeTab}
        onActive={(v) => setActiveTab(v as string)}
        disabled={{
          overview: isNilOrEmpty(scheme.schemeStorage?.scheme),
          export: isNilOrEmpty(scheme.schemeStorage?.cssVariables),
        }}
      />
      {isEqual(activeTab, 'export') && <SchemeExport scheme={scheme} />}
    </>
  );
}
