import {
  CollectionPageState,
  DataExtensionOverrides,
  FiltersMap,
  WixPatternsContainer,
} from '@wix/bex-core';
import { ToolbarCollectionState } from '../../state';
import { CustomFieldModal } from '../CustomFieldModal';
import { CustomColumnsFull } from '../CustomColumns';
import { ArchiveFieldModal } from '../CustomFieldsPanel/ArchiveFieldModal';
import { CustomColumnsAddCustomFieldCTAButton } from '../CustomFieldsPanel/CustomColumnsAddCustomFieldCTAButton';
import { CustomColumnsEditCustomFieldCTAButton } from '../CustomFieldsPanel/CustomColumnsEditCustomFieldCTAButton';
import { ArchivedItemsCTAButton } from '../ArchivedItemsPanel/ArchivedItemsCTAButton';
import { CollectionDataExtensionState } from './CollectionDataExtensionState';
import { ReactElement } from 'react';
import { FileCardList } from '../FileCard/FileCardList';
import { CustomFieldsCollectionPanelEntry } from '../CustomFieldsPanel/CustomFieldsCollectionPanelEntry';
import { MoreActionsItem } from '../MoreActions';
import { manageCustomFieldsPageAction } from './manageCustomFieldsPageAction';

export type DataExtensionObjectType = {
  setDataExtensionOnTable: <T, F extends FiltersMap>(
    toolbar: ToolbarCollectionState<T, F>,
    overrides?: DataExtensionProps,
  ) => void;
  CustomFieldModal: typeof CustomFieldModal;
  ArchiveFieldModal: typeof ArchiveFieldModal;
  FileCardList: typeof FileCardList;
  CustomColumns: typeof CustomColumnsFull;
  CustomColumnsAddCustomFieldCTAButton: typeof CustomColumnsAddCustomFieldCTAButton;
  CustomColumnsEditCustomFieldCTAButton: typeof CustomColumnsEditCustomFieldCTAButton;
  CustomColumnsArchivedItemsCTAButton: typeof ArchivedItemsCTAButton;
  syncTableCollectionProps: (state: ToolbarCollectionState<any, any>) => void;
  manageCustomFieldsPageAction: (params: {
    pageState: CollectionPageState;
    container: WixPatternsContainer;
  }) => MoreActionsItem;
};

export type DataExtensionProps = DataExtensionOverrides;

export const DataExtension: React.FC<DataExtensionProps> &
  DataExtensionObjectType = (_: DataExtensionProps) => {
  return null;
};

export type DataExtensionElement = ReactElement<
  DataExtensionProps,
  typeof DataExtension
>;

export type DataExtensionType = typeof DataExtension;

DataExtension.setDataExtensionOnTable = function (toolbar, overrides) {
  toolbar.collectionDataExtension ??= new CollectionDataExtensionState({
    toolbar,
    container: toolbar.container,
    DataExtension,
    props: overrides,
    ...overrides,
  });
};
DataExtension.FileCardList = FileCardList;
DataExtension.CustomFieldModal = CustomFieldModal;
DataExtension.ArchiveFieldModal = ArchiveFieldModal;
DataExtension.CustomColumns = CustomColumnsFull;
DataExtension.CustomColumnsAddCustomFieldCTAButton =
  CustomColumnsAddCustomFieldCTAButton;
DataExtension.CustomColumnsEditCustomFieldCTAButton =
  CustomColumnsEditCustomFieldCTAButton;
DataExtension.CustomColumnsArchivedItemsCTAButton = ArchivedItemsCTAButton;
DataExtension.syncTableCollectionProps = (
  state: ToolbarCollectionState<any>,
) => {
  state.panels.registerSidePanel(
    'customFields',
    CustomFieldsCollectionPanelEntry,
  );
};
DataExtension.manageCustomFieldsPageAction = manageCustomFieldsPageAction;
