"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _data = require("@wordpress/data");
var _i18n = require("@wordpress/i18n");
var _notices = require("@wordpress/notices");
var _element = require("@wordpress/element");
var _blocks = require("@wordpress/blocks");
var _constants = require("../../store/constants");
var _createTemplatePartModal = require("../../components/create-template-part-modal");
var _utils = require("./utils");
var _jsxRuntime = require("react/jsx-runtime");
/**
 * WordPress dependencies
 */

// @ts-ignore

/**
 * Internal dependencies
 */

const duplicateTemplatePart = {
  id: 'duplicate-template-part',
  label: (0, _i18n._x)('Duplicate', 'action label'),
  isEligible: item => item.type === _constants.TEMPLATE_PART_POST_TYPE,
  modalHeader: (0, _i18n._x)('Duplicate template part', 'action label'),
  RenderModal: ({
    items,
    closeModal
  }) => {
    const [item] = items;
    const blocks = (0, _element.useMemo)(() => {
      var _item$blocks;
      return (_item$blocks = item.blocks) !== null && _item$blocks !== void 0 ? _item$blocks : (0, _blocks.parse)(typeof item.content === 'string' ? item.content : item.content.raw, {
        __unstableSkipMigrationLogs: true
      });
    }, [item.content, item.blocks]);
    const {
      createSuccessNotice
    } = (0, _data.useDispatch)(_notices.store);
    function onTemplatePartSuccess() {
      createSuccessNotice((0, _i18n.sprintf)(
      // translators: %s: The new template part's title e.g. 'Call to action (copy)'.
      (0, _i18n.__)('"%s" duplicated.'), (0, _utils.getItemTitle)(item)), {
        type: 'snackbar',
        id: 'edit-site-patterns-success'
      });
      closeModal?.();
    }
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(_createTemplatePartModal.CreateTemplatePartModalContents, {
      blocks: blocks,
      defaultArea: item.area,
      defaultTitle: (0, _i18n.sprintf)( /* translators: %s: Existing template part title */
      (0, _i18n.__)('%s (Copy)'), (0, _utils.getItemTitle)(item)),
      onCreate: onTemplatePartSuccess,
      onError: closeModal,
      confirmLabel: (0, _i18n._x)('Duplicate', 'action label'),
      closeModal: closeModal
    });
  }
};
var _default = exports.default = duplicateTemplatePart;
//# sourceMappingURL=duplicate-template-part.js.map