import { isEqual, isNil } from 'lodash-es';
import { useState } from 'react';
import { Tab } from '../../components/Tab';
import { MaterialDesign3SchemeStorage } from '../../material-3-scheme';
import { SchemeContent } from '../../models';
import { SchemeExport } from './Export';
import { M3SchemeBuilder } from './m3-scheme/Builder';
import { M3SchemePreview } from './m3-scheme/Preview';

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

type M3SchemeProps = {
  scheme: SchemeContent<MaterialDesign3SchemeStorage>;
};

export function M3Scheme({ scheme }: M3SchemeProps) {
  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)} />
      {isEqual(activeTab, 'overview') && <M3SchemePreview scheme={scheme.schemeStorage.scheme} />}
      {isEqual(activeTab, 'builder') && (
        <M3SchemeBuilder scheme={scheme} onBuildCompleted={() => setActiveTab('overview')} />
      )}
      {isEqual(activeTab, 'export') && <SchemeExport scheme={scheme} />}
    </>
  );
}
