// packages/block-library/src/tabs/edit.js
import {
  useBlockProps,
  useInnerBlocksProps,
  BlockContextProvider,
  store as blockEditorStore
} from "@wordpress/block-editor";
import { useSelect } from "@wordpress/data";
import { useMemo } from "@wordpress/element";
import Controls from "./controls.mjs";
import useTabListItemsSync from "./use-tab-list-items-sync.mjs";
import { jsx, jsxs } from "react/jsx-runtime";
var EMPTY_ARRAY = [];
var TABS_TEMPLATE = [["core/tab-list"], ["core/tab-panels"]];
function Edit({ clientId, attributes }) {
  const { anchor, activeTabIndex, editorActiveTabIndex } = attributes;
  const { tabPanels, tabListClientId } = useSelect(
    (select) => {
      const { getBlocks } = select(blockEditorStore);
      const innerBlocks = getBlocks(clientId);
      const tabPanelsBlock = innerBlocks.find(
        (block) => block.name === "core/tab-panels"
      );
      const tabList = innerBlocks.find(
        (block) => block.name === "core/tab-list"
      );
      return {
        tabPanels: tabPanelsBlock?.innerBlocks ?? EMPTY_ARRAY,
        tabListClientId: tabList?.clientId ?? null
      };
    },
    [clientId]
  );
  useTabListItemsSync({ tabPanels, tabListClientId });
  const contextValue = useMemo(() => {
    const tabList = tabPanels.map((tab, index) => ({
      id: tab.attributes.anchor || `tab-${index}`,
      label: tab.attributes.label || "",
      clientId: tab.clientId,
      index
    }));
    return {
      "core/tabs-list": tabList,
      "core/tabs-id": anchor,
      "core/tabs-activeTabIndex": activeTabIndex,
      "core/tabs-editorActiveTabIndex": editorActiveTabIndex
    };
  }, [tabPanels, anchor, activeTabIndex, editorActiveTabIndex]);
  const blockProps = useBlockProps();
  const innerBlockProps = useInnerBlocksProps(blockProps, {
    __experimentalCaptureToolbars: true,
    template: TABS_TEMPLATE,
    templateLock: "all",
    renderAppender: false
  });
  return /* @__PURE__ */ jsx(BlockContextProvider, { value: contextValue, children: /* @__PURE__ */ jsxs("div", { ...innerBlockProps, children: [
    /* @__PURE__ */ jsx(Controls, { clientId }),
    innerBlockProps.children
  ] }) });
}
var edit_default = Edit;
export {
  edit_default as default
};
//# sourceMappingURL=edit.mjs.map
