/**
 * `createMenubar` — headless primitive for the Menubar compound.
 *
 * Owns:
 *  - `orientation` — horizontal or vertical.
 *  - `modal` — modal mode flag.
 *  - `disabled` — whole-menubar disabled state.
 *  - `loopFocus` — whether arrow-key navigation wraps.
 *  - `hasSubmenuOpen` — whether any child menu is currently open.
 *  - `registerOpenMenu` / `closeAll` — single-open coordinator.
 *
 * DOM concerns (FloatingTree, FloatingNode, role="menubar" element, keyboard
 * handler) stay in `<Menubar>`.
 */
import { createContext, createMemo, createSignal, useContext } from "solid-js";
import { readOption } from "../internals/maybe-accessor.jsx";
import { SNAPSHOT_KEYS, snapshotKeys } from "../internals/observability/snapshot.jsx";

// ---------------------------------------------------------------------------
// Public types
// ---------------------------------------------------------------------------

// ---------------------------------------------------------------------------
// Context
// ---------------------------------------------------------------------------

const MenubarApiContext = createContext(null);

/**
 * Read the nearest {@link MenubarApi}. Throws unless `optional` is true.
 */

export function useMenubar(optional) {
  const ctx = useContext(MenubarApiContext);
  if (ctx == null && !optional) {
    throw new Error("Base UI: useMenubar() must be called inside a <mb.Provider>.");
  }
  return ctx;
}

// ---------------------------------------------------------------------------
// Primitive
// ---------------------------------------------------------------------------

/**
 * Headless primitive for the Menubar compound. Owns logical state — orientation,
 * modal, disabled, loopFocus, hasSubmenuOpen, and the single-open coordinator.
 * DOM concerns (FloatingTree, role="menubar" element, keyboard handler) live
 * in `<Menubar>`.
 */
export function createMenubar(options = {}) {
  const orientation = createMemo(() => readOption(options.orientation) ?? "horizontal", {
    name: "menubar.orientation"
  });
  const modal = createMemo(() => readOption(options.modal) ?? true, {
    name: "menubar.modal"
  });
  const disabled = createMemo(() => Boolean(readOption(options.disabled)), {
    name: "menubar.disabled"
  });
  const loopFocus = createMemo(() => readOption(options.loopFocus) ?? true, {
    name: "menubar.loopFocus"
  });
  const [hasSubmenuOpen, setHasSubmenuOpen] = createSignal(false, {
    ownedWrite: true,
    name: "menubar.hasSubmenuOpen"
  });

  // Single-open coordinator — only one child menu open at a time.
  let currentCloser = null;
  const registerOpenMenu = closer => {
    if (currentCloser && currentCloser !== closer) {
      const prev = currentCloser;
      currentCloser = null;
      prev();
    }
    currentCloser = closer;
    setHasSubmenuOpen(true);
    return () => {
      // Only the currently-open menu clears the flag: a sibling that
      // closes because another one opened must not reset it.
      if (currentCloser === closer) {
        currentCloser = null;
        setHasSubmenuOpen(false);
      }
    };
  };
  const closeAll = () => {
    if (currentCloser) {
      const prev = currentCloser;
      currentCloser = null;
      setHasSubmenuOpen(false);
      prev();
    }
  };
  const api = {
    orientation,
    modal,
    disabled,
    loopFocus,
    hasSubmenuOpen,
    registerOpenMenu,
    closeAll,
    setHasSubmenuOpen: open => {
      setHasSubmenuOpen(open);
    },
    Provider: props => <MenubarApiContext value={api}>{props.children}</MenubarApiContext>
  };
  api[SNAPSHOT_KEYS] = MENUBAR_SNAPSHOT_KEYS;
  return api;
}
const MENUBAR_SNAPSHOT_KEYS = snapshotKeys("orientation", "modal", "disabled", "loopFocus", "hasSubmenuOpen");
//# sourceMappingURL=create-menubar.jsx.map
