// packages/block-editor/src/components/inner-blocks/index.js
import clsx from "clsx";
import { useMergeRefs } from "@wordpress/compose";
import { forwardRef, useMemo, memo } from "@wordpress/element";
import { useSelect } from "@wordpress/data";
import {
  getBlockSupport,
  store as blocksStore,
  __unstableGetInnerBlocksProps as getInnerBlocksProps
} from "@wordpress/blocks";
import ButtonBlockAppender from "./button-block-appender";
import DefaultBlockAppender from "./default-block-appender";
import useNestedSettingsUpdate from "./use-nested-settings-update";
import useInnerBlockTemplateSync from "./use-inner-block-template-sync";
import useBlockContext from "./use-block-context";
import { BlockListItems } from "../block-list";
import { BlockContextProvider } from "../block-context";
import { useBlockEditContext } from "../block-edit/context";
import useBlockSync from "../provider/use-block-sync";
import { store as blockEditorStore } from "../../store";
import useBlockDropZone from "../use-block-drop-zone";
import { unlock } from "../../lock-unlock";
import { jsx } from "react/jsx-runtime";
var EMPTY_OBJECT = {};
function BlockContext({ children, clientId }) {
  const context = useBlockContext(clientId);
  return /* @__PURE__ */ jsx(BlockContextProvider, { value: context, children });
}
var BlockListItemsMemo = memo(BlockListItems);
function UncontrolledInnerBlocks(props) {
  const {
    clientId,
    allowedBlocks,
    prioritizedInserterBlocks,
    defaultBlock,
    directInsert,
    __experimentalDefaultBlock,
    __experimentalDirectInsert,
    template,
    templateLock,
    wrapperRef,
    templateInsertUpdatesSelection,
    __experimentalCaptureToolbars: captureToolbars,
    __experimentalAppenderTagName,
    renderAppender,
    orientation,
    placeholder,
    layout,
    name,
    blockType,
    parentLock,
    defaultLayout
  } = props;
  useNestedSettingsUpdate(
    clientId,
    parentLock,
    allowedBlocks,
    prioritizedInserterBlocks,
    defaultBlock,
    directInsert,
    __experimentalDefaultBlock,
    __experimentalDirectInsert,
    templateLock,
    captureToolbars,
    orientation,
    layout
  );
  useInnerBlockTemplateSync(
    clientId,
    template,
    templateLock,
    templateInsertUpdatesSelection
  );
  const defaultLayoutBlockSupport = getBlockSupport(name, "layout") || getBlockSupport(name, "__experimentalLayout") || EMPTY_OBJECT;
  const { allowSizingOnChildren = false } = defaultLayoutBlockSupport;
  const usedLayout = layout || defaultLayoutBlockSupport;
  const memoedLayout = useMemo(
    () => ({
      // Default layout will know about any content/wide size defined by the theme.
      ...defaultLayout,
      ...usedLayout,
      ...allowSizingOnChildren && {
        allowSizingOnChildren: true
      }
    }),
    [defaultLayout, usedLayout, allowSizingOnChildren]
  );
  const items = /* @__PURE__ */ jsx(
    BlockListItemsMemo,
    {
      rootClientId: clientId,
      renderAppender,
      __experimentalAppenderTagName,
      layout: memoedLayout,
      wrapperRef,
      placeholder
    }
  );
  if (!blockType?.providesContext || Object.keys(blockType.providesContext).length === 0) {
    return items;
  }
  return /* @__PURE__ */ jsx(BlockContext, { clientId, children: items });
}
function ControlledInnerBlocks(props) {
  useBlockSync(props);
  return /* @__PURE__ */ jsx(UncontrolledInnerBlocks, { ...props });
}
var ForwardedInnerBlocks = forwardRef((props, ref) => {
  const innerBlocksProps = useInnerBlocksProps({ ref }, props);
  return /* @__PURE__ */ jsx("div", { className: "block-editor-inner-blocks", children: /* @__PURE__ */ jsx("div", { ...innerBlocksProps }) });
});
function useInnerBlocksProps(props = {}, options = {}) {
  const {
    __unstableDisableLayoutClassNames,
    __unstableDisableDropZone,
    dropZoneElement
  } = options;
  const {
    clientId,
    layout = null,
    __unstableLayoutClassNames: layoutClassNames = ""
  } = useBlockEditContext();
  const selected = useSelect(
    (select) => {
      const {
        getBlockName,
        isZoomOut,
        getTemplateLock,
        getBlockRootClientId,
        getBlockEditingMode,
        getBlockSettings,
        getSectionRootClientId
      } = unlock(select(blockEditorStore));
      if (!clientId) {
        const sectionRootClientId = getSectionRootClientId();
        return {
          isDropZoneDisabled: isZoomOut() && sectionRootClientId !== ""
        };
      }
      const { hasBlockSupport, getBlockType } = select(blocksStore);
      const blockName = getBlockName(clientId);
      const blockEditingMode = getBlockEditingMode(clientId);
      const parentClientId2 = getBlockRootClientId(clientId);
      const [defaultLayout2] = getBlockSettings(clientId, "layout");
      let _isDropZoneDisabled = blockEditingMode === "disabled";
      if (isZoomOut()) {
        const sectionRootClientId = getSectionRootClientId();
        _isDropZoneDisabled = clientId !== sectionRootClientId;
      }
      return {
        __experimentalCaptureToolbars: hasBlockSupport(
          blockName,
          "__experimentalExposeControlsToChildren",
          false
        ),
        name: blockName,
        blockType: getBlockType(blockName),
        parentLock: getTemplateLock(parentClientId2),
        parentClientId: parentClientId2,
        isDropZoneDisabled: _isDropZoneDisabled,
        defaultLayout: defaultLayout2
      };
    },
    [clientId]
  );
  const {
    __experimentalCaptureToolbars,
    name,
    blockType,
    parentLock,
    parentClientId,
    isDropZoneDisabled,
    defaultLayout
  } = selected;
  const blockDropZoneRef = useBlockDropZone({
    dropZoneElement,
    rootClientId: clientId,
    parentClientId
  });
  const ref = useMergeRefs([
    props.ref,
    __unstableDisableDropZone || isDropZoneDisabled || layout?.isManualPlacement && window.__experimentalEnableGridInteractivity ? null : blockDropZoneRef
  ]);
  const innerBlocksProps = {
    __experimentalCaptureToolbars,
    layout,
    name,
    blockType,
    parentLock,
    defaultLayout,
    ...options
  };
  const InnerBlocks = innerBlocksProps.value && innerBlocksProps.onChange ? ControlledInnerBlocks : UncontrolledInnerBlocks;
  return {
    ...props,
    ref,
    className: clsx(
      props.className,
      "block-editor-block-list__layout",
      __unstableDisableLayoutClassNames ? "" : layoutClassNames
    ),
    children: clientId ? /* @__PURE__ */ jsx(InnerBlocks, { ...innerBlocksProps, clientId }) : /* @__PURE__ */ jsx(BlockListItems, { ...options })
  };
}
useInnerBlocksProps.save = getInnerBlocksProps;
ForwardedInnerBlocks.DefaultBlockAppender = DefaultBlockAppender;
ForwardedInnerBlocks.ButtonBlockAppender = ButtonBlockAppender;
ForwardedInnerBlocks.Content = () => useInnerBlocksProps.save().children;
var inner_blocks_default = ForwardedInnerBlocks;
export {
  inner_blocks_default as default,
  useInnerBlocksProps
};
//# sourceMappingURL=index.js.map
