"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/template-part/edit/inner-blocks.js
var inner_blocks_exports = {};
__export(inner_blocks_exports, {
  default: () => TemplatePartInnerBlocks
});
module.exports = __toCommonJS(inner_blocks_exports);
var import_core_data = require("@wordpress/core-data");
var import_block_editor = require("@wordpress/block-editor");
var import_data = require("@wordpress/data");
var import_element = require("@wordpress/element");
var import_blocks = require("@wordpress/blocks");
var import_jsx_runtime = require("react/jsx-runtime");
function useRenderAppender(hasInnerBlocks) {
  const blockEditingMode = (0, import_block_editor.useBlockEditingMode)();
  if (blockEditingMode === "contentOnly") {
    return false;
  }
  if (!hasInnerBlocks) {
    return import_block_editor.InnerBlocks.ButtonBlockAppender;
  }
}
function useLayout(layout) {
  const themeSupportsLayout = (0, import_data.useSelect)((select) => {
    const { getSettings } = select(import_block_editor.store);
    return getSettings()?.supportsLayout;
  }, []);
  const [defaultLayout] = (0, import_block_editor.useSettings)("layout");
  if (themeSupportsLayout) {
    return layout?.inherit ? defaultLayout || {} : layout;
  }
}
function NonEditableTemplatePartPreview({
  postId: id,
  layout,
  tagName: TagName,
  blockProps
}) {
  (0, import_block_editor.useBlockEditingMode)("disabled");
  const { content, editedBlocks } = (0, import_data.useSelect)(
    (select) => {
      if (!id) {
        return {};
      }
      const { getEditedEntityRecord } = select(import_core_data.store);
      const editedRecord = getEditedEntityRecord(
        "postType",
        "wp_template_part",
        id,
        { context: "view" }
      );
      return {
        editedBlocks: editedRecord.blocks,
        content: editedRecord.content
      };
    },
    [id]
  );
  const blocks = (0, import_element.useMemo)(() => {
    if (!id) {
      return void 0;
    }
    if (editedBlocks) {
      return editedBlocks;
    }
    if (!content || typeof content !== "string") {
      return [];
    }
    return (0, import_blocks.parse)(content);
  }, [id, editedBlocks, content]);
  const innerBlocksProps = (0, import_block_editor.useInnerBlocksProps)(blockProps, {
    value: blocks,
    onInput: () => {
    },
    onChange: () => {
    },
    renderAppender: false,
    layout: useLayout(layout)
  });
  return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(TagName, { ...innerBlocksProps });
}
function EditableTemplatePartInnerBlocks({
  postId: id,
  hasInnerBlocks,
  layout,
  tagName: TagName,
  blockProps
}) {
  const onNavigateToEntityRecord = (0, import_data.useSelect)(
    (select) => select(import_block_editor.store).getSettings().onNavigateToEntityRecord,
    []
  );
  const [blocks, onInput, onChange] = (0, import_core_data.useEntityBlockEditor)(
    "postType",
    "wp_template_part",
    { id }
  );
  const innerBlocksProps = (0, import_block_editor.useInnerBlocksProps)(blockProps, {
    value: blocks,
    onInput,
    onChange,
    renderAppender: useRenderAppender(hasInnerBlocks),
    layout: useLayout(layout)
  });
  const blockEditingMode = (0, import_block_editor.useBlockEditingMode)();
  const customProps = blockEditingMode === "contentOnly" && onNavigateToEntityRecord ? {
    onDoubleClick: () => onNavigateToEntityRecord({
      postId: id,
      postType: "wp_template_part"
    })
  } : {};
  return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(TagName, { ...innerBlocksProps, ...customProps });
}
function TemplatePartInnerBlocks({
  postId: id,
  hasInnerBlocks,
  layout,
  tagName: TagName,
  blockProps
}) {
  const { canViewTemplatePart, canEditTemplatePart } = (0, import_data.useSelect)(
    (select) => {
      return {
        canViewTemplatePart: !!select(import_core_data.store).canUser("read", {
          kind: "postType",
          name: "wp_template_part",
          id
        }),
        canEditTemplatePart: !!select(import_core_data.store).canUser("update", {
          kind: "postType",
          name: "wp_template_part",
          id
        })
      };
    },
    [id]
  );
  if (!canViewTemplatePart) {
    return null;
  }
  const TemplatePartInnerBlocksComponent = canEditTemplatePart ? EditableTemplatePartInnerBlocks : NonEditableTemplatePartPreview;
  return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
    TemplatePartInnerBlocksComponent,
    {
      postId: id,
      hasInnerBlocks,
      layout,
      tagName: TagName,
      blockProps
    }
  );
}
//# sourceMappingURL=inner-blocks.js.map
