'use strict';

var JTLIIJ4U = require('../__chunks/JTLIIJ4U.cjs');
var _2CHYBBFH = require('../__chunks/2CHYBBFH.cjs');
var BCALMBPZ = require('../__chunks/BCALMBPZ.cjs');
var PBFD2E7P = require('../__chunks/PBFD2E7P.cjs');
var _3YLGPPWQ = require('../__chunks/3YLGPPWQ.cjs');

// src/menu/menu-store.ts
function createMenuStore(_a = {}) {
  var _b = _a, {
    combobox,
    parent,
    menubar
  } = _b, props = _3YLGPPWQ.__objRest(_b, [
    "combobox",
    "parent",
    "menubar"
  ]);
  const parentIsMenubar = !!menubar && !parent;
  const store = BCALMBPZ.mergeStore(
    props.store,
    BCALMBPZ.pick(parent, ["values"]),
    BCALMBPZ.omit(combobox, [
      "arrowElement",
      "anchorElement",
      "contentElement",
      "popoverElement",
      "disclosureElement"
    ])
  );
  BCALMBPZ.throwOnConflictingProps(props, store);
  const syncState = store.getState();
  const composite = _2CHYBBFH.createCompositeStore(_3YLGPPWQ.__spreadProps(_3YLGPPWQ.__spreadValues({}, props), {
    store,
    orientation: PBFD2E7P.defaultValue(
      props.orientation,
      syncState.orientation,
      "vertical"
    )
  }));
  const hovercard = JTLIIJ4U.createHovercardStore(_3YLGPPWQ.__spreadProps(_3YLGPPWQ.__spreadValues({}, props), {
    store,
    placement: PBFD2E7P.defaultValue(
      props.placement,
      syncState.placement,
      "bottom-start"
    ),
    timeout: PBFD2E7P.defaultValue(
      props.timeout,
      syncState.timeout,
      parentIsMenubar ? 0 : 150
    ),
    hideTimeout: PBFD2E7P.defaultValue(props.hideTimeout, syncState.hideTimeout, 0)
  }));
  const initialState = _3YLGPPWQ.__spreadProps(_3YLGPPWQ.__spreadValues(_3YLGPPWQ.__spreadValues({}, composite.getState()), hovercard.getState()), {
    initialFocus: PBFD2E7P.defaultValue(syncState.initialFocus, "container"),
    values: PBFD2E7P.defaultValue(
      props.values,
      syncState.values,
      props.defaultValues,
      {}
    )
  });
  const menu = BCALMBPZ.createStore(initialState, composite, hovercard, store);
  BCALMBPZ.setup(
    menu,
    () => BCALMBPZ.sync(menu, ["mounted"], (state) => {
      if (state.mounted) return;
      menu.setState("activeId", null);
    })
  );
  BCALMBPZ.setup(
    menu,
    () => BCALMBPZ.sync(parent, ["orientation"], (state) => {
      menu.setState(
        "placement",
        state.orientation === "vertical" ? "right-start" : "bottom-start"
      );
    })
  );
  return _3YLGPPWQ.__spreadProps(_3YLGPPWQ.__spreadValues(_3YLGPPWQ.__spreadValues(_3YLGPPWQ.__spreadValues({}, composite), hovercard), menu), {
    combobox,
    parent,
    menubar,
    hideAll: () => {
      hovercard.hide();
      parent == null ? void 0 : parent.hideAll();
    },
    setInitialFocus: (value) => menu.setState("initialFocus", value),
    setValues: (values) => menu.setState("values", values),
    setValue: (name, value) => {
      if (name === "__proto__") return;
      if (name === "constructor") return;
      if (Array.isArray(name)) return;
      menu.setState("values", (values) => {
        const prevValue = values[name];
        const nextValue = PBFD2E7P.applyState(value, prevValue);
        if (nextValue === prevValue) return values;
        return _3YLGPPWQ.__spreadProps(_3YLGPPWQ.__spreadValues({}, values), {
          [name]: nextValue !== void 0 && nextValue
        });
      });
    }
  });
}

exports.createMenuStore = createMenuStore;
//# sourceMappingURL=menu-store.cjs.map
