import { Delete } from '@wix/wix-ui-icons-common';
import React from 'react';
import {
  CollectionOptimisticActions,
  DeleteManyParams,
  FiltersMap,
} from '@wix/bex-core';
import { ActionCellAPI } from '../../state';

export interface DeleteSecondaryActionProps<T, F extends FiltersMap, R>
  extends DeleteManyParams<T, R> {
  /**
   * Actions for performing optimistic updates on the collection.
   * These updates are applied immediately for better user experience
   * while waiting for the server response.
   * @external
   */
  optimisticActions: CollectionOptimisticActions<T, F>;
  /**
   * API for interacting with the action cell.
   * This API provides methods for manipulating and accessing the action cell's state.
   * @external
   */
  actionCellAPI: ActionCellAPI<T, F>;
}

export function deleteSecondaryAction<T, F extends FiltersMap, R>(
  props: DeleteSecondaryActionProps<T, F, R>,
) {
  const {
    optimisticActions,
    actionCellAPI: { openConfirmDeleteModal, collection, keyedItem },
    ...deleteManyParams
  } = props;
  const { translate: t } = collection;
  return {
    dataHook: 'collection-delete-action',
    text: t('cairo.delete.button'),
    icon: <Delete />,
    onClick: () => {
      openConfirmDeleteModal({
        primaryButtonOnClick: () => {
          optimisticActions.deleteOne(keyedItem.item, deleteManyParams);
        },
      });
    },
  };
}
