"use client";
import * as React from 'react';
import { jsx } from 'react/jsx-runtime';

var LEGACY_ENABLED_KEY = "toolbox-grooved-panel-enabled";
var STORAGE_KEY = "toolbox-grooved-panel";
var GroovedPanelPreferenceContext = React.createContext(null);
function readStored() {
  if (typeof window === "undefined") {
    return { enabled: true };
  }
  try {
    const raw = window.localStorage.getItem(STORAGE_KEY);
    if (raw) {
      const parsed = JSON.parse(raw);
      return {
        enabled: parsed.enabled !== false
      };
    }
    const legacy = window.localStorage.getItem(LEGACY_ENABLED_KEY);
    return {
      enabled: legacy !== "false"
    };
  } catch (e) {
    return { enabled: true };
  }
}
function writeStored(enabled) {
  try {
    const payload = { enabled };
    window.localStorage.setItem(STORAGE_KEY, JSON.stringify(payload));
  } catch (e) {
  }
}
function GroovedPanelPreferenceProvider({
  children
}) {
  const [groovedPanelEnabled, setGroovedPanelEnabledState] = React.useState(true);
  const [hydrated, setHydrated] = React.useState(false);
  React.useLayoutEffect(() => {
    const { enabled } = readStored();
    setGroovedPanelEnabledState(enabled);
    setHydrated(true);
  }, []);
  React.useEffect(() => {
    if (!hydrated) return;
    writeStored(groovedPanelEnabled);
  }, [hydrated, groovedPanelEnabled]);
  React.useEffect(() => {
    const onStorage = (event) => {
      if (event.key !== STORAGE_KEY || event.newValue == null) return;
      try {
        const parsed = JSON.parse(event.newValue);
        setGroovedPanelEnabledState(parsed.enabled !== false);
      } catch (e) {
      }
    };
    window.addEventListener("storage", onStorage);
    return () => window.removeEventListener("storage", onStorage);
  }, []);
  const setGroovedPanelEnabled = React.useCallback((value2) => {
    setGroovedPanelEnabledState(value2);
  }, []);
  const value = React.useMemo(
    () => ({
      groovedPanelEnabled,
      setGroovedPanelEnabled
    }),
    [groovedPanelEnabled, setGroovedPanelEnabled]
  );
  return /* @__PURE__ */ jsx(GroovedPanelPreferenceContext.Provider, { value, children });
}
function useGroovedPanelPreference() {
  const ctx = React.useContext(GroovedPanelPreferenceContext);
  if (ctx == null) {
    throw new Error(
      "useGroovedPanelPreference must be used within GroovedPanelPreferenceProvider"
    );
  }
  return ctx;
}

export { GroovedPanelPreferenceProvider, useGroovedPanelPreference };
//# sourceMappingURL=grooved-panel-preference.js.map
//# sourceMappingURL=grooved-panel-preference.js.map