import { isEqual, isNil } from 'lodash-es';
import { useState } from 'react';
import { Tab } from '../../components/Tab';
import { SchemeContent } from '../../models';
import { SwatchSchemeStorage } from '../../swatch_scheme';
import { isNilOrEmpty } from '../../utls';
import { SchemeExport } from './Export';
import { SwatchSchemeBuilder } from './swatch-scheme/Builder';
import { SwatchSchemePreview } from './swatch-scheme/Preview';

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

type SwatchSchemeProps = {
  scheme: SchemeContent<SwatchSchemeStorage>;
};

export function SwatchScheme({ scheme }: SwatchSchemeProps) {
  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, 'overview') && <SwatchSchemePreview scheme={scheme} />}
      {isEqual(activeTab, 'builder') && (
        <SwatchSchemeBuilder scheme={scheme} onBuildCompleted={() => setActiveTab('overview')} />
      )}
      {isEqual(activeTab, 'export') && <SchemeExport scheme={scheme} />}
    </>
  );
}
