"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// packages/block-library/src/navigation/edit/unsaved-inner-blocks.js
var unsaved_inner_blocks_exports = {};
__export(unsaved_inner_blocks_exports, {
  default: () => UnsavedInnerBlocks
});
module.exports = __toCommonJS(unsaved_inner_blocks_exports);
var import_block_editor = require("@wordpress/block-editor");
var import_components = require("@wordpress/components");
var import_core_data = require("@wordpress/core-data");
var import_data = require("@wordpress/data");
var import_element = require("@wordpress/element");
var import_are_blocks_dirty = require("./are-blocks-dirty");
var import_constants = require("../constants");
var import_jsx_runtime = require("react/jsx-runtime");
var EMPTY_OBJECT = {};
function UnsavedInnerBlocks({
  blocks,
  createNavigationMenu,
  hasSelection
}) {
  const originalBlocksRef = (0, import_element.useRef)();
  (0, import_element.useEffect)(() => {
    if (!originalBlocksRef?.current) {
      originalBlocksRef.current = blocks;
    }
  }, [blocks]);
  const innerBlocksAreDirty = (0, import_are_blocks_dirty.areBlocksDirty)(
    originalBlocksRef?.current,
    blocks
  );
  const isDisabled = (0, import_element.useContext)(import_components.Disabled.Context);
  const innerBlocksProps = (0, import_block_editor.useInnerBlocksProps)(
    {
      className: "wp-block-navigation__container"
    },
    {
      renderAppender: hasSelection ? void 0 : false,
      defaultBlock: import_constants.DEFAULT_BLOCK,
      directInsert: true
    }
  );
  const { isSaving, hasResolvedAllNavigationMenus } = (0, import_data.useSelect)(
    (select) => {
      if (isDisabled) {
        return EMPTY_OBJECT;
      }
      const { hasFinishedResolution, isSavingEntityRecord } = select(import_core_data.store);
      return {
        isSaving: isSavingEntityRecord("postType", "wp_navigation"),
        hasResolvedAllNavigationMenus: hasFinishedResolution(
          "getEntityRecords",
          import_constants.SELECT_NAVIGATION_MENUS_ARGS
        )
      };
    },
    [isDisabled]
  );
  (0, import_element.useEffect)(() => {
    if (isDisabled || isSaving || !hasResolvedAllNavigationMenus || !hasSelection || !innerBlocksAreDirty) {
      return;
    }
    createNavigationMenu(null, blocks);
  }, [
    blocks,
    createNavigationMenu,
    isDisabled,
    isSaving,
    hasResolvedAllNavigationMenus,
    innerBlocksAreDirty,
    hasSelection
  ]);
  const Wrapper = isSaving ? import_components.Disabled : "div";
  return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Wrapper, { ...innerBlocksProps });
}
//# sourceMappingURL=unsaved-inner-blocks.js.map
