import { useEffect, useLayoutEffect, useRef, useMemo, useState, useCallback } from "react";
import "../../../../@dnd-kit_state@0.1.21/node_modules/@dnd-kit/state/dist/index.js";
import { flushSync } from "react-dom";
import { currentValue } from "./utilities.js";
import { effect as E, untracked as n } from "../../../../@preact_signals-core@1.12.0/node_modules/@preact/signals-core/dist/signals-core.module.js";
var canUseDOM = typeof window !== "undefined" && typeof window.document !== "undefined" && typeof window.document.createElement !== "undefined";
var useIsomorphicLayoutEffect = canUseDOM ? useLayoutEffect : useEffect;
function useForceUpdate() {
  const setState = useState(0)[1];
  return useCallback(() => {
    setState((value) => value + 1);
  }, [setState]);
}
function useDeepSignal(target, synchronous) {
  const tracked = useRef(/* @__PURE__ */ new Map());
  const forceUpdate = useForceUpdate();
  useIsomorphicLayoutEffect(() => {
    if (!target) {
      tracked.current.clear();
      return;
    }
    return E(() => {
      var _a;
      let stale = false;
      let sync = false;
      for (const entry of tracked.current) {
        const [key] = entry;
        const value = n(() => entry[1]);
        const latestValue = target[key];
        if (value !== latestValue) {
          stale = true;
          tracked.current.set(key, latestValue);
          sync = (_a = synchronous == null ? void 0 : synchronous(key, value, latestValue)) != null ? _a : false;
        }
      }
      if (stale) {
        sync ? flushSync(forceUpdate) : forceUpdate();
      }
    });
  }, [target]);
  return useMemo(
    () => target ? new Proxy(target, {
      get(target2, key) {
        const value = target2[key];
        tracked.current.set(key, value);
        return value;
      }
    }) : target,
    [target]
  );
}
function useImmediateEffect(callback, _) {
  callback();
}
function useLatest(value) {
  const valueRef = useRef(value);
  useIsomorphicLayoutEffect(() => {
    valueRef.current = value;
  }, [value]);
  return valueRef;
}
function useOnValueChange(value, onChange, effect3 = useEffect, compare = Object.is) {
  const tracked = useRef(value);
  effect3(() => {
    const oldValue = tracked.current;
    if (!compare(value, oldValue)) {
      tracked.current = value;
      onChange(value, oldValue);
    }
  }, [onChange, value]);
}
function useOnElementChange(value, onChange) {
  const previous = useRef(currentValue(value));
  useIsomorphicLayoutEffect(() => {
    const current = currentValue(value);
    if (current !== previous.current) {
      previous.current = current;
      onChange(current);
    }
  });
}
export {
  useDeepSignal,
  useImmediateEffect,
  useIsomorphicLayoutEffect,
  useLatest,
  useOnElementChange,
  useOnValueChange
};
//# sourceMappingURL=hooks.js.map
