import { FilterOperator } from '@wix/bex-core';
import type { FieldsSourceRendering } from '../../state/CollectionFieldsSourceState';
import type { SchemaState } from './SchemaState';
import {
  createSchemaFieldActionsMenu,
  schemaFieldActionHandlers,
} from './schemaFieldActions';
import { FieldManagementModals } from '../CustomFieldsPanel/FieldManagementModals';
import { CustomColumnsFull } from '../CustomColumns/CustomColumnsFull';
import { CollectionToolbarFilters } from '../CollectionToolbarFilters';

// Text operators a schema source's backend can execute.
const SCHEMA_TEXT_OPERATORS = [
  FilterOperator.CONTAINS,
  FilterOperator.NOT_CONTAINS,
  FilterOperator.IS_EMPTY,
  FilterOperator.EXISTS,
];

/**
 * Builds the schema source's config, shaped as a FieldsSourceRendering. The spine
 * builds the field-management panel from this; the per-field actions come from
 * schemaFieldActions.
 */
export const schemaRendering = (
  s: SchemaState<any>,
): FieldsSourceRendering => ({
  filterConfig: {
    textOperators: SCHEMA_TEXT_OPERATORS,
    numberIsDecimal: () => true,
  },
  createFieldActionsMenu: createSchemaFieldActionsMenu(s),
  createFieldActionHandlers: (panel) => schemaFieldActionHandlers(s, panel),
  featuredPageActions: [],
  components: {
    FieldManagementModals,
    CustomColumns: CustomColumnsFull,
    CollectionToolbarFilters,
  },
});
