import {
  CustomTotalFn,
  FiltersMap,
  ModalState,
  SaveAsFn,
  TransformQuery,
} from '@wix/bex-core';
import { GetExportModalStepProps } from './getExportModalStepProps';
import {
  ArrayFieldDelimiter,
  CreateExportAsyncJobRequest,
  MethodSpec,
  QueryFieldNumber,
} from '@wix/bex-utils/@wix/ambassador-fedinfra-exportservice-v1-export-async-job/types';

type ExportButtonToolbarType = 'default' | 'bulkActionToolbar';

export interface ExportButtonParams<T, F extends FiltersMap> {
  renderError?: GetExportModalStepProps<T, F>['renderError'];
  /**
   *  A function to transform the cairo query to a QueryV2 object (which will be used to fetch data from the server):
   * - `platformizedQuery`: QueryV2
   * - `wixPatternsQuery`: ComputedQueryFull<F>
   * - `selectedItems?`: string[]
   * - `uncheckedValues?`: T[]
   * - `isSelectAll`: boolean
   * The function should return a QueryV2 instance or a promise of QueryV2
   */
  transformPlatformizedQuery?: TransformQuery<T, F>;
  /**
   * When set to `true`, the export progress will not show total / out of items.
   */
  differentQueryEndpointThanCollection?: boolean;
  /**
   * A function that returns the number of total items in the export, to show in the progress bar. Use this option when the export has a different total than the collection itself. For example: table is showing products, but the export includes also variants.
   * The function accepts the same arguments as `transformPlatformizedQuery`, with the following modifications:
   * - `platformizedQuery` is already the transformed QueryV2 returned from `transformPlatformizedQuery`
   * - `collectionTotalPromise` - A promise that resolves to the `total` of the underlying table collection.
   */
  customTotal?: CustomTotalFn<T, F>;
  /**
   * The target endpoint to query data from. This endpoint must be verified to be compatible with export-service first, see [server side docs](https://dev.wix.com/docs/rest/business-management/export/export-async-job-v1/integration)
   * - `artifact: string`
   * - `service: string`
   * - `method: string`
   */
  methodMetadata: CreateExportAsyncJobRequest['methodMetadata'];

  /**
   * The delimiter to use for array fields
   * @default SEMICOLON
   */
  arrayFieldDelimiter?: keyof typeof ArrayFieldDelimiter;

  /**
   * Additional endpoint possible variations
   * `requestQueryFieldNumber?: number` - The field number of the request query field. The default is field number = 1
   * `responseRepeatedFieldName: string | null` - The name of the response repeated field, i.e items/contacts/products etc. (required)
   * `responsePagingMetadataFieldName?: string | null` - The name of the response PagingMetadata field (default=paging_metadata)
   */
  methodSpec?: {
    requestQueryFieldNumber?: keyof typeof QueryFieldNumber;
    responsePagingMetadataFieldName?: MethodSpec['responsePagingMetadataFieldName'];
    responseRepeatedFieldName?: MethodSpec['responseRepeatedFieldName'];
  };
  /**
   * Translated CSV headers
   * - `id?`: string; The path to the field out of the query response items. You can ask your BED team to create a list of available paths by running this [Scala script](https://bo.wix.com/wix-docs/rest/drafts/exportservice/integration#drafts_exportservice_integration_fields).
   * - `header?`: string; How to present the filed in the CSV headers (translated).
   */
  fields: CreateExportAsyncJobRequest['fields'];
  /**
   *  Optional function to configure the download file name:
   * - `platformizedQuery`: QueryV2
   * - `wixPatternsQuery`: ComputedQueryFull<F>
   * - `selectedItems?`: string[]
   * - `uncheckedValues?`: T[]
   * The function should return a string
   */
  saveAs?: SaveAsFn<T, F>;
  /**
   * Optional footer for export modal
   * `text`: string
   */
  exportModalFooter?: GetExportModalStepProps<T, F>['exportModalFooter'];
  /**
   * Optional custom title for the items to export
   */
  exportModalTitle?: GetExportModalStepProps<T, F>['exportModalTitle'];
  /**
   * Optional custom title during the export process
   */
  onProgressModalTitle?: GetExportModalStepProps<T, F>['onProgressModalTitle'];
  /**
   * Optional custom message for the items that will be exported to CSV
   */
  exportToCSVMessage?: GetExportModalStepProps<T, F>['exportToCSVMessage'];
}

export type ExportButtonProps<T, F extends FiltersMap> = ExportButtonParams<
  T,
  F
> & { toolbarType?: ExportButtonToolbarType };

export type ExportModalProps<T, F extends FiltersMap> = ExportButtonParams<
  T,
  F
>;

export type ExportButtonModalLayoutProps<
  T,
  F extends FiltersMap,
> = ExportModalProps<T, F> & {
  exportModalState: ModalState;
};
