import { isEqual, isNil } from 'lodash-es';
import { useState } from 'react';
import { Tab } from '../../components/Tab';
import { SchemeContent } from '../../models';
import { QSchemeStorage } from '../../q-scheme';
import { SchemeExport } from './Export';
import { QSchemeBuilder } from './q-scheme/Builder';
import { QSchemePreview } from './q-scheme/Preview';

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

type QSchemeProps = {
  scheme: SchemeContent<QSchemeStorage>;
};

export function QScheme({ scheme }: QSchemeProps) {
  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') && <QSchemePreview scheme={scheme} />}
      {isEqual(activeTab, 'builder') && (
        <QSchemeBuilder scheme={scheme} onBuildCompleted={() => setActiveTab('overview')} />
      )}
      {isEqual(activeTab, 'export') && <SchemeExport scheme={scheme} />}
    </>
  );
}
