import { useCallback as d, useState as C, useEffect as f, useRef as g, useMemo as R, useLayoutEffect as z } from "react";
import v from "../../../../../../_virtual/index.js";
var w = typeof window < "u" ? z : f;
function S(r, e, t, o) {
  const u = g(e);
  w(() => {
    u.current = e;
  }, [e]), f(() => {
    const a = window;
    if (!(a && a.addEventListener))
      return;
    const n = (s) => {
      u.current(s);
    };
    return a.addEventListener(r, n, o), () => {
      a.removeEventListener(r, n, o);
    };
  }, [r, t, o]);
}
function L(r) {
  const e = g(() => {
    throw new Error("Cannot call an event handler while rendering.");
  });
  return w(() => {
    e.current = r;
  }, [r]), d((...t) => {
    var o;
    return (o = e.current) == null ? void 0 : o.call(e, ...t);
  }, [e]);
}
var h = typeof window > "u";
function F(r, e, t = {}) {
  const { initializeWithValue: o = !0 } = t, u = d(
    (c) => t.serializer ? t.serializer(c) : JSON.stringify(c),
    [t]
  ), a = d(
    (c) => {
      if (t.deserializer)
        return t.deserializer(c);
      if (c === "undefined")
        return;
      const i = e instanceof Function ? e() : e;
      let m;
      try {
        m = JSON.parse(c);
      } catch (D) {
        return console.error("Error parsing JSON:", D), i;
      }
      return m;
    },
    [t, e]
  ), n = d(() => {
    const c = e instanceof Function ? e() : e;
    if (h)
      return c;
    try {
      const i = window.localStorage.getItem(r);
      return i ? a(i) : c;
    } catch (i) {
      return console.warn(`Error reading localStorage key “${r}”:`, i), c;
    }
  }, [e, r, a]), [s, l] = C(() => o ? n() : e instanceof Function ? e() : e), M = L((c) => {
    h && console.warn(
      `Tried setting localStorage key “${r}” even though environment is not a client`
    );
    try {
      const i = c instanceof Function ? c(n()) : c;
      window.localStorage.setItem(r, u(i)), l(i), window.dispatchEvent(new StorageEvent("local-storage", { key: r }));
    } catch (i) {
      console.warn(`Error setting localStorage key “${r}”:`, i);
    }
  }), b = L(() => {
    h && console.warn(
      `Tried removing localStorage key “${r}” even though environment is not a client`
    );
    const c = e instanceof Function ? e() : e;
    window.localStorage.removeItem(r), l(c), window.dispatchEvent(new StorageEvent("local-storage", { key: r }));
  });
  f(() => {
    l(n());
  }, [r]);
  const E = d(
    (c) => {
      c.key && c.key !== r || l(n());
    },
    [r, n]
  );
  return S("storage", E), S("local-storage", E), [s, M, b];
}
var O = typeof window > "u";
function I(r, {
  defaultValue: e = !1,
  initializeWithValue: t = !0
} = {}) {
  const o = (s) => O ? e : window.matchMedia(s).matches, [u, a] = C(() => t ? o(r) : e);
  function n() {
    a(o(r));
  }
  return w(() => {
    const s = window.matchMedia(r);
    return n(), s.addListener ? s.addListener(n) : s.addEventListener("change", n), () => {
      s.removeListener ? s.removeListener(n) : s.removeEventListener("change", n);
    };
  }, [r]), u;
}
var _ = "(prefers-color-scheme: dark)", k = "usehooks-ts-dark-mode";
function J(r = {}) {
  const {
    defaultValue: e,
    localStorageKey: t = k,
    initializeWithValue: o = !0
  } = r, u = I(_, {
    initializeWithValue: o,
    defaultValue: e
  }), [a, n] = F(
    t,
    e ?? u ?? !1,
    { initializeWithValue: o }
  );
  return w(() => {
    u !== a && n(u);
  }, [u]), {
    isDarkMode: a,
    toggle: () => {
      n((s) => !s);
    },
    enable: () => {
      n(!0);
    },
    disable: () => {
      n(!1);
    },
    set: (s) => {
      n(s);
    }
  };
}
function p(r) {
  const e = g(r);
  e.current = r, f(
    () => () => {
      e.current();
    },
    []
  );
}
function U(r, e = 500, t) {
  const o = g();
  p(() => {
    o.current && o.current.cancel();
  });
  const u = R(() => {
    const a = v(r, e, t), n = (...s) => a(...s);
    return n.cancel = () => {
      a.cancel();
    }, n.isPending = () => !!o.current, n.flush = () => a.flush(), n;
  }, [r, e, t]);
  return f(() => {
    o.current = v(r, e, t);
  }, [r, e, t]), u;
}
export {
  J as useDarkMode,
  U as useDebounceCallback,
  L as useEventCallback,
  S as useEventListener,
  w as useIsomorphicLayoutEffect,
  F as useLocalStorage,
  I as useMediaQuery,
  p as useUnmount
};
//# sourceMappingURL=index.js.map
