"use client";
'use strict';

var React = require('react');
var jsxRuntime = require('react/jsx-runtime');

function _interopNamespace(e) {
  if (e && e.__esModule) return e;
  var n = Object.create(null);
  if (e) {
    Object.keys(e).forEach(function (k) {
      if (k !== 'default') {
        var d = Object.getOwnPropertyDescriptor(e, k);
        Object.defineProperty(n, k, d.get ? d : {
          enumerable: true,
          get: function () { return e[k]; }
        });
      }
    });
  }
  n.default = e;
  return Object.freeze(n);
}

var React__namespace = /*#__PURE__*/_interopNamespace(React);

var LEGACY_ENABLED_KEY = "toolbox-grooved-panel-enabled";
var STORAGE_KEY = "toolbox-grooved-panel";
var GroovedPanelPreferenceContext = React__namespace.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__namespace.useState(true);
  const [hydrated, setHydrated] = React__namespace.useState(false);
  React__namespace.useLayoutEffect(() => {
    const { enabled } = readStored();
    setGroovedPanelEnabledState(enabled);
    setHydrated(true);
  }, []);
  React__namespace.useEffect(() => {
    if (!hydrated) return;
    writeStored(groovedPanelEnabled);
  }, [hydrated, groovedPanelEnabled]);
  React__namespace.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__namespace.useCallback((value2) => {
    setGroovedPanelEnabledState(value2);
  }, []);
  const value = React__namespace.useMemo(
    () => ({
      groovedPanelEnabled,
      setGroovedPanelEnabled
    }),
    [groovedPanelEnabled, setGroovedPanelEnabled]
  );
  return /* @__PURE__ */ jsxRuntime.jsx(GroovedPanelPreferenceContext.Provider, { value, children });
}
function useGroovedPanelPreference() {
  const ctx = React__namespace.useContext(GroovedPanelPreferenceContext);
  if (ctx == null) {
    throw new Error(
      "useGroovedPanelPreference must be used within GroovedPanelPreferenceProvider"
    );
  }
  return ctx;
}

exports.GroovedPanelPreferenceProvider = GroovedPanelPreferenceProvider;
exports.useGroovedPanelPreference = useGroovedPanelPreference;
//# sourceMappingURL=grooved-panel-preference.cjs.map
//# sourceMappingURL=grooved-panel-preference.cjs.map