"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);

// packages/block-editor/src/components/block-rename/modal.js
var modal_exports = {};
__export(modal_exports, {
  default: () => BlockRenameModal
});
module.exports = __toCommonJS(modal_exports);
var import_components = require("@wordpress/components");
var import_i18n = require("@wordpress/i18n");
var import_element = require("@wordpress/element");
var import_a11y = require("@wordpress/a11y");
var import_data = require("@wordpress/data");
var import_store = require("../../store/index.cjs");
var import__ = require("../index.cjs");
var import_is_empty_string = __toESM(require("./is-empty-string.cjs"));
var import_utils = require("../../hooks/utils.cjs");
var import_jsx_runtime = require("react/jsx-runtime");
function BlockRenameModal({ clientId, onClose }) {
  const [editedBlockName, setEditedBlockName] = (0, import_element.useState)();
  const blockInformation = (0, import__.useBlockDisplayInformation)(clientId);
  const { metadata } = (0, import_data.useSelect)(
    (select) => {
      const { getBlockAttributes } = select(import_store.store);
      return {
        metadata: getBlockAttributes(clientId)?.metadata
      };
    },
    [clientId]
  );
  const { updateBlockAttributes } = (0, import_data.useDispatch)(import_store.store);
  const blockName = metadata?.name || "";
  const originalBlockName = blockInformation?.title;
  const hasOverridesWarning = !!blockName && !!metadata?.bindings && Object.values(metadata.bindings).some(
    (binding) => binding.source === "core/pattern-overrides"
  );
  const nameHasChanged = editedBlockName !== void 0 && editedBlockName !== blockName;
  const nameIsOriginal = editedBlockName === originalBlockName;
  const nameIsEmpty = (0, import_is_empty_string.default)(editedBlockName);
  const isNameValid = nameHasChanged || nameIsOriginal;
  const autoSelectInputText = (event) => event.target.select();
  const handleSubmit = () => {
    const newName = nameIsOriginal || nameIsEmpty ? void 0 : editedBlockName;
    const message = nameIsOriginal || nameIsEmpty ? (0, import_i18n.sprintf)(
      /* translators: %s: new name/label for the block */
      (0, import_i18n.__)('Block name reset to: "%s".'),
      editedBlockName
    ) : (0, import_i18n.sprintf)(
      /* translators: %s: new name/label for the block */
      (0, import_i18n.__)('Block name changed to: "%s".'),
      editedBlockName
    );
    (0, import_a11y.speak)(message, "assertive");
    updateBlockAttributes([clientId], {
      metadata: (0, import_utils.cleanEmptyObject)({
        ...metadata,
        name: newName
      })
    });
    onClose();
  };
  return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
    import_components.Modal,
    {
      title: (0, import_i18n.__)("Rename"),
      onRequestClose: onClose,
      overlayClassName: "block-editor-block-rename-modal",
      focusOnMount: "firstContentElement",
      size: "small",
      children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
        "form",
        {
          onSubmit: (e) => {
            e.preventDefault();
            if (!isNameValid) {
              return;
            }
            handleSubmit();
          },
          children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_components.__experimentalVStack, { spacing: "3", children: [
            /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
              import_components.TextControl,
              {
                value: editedBlockName ?? blockName,
                label: (0, import_i18n.__)("Name"),
                help: hasOverridesWarning ? (0, import_i18n.__)(
                  "This block allows overrides. Changing the name can cause problems with content entered into instances of this pattern."
                ) : void 0,
                placeholder: originalBlockName,
                onChange: setEditedBlockName,
                onFocus: autoSelectInputText
              }
            ),
            /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_components.__experimentalHStack, { justify: "right", children: [
              /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
                import_components.Button,
                {
                  __next40pxDefaultSize: true,
                  variant: "tertiary",
                  onClick: onClose,
                  children: (0, import_i18n.__)("Cancel")
                }
              ),
              /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
                import_components.Button,
                {
                  __next40pxDefaultSize: true,
                  accessibleWhenDisabled: true,
                  disabled: !isNameValid,
                  variant: "primary",
                  type: "submit",
                  children: (0, import_i18n.__)("Save")
                }
              )
            ] })
          ] })
        }
      )
    }
  );
}
//# sourceMappingURL=modal.cjs.map
