"use client";
'use strict';

var ABQUS43J = require('./ABQUS43J.cjs');
var _3YLGPPWQ = require('./3YLGPPWQ.cjs');
var PBFD2E7P = require('../../../../../../@ariakit_core@0.4.15/node_modules/@ariakit/core/esm/__chunks/PBFD2E7P.cjs');
var BCALMBPZ = require('../../../../../../@ariakit_core@0.4.15/node_modules/@ariakit/core/esm/__chunks/BCALMBPZ.cjs');
var React = require('react');
var index = require('../../../../../../use-sync-external-store@1.5.0_react@19.1.1/node_modules/use-sync-external-store/shim/index.cjs');

function _interopNamespaceDefault(e) {
  var n = Object.create(null);
  if (e) {
    Object.keys(e).forEach(function (k) {
      if (k !== 'default') {
        var d = Object.getOwnPropertyDescriptor(e, k);
        Object.defineProperty(n, k, d.get ? d : {
          enumerable: true,
          get: function () { return e[k]; }
        });
      }
    });
  }
  n.default = e;
  return Object.freeze(n);
}

var React__namespace = /*#__PURE__*/_interopNamespaceDefault(React);

var { useSyncExternalStore } = index.default;
var noopSubscribe = () => () => {
};
function useStoreState(store, keyOrSelector = PBFD2E7P.identity) {
  const storeSubscribe = React__namespace.useCallback(
    (callback) => {
      if (!store) return noopSubscribe();
      return BCALMBPZ.subscribe(store, null, callback);
    },
    [store]
  );
  const getSnapshot = () => {
    const key = typeof keyOrSelector === "string" ? keyOrSelector : null;
    const selector = typeof keyOrSelector === "function" ? keyOrSelector : null;
    const state = store == null ? void 0 : store.getState();
    if (selector) return selector(state);
    if (!state) return;
    if (!key) return;
    if (!PBFD2E7P.hasOwnProperty(state, key)) return;
    return state[key];
  };
  return useSyncExternalStore(storeSubscribe, getSnapshot, getSnapshot);
}
function useStoreStateObject(store, object) {
  const objRef = React__namespace.useRef(
    {}
  );
  const storeSubscribe = React__namespace.useCallback(
    (callback) => {
      if (!store) return noopSubscribe();
      return BCALMBPZ.subscribe(store, null, callback);
    },
    [store]
  );
  const getSnapshot = () => {
    const state = store == null ? void 0 : store.getState();
    let updated = false;
    const obj = objRef.current;
    for (const prop in object) {
      const keyOrSelector = object[prop];
      if (typeof keyOrSelector === "function") {
        const value = keyOrSelector(state);
        if (value !== obj[prop]) {
          obj[prop] = value;
          updated = true;
        }
      }
      if (typeof keyOrSelector === "string") {
        if (!state) continue;
        if (!PBFD2E7P.hasOwnProperty(state, keyOrSelector)) continue;
        const value = state[keyOrSelector];
        if (value !== obj[prop]) {
          obj[prop] = value;
          updated = true;
        }
      }
    }
    if (updated) {
      objRef.current = _3YLGPPWQ.__spreadValues({}, obj);
    }
    return objRef.current;
  };
  return useSyncExternalStore(storeSubscribe, getSnapshot, getSnapshot);
}
function useStoreProps(store, props, key, setKey) {
  const value = PBFD2E7P.hasOwnProperty(props, key) ? props[key] : void 0;
  const setValue = setKey ? props[setKey] : void 0;
  const propsRef = ABQUS43J.useLiveRef({ value, setValue });
  ABQUS43J.useSafeLayoutEffect(() => {
    return BCALMBPZ.sync(store, [key], (state, prev) => {
      const { value: value2, setValue: setValue2 } = propsRef.current;
      if (!setValue2) return;
      if (state[key] === prev[key]) return;
      if (state[key] === value2) return;
      setValue2(state[key]);
    });
  }, [store, key]);
  ABQUS43J.useSafeLayoutEffect(() => {
    if (value === void 0) return;
    store.setState(key, value);
    return BCALMBPZ.batch(store, [key], () => {
      if (value === void 0) return;
      store.setState(key, value);
    });
  });
}
function useStore(createStore, props) {
  const [store, setStore] = React__namespace.useState(() => createStore(props));
  ABQUS43J.useSafeLayoutEffect(() => BCALMBPZ.init(store), [store]);
  const useState2 = React__namespace.useCallback(
    (keyOrSelector) => useStoreState(store, keyOrSelector),
    [store]
  );
  const memoizedStore = React__namespace.useMemo(
    () => _3YLGPPWQ.__spreadProps(_3YLGPPWQ.__spreadValues({}, store), { useState: useState2 }),
    [store, useState2]
  );
  const updateStore = ABQUS43J.useEvent(() => {
    setStore((store2) => createStore(_3YLGPPWQ.__spreadValues(_3YLGPPWQ.__spreadValues({}, props), store2.getState())));
  });
  return [memoizedStore, updateStore];
}

exports.useStore = useStore;
exports.useStoreProps = useStoreProps;
exports.useStoreState = useStoreState;
exports.useStoreStateObject = useStoreStateObject;
//# sourceMappingURL=YV4JVR4I.cjs.map
