"use strict";

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

// @ts-ignore

/**
 * Internal dependencies
 */

// Patterns.
const {
  PATTERN_TYPES
} = (0, _lockUnlock.unlock)(_patterns.privateApis);
const renamePost = {
  id: 'rename-post',
  label: (0, _i18n.__)('Rename'),
  isEligible(post) {
    if (post.status === 'trash') {
      return false;
    }
    // Templates, template parts and patterns have special checks for renaming.
    if (![_constants.TEMPLATE_POST_TYPE, _constants.TEMPLATE_PART_POST_TYPE, ...Object.values(PATTERN_TYPES)].includes(post.type)) {
      return post.permissions?.update;
    }

    // In the case of templates, we can only rename custom templates.
    if ((0, _utils.isTemplate)(post)) {
      return (0, _utils.isTemplateRemovable)(post) && post.is_custom && post.permissions?.update;
    }
    if ((0, _utils.isTemplatePart)(post)) {
      return post.source === _constants.TEMPLATE_ORIGINS.custom && !post?.has_theme_file && post.permissions?.update;
    }
    return post.type === PATTERN_TYPES.user && post.permissions?.update;
  },
  RenderModal: ({
    items,
    closeModal,
    onActionPerformed
  }) => {
    const [item] = items;
    const [title, setTitle] = (0, _element.useState)(() => (0, _utils.getItemTitle)(item));
    const {
      editEntityRecord,
      saveEditedEntityRecord
    } = (0, _data.useDispatch)(_coreData.store);
    const {
      createSuccessNotice,
      createErrorNotice
    } = (0, _data.useDispatch)(_notices.store);
    async function onRename(event) {
      event.preventDefault();
      try {
        await editEntityRecord('postType', item.type, item.id, {
          title
        });
        // Update state before saving rerenders the list.
        setTitle('');
        closeModal?.();
        // Persist edited entity.
        await saveEditedEntityRecord('postType', item.type, item.id, {
          throwOnError: true
        });
        createSuccessNotice((0, _i18n.__)('Name updated'), {
          type: 'snackbar'
        });
        onActionPerformed?.(items);
      } catch (error) {
        const typedError = error;
        const errorMessage = typedError.message && typedError.code !== 'unknown_error' ? typedError.message : (0, _i18n.__)('An error occurred while updating the name');
        createErrorNotice(errorMessage, {
          type: 'snackbar'
        });
      }
    }
    return /*#__PURE__*/(0, _jsxRuntime.jsx)("form", {
      onSubmit: onRename,
      children: /*#__PURE__*/(0, _jsxRuntime.jsxs)(_components.__experimentalVStack, {
        spacing: "5",
        children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_components.TextControl, {
          __nextHasNoMarginBottom: true,
          __next40pxDefaultSize: true,
          label: (0, _i18n.__)('Name'),
          value: title,
          onChange: setTitle,
          required: true
        }), /*#__PURE__*/(0, _jsxRuntime.jsxs)(_components.__experimentalHStack, {
          justify: "right",
          children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_components.Button, {
            __next40pxDefaultSize: true,
            variant: "tertiary",
            onClick: () => {
              closeModal?.();
            },
            children: (0, _i18n.__)('Cancel')
          }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.Button, {
            __next40pxDefaultSize: true,
            variant: "primary",
            type: "submit",
            children: (0, _i18n.__)('Save')
          })]
        })]
      })
    });
  }
};
var _default = exports.default = renamePost;
//# sourceMappingURL=rename-post.js.map