'use strict';

var QAGXQEUG = require('../__chunks/QAGXQEUG.cjs');
var ME2CUF3F = require('../__chunks/ME2CUF3F.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/combobox/combobox-store.ts
var isTouchSafari = QAGXQEUG.isSafari() && QAGXQEUG.isTouchDevice();
function createComboboxStore(_a = {}) {
  var _b = _a, {
    tag
  } = _b, props = _3YLGPPWQ.__objRest(_b, [
    "tag"
  ]);
  const store = BCALMBPZ.mergeStore(props.store, BCALMBPZ.pick(tag, ["value", "rtl"]));
  BCALMBPZ.throwOnConflictingProps(props, store);
  const tagState = tag == null ? void 0 : tag.getState();
  const syncState = store == null ? void 0 : store.getState();
  const activeId = PBFD2E7P.defaultValue(
    props.activeId,
    syncState == null ? void 0 : syncState.activeId,
    props.defaultActiveId,
    null
  );
  const composite = _2CHYBBFH.createCompositeStore(_3YLGPPWQ.__spreadProps(_3YLGPPWQ.__spreadValues({}, props), {
    activeId,
    includesBaseElement: PBFD2E7P.defaultValue(
      props.includesBaseElement,
      syncState == null ? void 0 : syncState.includesBaseElement,
      true
    ),
    orientation: PBFD2E7P.defaultValue(
      props.orientation,
      syncState == null ? void 0 : syncState.orientation,
      "vertical"
    ),
    focusLoop: PBFD2E7P.defaultValue(props.focusLoop, syncState == null ? void 0 : syncState.focusLoop, true),
    focusWrap: PBFD2E7P.defaultValue(props.focusWrap, syncState == null ? void 0 : syncState.focusWrap, true),
    virtualFocus: PBFD2E7P.defaultValue(
      props.virtualFocus,
      syncState == null ? void 0 : syncState.virtualFocus,
      true
    )
  }));
  const popover = ME2CUF3F.createPopoverStore(_3YLGPPWQ.__spreadProps(_3YLGPPWQ.__spreadValues({}, props), {
    placement: PBFD2E7P.defaultValue(
      props.placement,
      syncState == null ? void 0 : syncState.placement,
      "bottom-start"
    )
  }));
  const value = PBFD2E7P.defaultValue(
    props.value,
    syncState == null ? void 0 : syncState.value,
    props.defaultValue,
    ""
  );
  const selectedValue = PBFD2E7P.defaultValue(
    props.selectedValue,
    syncState == null ? void 0 : syncState.selectedValue,
    tagState == null ? void 0 : tagState.values,
    props.defaultSelectedValue,
    ""
  );
  const multiSelectable = Array.isArray(selectedValue);
  const initialState = _3YLGPPWQ.__spreadProps(_3YLGPPWQ.__spreadValues(_3YLGPPWQ.__spreadValues({}, composite.getState()), popover.getState()), {
    value,
    selectedValue,
    resetValueOnSelect: PBFD2E7P.defaultValue(
      props.resetValueOnSelect,
      syncState == null ? void 0 : syncState.resetValueOnSelect,
      multiSelectable
    ),
    resetValueOnHide: PBFD2E7P.defaultValue(
      props.resetValueOnHide,
      syncState == null ? void 0 : syncState.resetValueOnHide,
      multiSelectable && !tag
    ),
    activeValue: syncState == null ? void 0 : syncState.activeValue
  });
  const combobox = BCALMBPZ.createStore(initialState, composite, popover, store);
  if (isTouchSafari) {
    BCALMBPZ.setup(
      combobox,
      () => BCALMBPZ.sync(combobox, ["virtualFocus"], () => {
        combobox.setState("virtualFocus", false);
      })
    );
  }
  BCALMBPZ.setup(combobox, () => {
    if (!tag) return;
    return PBFD2E7P.chain(
      BCALMBPZ.sync(combobox, ["selectedValue"], (state) => {
        if (!Array.isArray(state.selectedValue)) return;
        tag.setValues(state.selectedValue);
      }),
      BCALMBPZ.sync(tag, ["values"], (state) => {
        combobox.setState("selectedValue", state.values);
      })
    );
  });
  BCALMBPZ.setup(
    combobox,
    () => BCALMBPZ.sync(combobox, ["resetValueOnHide", "mounted"], (state) => {
      if (!state.resetValueOnHide) return;
      if (state.mounted) return;
      combobox.setState("value", value);
    })
  );
  BCALMBPZ.setup(
    combobox,
    () => BCALMBPZ.sync(combobox, ["open"], (state) => {
      if (state.open) return;
      combobox.setState("activeId", activeId);
      combobox.setState("moves", 0);
    })
  );
  BCALMBPZ.setup(
    combobox,
    () => BCALMBPZ.sync(combobox, ["moves", "activeId"], (state, prevState) => {
      if (state.moves === prevState.moves) {
        combobox.setState("activeValue", void 0);
      }
    })
  );
  BCALMBPZ.setup(
    combobox,
    () => BCALMBPZ.batch(combobox, ["moves", "renderedItems"], (state, prev) => {
      if (state.moves === prev.moves) return;
      const { activeId: activeId2 } = combobox.getState();
      const activeItem = composite.item(activeId2);
      combobox.setState("activeValue", activeItem == null ? void 0 : activeItem.value);
    })
  );
  return _3YLGPPWQ.__spreadProps(_3YLGPPWQ.__spreadValues(_3YLGPPWQ.__spreadValues(_3YLGPPWQ.__spreadValues({}, popover), composite), combobox), {
    tag,
    setValue: (value2) => combobox.setState("value", value2),
    resetValue: () => combobox.setState("value", initialState.value),
    setSelectedValue: (selectedValue2) => combobox.setState("selectedValue", selectedValue2)
  });
}

exports.createComboboxStore = createComboboxStore;
//# sourceMappingURL=combobox-store.cjs.map
