import { useRef as R, useEffect as D } from "react";
import { isNil as z, isEqual as v } from "es-toolkit";
import { toQueryString as T, deserializeValue as M, serializeValue as A } from "../utils/routeUtils.js";
import E from "./useAfterMount.js";
import U from "./useLatest.js";
const C = (r) => {
  const e = r.replace(/^\?/, "");
  return new URLSearchParams(e).toString();
}, L = (r, e) => r.customDeserialize ? r.customDeserialize(e) : M(e, r.type ?? "string"), N = (r, e) => r ?? e, I = (r) => {
  const { valueToApply: e, currentStoreValue: t, urlRawValue: a, defaultValue: d } = r;
  if (typeof e > "u" || v(e, t))
    return !1;
  const l = z(a), n = v(t, d);
  return !(l && n);
}, O = (r, e, t) => t ? null : r.customSerialize ? r.customSerialize(e) : A(e, r.type ?? "string"), x = (r) => {
  const { nextParams: e, key: t, serializedValue: a } = r;
  return z(a) ? e.has(t) ? (e.delete(t), !0) : !1 : (e.set(t, a), !0);
}, K = (r, e) => {
  if (z(e))
    return !0;
  const t = L(r, e), a = N(t, r.defaultValue);
  return v(a, r.defaultValue);
}, F = ({
  config: r,
  searchString: e,
  storeValues: t,
  navigationType: a,
  onUpdateStore: d,
  lastWrittenNormalizedSearchRef: l,
  isRestoringRef: n,
  hasCheckedStorageRef: g
}) => {
  const h = U(r), p = U(t), y = U(d);
  D(() => {
    if (!g.current)
      return;
    const s = C(e);
    if (l.current === s && (l.current = null), n.current)
      return;
    const P = new URLSearchParams(e);
    h.current.forEach((c) => {
      const u = P.get(c.key), i = L(c, u), f = N(i, c.defaultValue), S = p.current[c.key];
      if (I({
        valueToApply: f,
        currentStoreValue: S,
        urlRawValue: u,
        defaultValue: c.defaultValue
      })) {
        if (z(u) && a !== "POP")
          return;
        y.current(c.action(f));
      }
    });
  }, [e, l, n, g]);
}, q = (r) => {
  const {
    config: e,
    searchString: t,
    storeValues: a,
    onNavigate: d,
    lastWrittenNormalizedSearchRef: l,
    localStorageKey: n,
    navigationType: g
  } = r, h = U(d), p = R(C(t));
  E(() => {
    const y = C(t), s = p.current !== y;
    if (p.current = y, g === "POP" && s)
      return;
    const c = new URLSearchParams(t), u = new URLSearchParams(t);
    let i = !1, f = !1, S = !0;
    if (e.forEach((o) => {
      const V = a[o.key], m = c.get(o.key), k = v(V, o.defaultValue);
      k || (S = !1);
      const w = O(o, V, k);
      w !== m && (i = x({ nextParams: u, key: o.key, serializedValue: w }) || i, ((o.history ?? "push") === "push" || !k && K(o, m)) && (f = !0));
    }), i) {
      const o = T(u, e), V = u.toString();
      l.current = V, n && (S && o === "" ? localStorage.removeItem(n) : localStorage.setItem(n, o));
      const m = f ? "push" : "replace";
      h.current(o, m);
    }
  }, [a, t, e, n, g]);
}, Q = (r) => {
  const {
    config: e,
    searchString: t,
    localStorageKey: a,
    onUpdateStore: d,
    onNavigate: l,
    isRestoringRef: n,
    lastWrittenNormalizedSearchRef: g,
    hasCheckedStorageRef: h
  } = r;
  D(() => {
    if (!a) {
      h.current = !0;
      return;
    }
    const p = new URLSearchParams(t);
    if (!e.some((s) => p.has(s.key))) {
      const s = localStorage.getItem(a);
      if (s) {
        n.current = !0;
        const P = new URLSearchParams(s);
        e.forEach((i) => {
          const f = P.get(i.key), S = L(i, f);
          S !== null && typeof S < "u" && d(i.action(S));
        });
        const c = s.replace(/^\?/, ""), u = t.replace(/^\?/, "");
        c !== u && (g.current = new URLSearchParams(s).toString(), l(s, "replace")), setTimeout(() => {
          n.current = !1;
        }, 0);
      }
    }
    h.current = !0;
  }, [a]);
}, J = (r) => {
  const e = R(null), t = R(!1), a = R(!r.localStorageKey);
  Q({
    ...r,
    lastWrittenNormalizedSearchRef: e,
    isRestoringRef: t,
    hasCheckedStorageRef: a
  }), F({
    ...r,
    lastWrittenNormalizedSearchRef: e,
    isRestoringRef: t,
    hasCheckedStorageRef: a
  }), q({
    ...r,
    lastWrittenNormalizedSearchRef: e
  });
};
export {
  J as default
};
//# sourceMappingURL=useUrlState.js.map
