// src/js-tools/react-utils/hooks/useMemoObjectShallow.ts
import { useRef } from "react";
function isEqualShallow(obj1, obj2) {
  const obj1Keys = new Set(Object.keys(obj1));
  const obj2Keys = new Set(Object.keys(obj2));
  return obj1Keys.size === obj2Keys.size && Object.keys(obj1).every(
    (key) => obj2Keys.has(key) && obj1[key] === obj2[key]
  );
}
function useMemoObjectShallow(obj) {
  const objRef = useRef(obj);
  if (!objRef.current || !obj) {
    if (objRef.current !== obj) {
      objRef.current = obj;
    }
  } else {
    if (!isEqualShallow(objRef.current, obj)) {
      objRef.current = obj;
    }
  }
  return objRef.current;
}
var useMemoObjectShallow_default = useMemoObjectShallow;

// src/js-tools/react-utils/hooks/useUpdatingRef.ts
import { useRef as useRef2 } from "react";
function useUpdatingRef(value) {
  const ref = useRef2(value);
  ref.current = value;
  return ref;
}
var useUpdatingRef_default = useUpdatingRef;

// src/js-tools/react-utils/state/state-context/StateContextProvider.tsx
import {
  useCallback,
  useContext,
  useMemo,
  useState
} from "react";
import { jsx } from "react/jsx-runtime";
function InternalStateContextProvider({
  context,
  value,
  children
}) {
  const [prevState] = useContext(context);
  const [_internalState, setInternalState] = useState();
  const stableValue = useMemoObjectShallow_default(value);
  const stablePrevState = useMemoObjectShallow_default(prevState);
  const internalState = useMemoObjectShallow_default(_internalState);
  const prevStateRef = useUpdatingRef_default(stablePrevState);
  const valueRef = useUpdatingRef_default(stableValue);
  const setState = useCallback(
    (next) => {
      setInternalState((internalState2) => {
        const state2 = {
          ...prevStateRef.current,
          ...valueRef.current,
          ...internalState2
        };
        if (typeof next === "function") {
          return {
            ...internalState2,
            ...next(state2)
          };
        } else {
          return {
            ...internalState2,
            ...next
          };
        }
      });
    },
    [prevStateRef, valueRef, setInternalState]
  );
  const state = useMemo(
    () => ({ ...stablePrevState, ...stableValue, ...internalState }),
    [stablePrevState, stableValue, internalState]
  );
  return useMemo(
    () => /* @__PURE__ */ jsx(context.Provider, { value: [state, setState], children }),
    [context, children, state, setState]
  );
}
function StateContextProvider({
  contexts,
  values,
  children
}) {
  contexts = useMemoObjectShallow_default(contexts);
  values = useMemoObjectShallow_default(values);
  return useMemo(() => {
    let prev = children;
    for (let i = 0; i < contexts.length; i++) {
      const context = contexts[i];
      const value = values[i];
      prev = /* @__PURE__ */ jsx(InternalStateContextProvider, { context, value, children: prev });
    }
    return prev;
  }, [contexts, values, children]);
}
var StateContextProvider_default = StateContextProvider;

// src/js-tools/react-utils/state/state-context/createStateContext.ts
import { createContext } from "react";
function createStateContext() {
  return createContext([
    {},
    () => {
      throw new Error("Invalid setContext method.");
    }
  ]);
}
var createStateContext_default = createStateContext;

// src/js-tools/react-utils/hooks/useDeps.ts
import { useRef as useRef3 } from "react";
function useDeps(deps) {
  const prevDepsRef = useRef3(void 0);
  const seqRef = useRef3(0);
  const prevDeps = prevDepsRef.current;
  if (!deps || !prevDeps || deps.length !== prevDeps.length || deps.some((d, i) => d !== prevDeps[i])) {
    prevDepsRef.current = deps;
    seqRef.current++;
  }
  return seqRef.current;
}
var useDeps_default = useDeps;

export {
  useMemoObjectShallow_default,
  useUpdatingRef_default,
  StateContextProvider_default,
  createStateContext_default,
  useDeps_default
};
