"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = DeleteCategoryMenuItem;
var _components = require("@wordpress/components");
var _coreData = require("@wordpress/core-data");
var _data = require("@wordpress/data");
var _element = require("@wordpress/element");
var _htmlEntities = require("@wordpress/html-entities");
var _i18n = require("@wordpress/i18n");
var _notices = require("@wordpress/notices");
var _router = require("@wordpress/router");
var _lockUnlock = require("../../lock-unlock");
var _constants = require("../../utils/constants");
var _jsxRuntime = require("react/jsx-runtime");
/**
 * WordPress dependencies
 */

/**
 * Internal dependencies
 */

const {
  useHistory
} = (0, _lockUnlock.unlock)(_router.privateApis);
function DeleteCategoryMenuItem({
  category,
  onClose
}) {
  const [isModalOpen, setIsModalOpen] = (0, _element.useState)(false);
  const history = useHistory();
  const {
    createSuccessNotice,
    createErrorNotice
  } = (0, _data.useDispatch)(_notices.store);
  const {
    deleteEntityRecord,
    invalidateResolution
  } = (0, _data.useDispatch)(_coreData.store);
  const onDelete = async () => {
    try {
      await deleteEntityRecord('taxonomy', 'wp_pattern_category', category.id, {
        force: true
      }, {
        throwOnError: true
      });

      // Prevent the need to refresh the page to get up-to-date categories
      // and pattern categorization.
      invalidateResolution('getUserPatternCategories');
      invalidateResolution('getEntityRecords', ['postType', _constants.PATTERN_TYPES.user, {
        per_page: -1
      }]);
      createSuccessNotice((0, _i18n.sprintf)(/* translators: %s: The pattern category's name */
      (0, _i18n._x)('"%s" deleted.', 'pattern category'), category.label), {
        type: 'snackbar',
        id: 'pattern-category-delete'
      });
      onClose?.();
      history.navigate(`/pattern?categoryId=${_constants.PATTERN_DEFAULT_CATEGORY}`);
    } catch (error) {
      const errorMessage = error.message && error.code !== 'unknown_error' ? error.message : (0, _i18n.__)('An error occurred while deleting the pattern category.');
      createErrorNotice(errorMessage, {
        type: 'snackbar',
        id: 'pattern-category-delete'
      });
    }
  };
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
    children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_components.MenuItem, {
      isDestructive: true,
      onClick: () => setIsModalOpen(true),
      children: (0, _i18n.__)('Delete')
    }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.__experimentalConfirmDialog, {
      isOpen: isModalOpen,
      onConfirm: onDelete,
      onCancel: () => setIsModalOpen(false),
      confirmButtonText: (0, _i18n.__)('Delete'),
      className: "edit-site-patterns__delete-modal",
      title: (0, _i18n.sprintf)(
      // translators: %s: The pattern category's name.
      (0, _i18n._x)('Delete "%s"?', 'pattern category'), (0, _htmlEntities.decodeEntities)(category.label)),
      size: "medium",
      __experimentalHideHeader: false,
      children: (0, _i18n.sprintf)(
      // translators: %s: The pattern category's name.
      (0, _i18n.__)('Are you sure you want to delete the category "%s"? The patterns will not be deleted.'), (0, _htmlEntities.decodeEntities)(category.label))
    })]
  });
}
//# sourceMappingURL=delete-category-menu-item.js.map