"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var content_only_settings_menu_exports = {};
__export(content_only_settings_menu_exports, {
  default: () => ContentOnlySettingsMenu
});
module.exports = __toCommonJS(content_only_settings_menu_exports);
var import_jsx_runtime = require("react/jsx-runtime");
var import_block_editor = require("@wordpress/block-editor");
var import_core_data = require("@wordpress/core-data");
var import_components = require("@wordpress/components");
var import_data = require("@wordpress/data");
var import_i18n = require("@wordpress/i18n");
var import_store = require("../../store");
var import_lock_unlock = require("../../lock-unlock");
var import_use_post_content_blocks = __toESM(require("../provider/use-post-content-blocks"));
function ContentOnlySettingsMenuItems({ clientId, onClose }) {
  const postContentBlocks = (0, import_use_post_content_blocks.default)();
  const { entity, onNavigateToEntityRecord, canEditTemplates } = (0, import_data.useSelect)(
    (select) => {
      const {
        getBlockParentsByBlockName,
        getSettings,
        getBlockAttributes,
        getBlockParents
      } = select(import_block_editor.store);
      const { getCurrentTemplateId, getRenderingMode } = select(import_store.store);
      const patternParent = getBlockParentsByBlockName(
        clientId,
        "core/block",
        true
      )[0];
      let record;
      if (patternParent) {
        record = select(import_core_data.store).getEntityRecord(
          "postType",
          "wp_block",
          getBlockAttributes(patternParent).ref
        );
      } else if (getRenderingMode() === "template-locked" && !getBlockParents(clientId).some(
        (parent) => postContentBlocks.includes(parent)
      )) {
        record = select(import_core_data.store).getEntityRecord(
          "postType",
          "wp_template",
          getCurrentTemplateId()
        );
      }
      if (!record) {
        return {};
      }
      const _canEditTemplates = select(import_core_data.store).canUser("create", {
        kind: "postType",
        name: "wp_template"
      });
      return {
        canEditTemplates: _canEditTemplates,
        entity: record,
        onNavigateToEntityRecord: getSettings().onNavigateToEntityRecord
      };
    },
    [clientId, postContentBlocks]
  );
  if (!entity) {
    return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
      TemplateLockContentOnlyMenuItems,
      {
        clientId,
        onClose
      }
    );
  }
  const isPattern = entity.type === "wp_block";
  let helpText = isPattern ? (0, import_i18n.__)(
    "Edit the pattern to move, delete, or make further changes to this block."
  ) : (0, import_i18n.__)(
    "Edit the template to move, delete, or make further changes to this block."
  );
  if (!canEditTemplates) {
    helpText = (0, import_i18n.__)(
      "Only users with permissions to edit the template can move or delete this block"
    );
  }
  return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [
    /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_block_editor.__unstableBlockSettingsMenuFirstItem, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
      import_components.MenuItem,
      {
        onClick: () => {
          onNavigateToEntityRecord({
            postId: entity.id,
            postType: entity.type
          });
        },
        disabled: !canEditTemplates,
        children: isPattern ? (0, import_i18n.__)("Edit pattern") : (0, import_i18n.__)("Edit template")
      }
    ) }),
    /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
      import_components.__experimentalText,
      {
        variant: "muted",
        as: "p",
        className: "editor-content-only-settings-menu__description",
        children: helpText
      }
    )
  ] });
}
function TemplateLockContentOnlyMenuItems({ clientId, onClose }) {
  const { contentLockingParent } = (0, import_data.useSelect)(
    (select) => {
      const { getContentLockingParent } = (0, import_lock_unlock.unlock)(
        select(import_block_editor.store)
      );
      return {
        contentLockingParent: getContentLockingParent(clientId)
      };
    },
    [clientId]
  );
  const blockDisplayInformation = (0, import_block_editor.useBlockDisplayInformation)(contentLockingParent);
  const blockEditorActions = (0, import_data.useDispatch)(import_block_editor.store);
  if (!blockDisplayInformation?.title) {
    return null;
  }
  const { modifyContentLockBlock } = (0, import_lock_unlock.unlock)(blockEditorActions);
  return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [
    /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_block_editor.__unstableBlockSettingsMenuFirstItem, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
      import_components.MenuItem,
      {
        onClick: () => {
          modifyContentLockBlock(contentLockingParent);
          onClose();
        },
        children: (0, import_i18n._x)("Unlock", "Unlock content locked blocks")
      }
    ) }),
    /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
      import_components.__experimentalText,
      {
        variant: "muted",
        as: "p",
        className: "editor-content-only-settings-menu__description",
        children: (0, import_i18n.__)(
          "Temporarily unlock the parent block to edit, delete or make further changes to this block."
        )
      }
    )
  ] });
}
function ContentOnlySettingsMenu() {
  return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_block_editor.BlockSettingsMenuControls, { children: ({ selectedClientIds, onClose }) => selectedClientIds.length === 1 && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
    ContentOnlySettingsMenuItems,
    {
      clientId: selectedClientIds[0],
      onClose
    }
  ) });
}
//# sourceMappingURL=content-only-settings-menu.js.map
