"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _icons = require("@wordpress/icons");
var _data = require("@wordpress/data");
var _coreData = require("@wordpress/core-data");
var _i18n = require("@wordpress/i18n");
var _notices = require("@wordpress/notices");
var _element = require("@wordpress/element");
var _components = require("@wordpress/components");
var _constants = require("../../store/constants");
var _store = require("../../store");
var _lockUnlock = require("../../lock-unlock");
var _utils = require("./utils");
var _jsxRuntime = require("react/jsx-runtime");
/**
 * WordPress dependencies
 */

/**
 * Internal dependencies
 */

const resetPost = {
  id: 'reset-post',
  label: (0, _i18n.__)('Reset'),
  isEligible: item => {
    return (0, _utils.isTemplateOrTemplatePart)(item) && item?.source === _constants.TEMPLATE_ORIGINS.custom && (Boolean(item.type === 'wp_template' && item?.plugin) || item?.has_theme_file);
  },
  icon: _icons.backup,
  supportsBulk: true,
  hideModalHeader: true,
  RenderModal: ({
    items,
    closeModal,
    onActionPerformed
  }) => {
    const [isBusy, setIsBusy] = (0, _element.useState)(false);
    const {
      revertTemplate
    } = (0, _lockUnlock.unlock)((0, _data.useDispatch)(_store.store));
    const {
      saveEditedEntityRecord
    } = (0, _data.useDispatch)(_coreData.store);
    const {
      createSuccessNotice,
      createErrorNotice
    } = (0, _data.useDispatch)(_notices.store);
    const onConfirm = async () => {
      try {
        for (const template of items) {
          await revertTemplate(template, {
            allowUndo: false
          });
          await saveEditedEntityRecord('postType', template.type, template.id);
        }
        createSuccessNotice(items.length > 1 ? (0, _i18n.sprintf)( /* translators: The number of items. */
        (0, _i18n.__)('%s items reset.'), items.length) : (0, _i18n.sprintf)( /* translators: The template/part's name. */
        (0, _i18n.__)('"%s" reset.'), (0, _utils.getItemTitle)(items[0])), {
          type: 'snackbar',
          id: 'revert-template-action'
        });
      } catch (error) {
        let fallbackErrorMessage;
        if (items[0].type === _constants.TEMPLATE_POST_TYPE) {
          fallbackErrorMessage = items.length === 1 ? (0, _i18n.__)('An error occurred while reverting the template.') : (0, _i18n.__)('An error occurred while reverting the templates.');
        } else {
          fallbackErrorMessage = items.length === 1 ? (0, _i18n.__)('An error occurred while reverting the template part.') : (0, _i18n.__)('An error occurred while reverting the template parts.');
        }
        const typedError = error;
        const errorMessage = typedError.message && typedError.code !== 'unknown_error' ? typedError.message : fallbackErrorMessage;
        createErrorNotice(errorMessage, {
          type: 'snackbar'
        });
      }
    };
    return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_components.__experimentalVStack, {
      spacing: "5",
      children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_components.__experimentalText, {
        children: (0, _i18n.__)('Reset to default and clear all customizations?')
      }), /*#__PURE__*/(0, _jsxRuntime.jsxs)(_components.__experimentalHStack, {
        justify: "right",
        children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_components.Button, {
          __next40pxDefaultSize: true,
          variant: "tertiary",
          onClick: closeModal,
          disabled: isBusy,
          accessibleWhenDisabled: true,
          children: (0, _i18n.__)('Cancel')
        }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.Button, {
          __next40pxDefaultSize: true,
          variant: "primary",
          onClick: async () => {
            setIsBusy(true);
            await onConfirm();
            onActionPerformed?.(items);
            setIsBusy(false);
            closeModal?.();
          },
          isBusy: isBusy,
          disabled: isBusy,
          accessibleWhenDisabled: true,
          children: (0, _i18n.__)('Reset')
        })]
      })]
    });
  }
};
var _default = exports.default = resetPost;
//# sourceMappingURL=reset-post.js.map