"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _react = _interopRequireWildcard(require("react"));

var _reactRouter = require("react-router");

var _propertyType = _interopRequireDefault(require("../property-type"));

var _apiClient = _interopRequireDefault(require("../../utils/api-client"));

var _withNotice = _interopRequireDefault(require("../../store/with-notice"));

var _appendForceRefresh = require("./utils/append-force-refresh");

var _designSystem = require("../design-system");

var _actionHeader = _interopRequireDefault(require("../app/action-header"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

/**
 * @name ShowAction
 * @category Actions
 * @description Shows a given record.
 * @component
 * @private
 */
const BulkDelete = props => {
  const {
    resource,
    records,
    action,
    addNotice,
    history
  } = props;
  const [loading, setLoading] = (0, _react.useState)(false);

  if (!records) {
    return _react.default.createElement(_designSystem.Text, null, "In order to remove records, you have to pick them first.");
  }

  const handleClick = () => {
    const api = new _apiClient.default();
    setLoading(true);
    const recordIds = records.map(r => r.id);
    api.bulkAction({
      resourceId: resource.id,
      actionName: action.name,
      recordIds,
      method: 'post'
    }).then(response => {
      setLoading(false);

      if (response.data.notice) {
        addNotice(response.data.notice);
      }

      if (response.data.redirectUrl) {
        const search = new URLSearchParams(window.location.search); // bulk function have recordIds in the URL so it has to be stripped before redirect

        search.delete('recordIds');
        history.push((0, _appendForceRefresh.appendForceRefresh)(response.data.redirectUrl, search.toString()));
      }
    }).catch(error => {
      setLoading(false);
      addNotice({
        message: 'There was an error deleting records, Check out console to see more information.',
        type: 'error'
      });
      throw error;
    });
  };

  return _react.default.createElement(_react.default.Fragment, null, _react.default.createElement(_designSystem.DrawerContent, null, _react.default.createElement(_actionHeader.default, props), _react.default.createElement(_designSystem.MessageBox, {
    mb: "xxl",
    variant: "danger",
    message: "Following records will be removed"
  }), _react.default.createElement(_designSystem.Table, null, _react.default.createElement(_designSystem.TableBody, null, records.map(record => _react.default.createElement(_designSystem.TableRow, {
    key: record.id
  }, _react.default.createElement(_designSystem.TableCell, null, _react.default.createElement(_propertyType.default, {
    where: "list",
    property: resource.titleProperty,
    resource: resource,
    record: record
  }))))))), _react.default.createElement(_designSystem.DrawerFooter, null, _react.default.createElement(_designSystem.Button, {
    variant: "primary",
    size: "lg",
    onClick: handleClick
  }, `Confirm the removal of ${records.length} records`)));
};

var _default = (0, _withNotice.default)((0, _reactRouter.withRouter)(BulkDelete));

exports.default = _default;