"use strict";
const jsxRuntime = require("react/jsx-runtime");
const React = require("react");
const curry = require("lodash/curry.js");
const styles_lib_css_atoms_sprinkles_css_cjs = require("../css/atoms/sprinkles.css.cjs");
const sideEffects_lib_components_BraidProvider_BraidProvider_cjs = require("../../../side-effects/lib/components/BraidProvider/BraidProvider.cjs");
const styles_lib_components_private_Placeholder_Placeholder_cjs = require("../components/private/Placeholder/Placeholder.cjs");
const styles_lib_components_Autosuggest_Autosuggest_cjs = require("../components/Autosuggest/Autosuggest.cjs");
const _interopDefaultCompat = (e) => e && typeof e === "object" && "default" in e ? e : { default: e };
const curry__default = /* @__PURE__ */ _interopDefaultCompat(curry);
const unwrapValue = (value) => {
  let actualValue = value;
  if (typeof value === "object" && value !== null && "currentTarget" in value) {
    const { currentTarget } = value;
    actualValue = currentTarget.type === "checkbox" ? currentTarget.checked : currentTarget.value;
  }
  return actualValue;
};
const makeStoreConsumer = (defaultState, store, setStore) => {
  const setDefaultState = (key, value) => {
    defaultState.set(key, value);
  };
  const getState = (key) => store.get(key) ?? defaultState.get(key);
  const setState = curry__default.default(
    (key, value) => setStore(new Map(store.set(key, unwrapValue(value))))
  );
  const toggleState = (key) => setState(key, !getState(key));
  const resetState = (...keys) => {
    if (keys.length) {
      keys.forEach((key) => {
        store.delete(key);
      });
      setStore(new Map(store));
    } else {
      setStore(/* @__PURE__ */ new Map());
    }
  };
  return {
    setDefaultState,
    getState,
    setState,
    toggleState,
    resetState
  };
};
const PlayroomStateContext = React.createContext(null);
const PlayroomStateProvider = ({
  defaultState: defaultStateProp,
  children
}) => {
  const [fallbackDefaultState] = React.useState(() => /* @__PURE__ */ new Map());
  const defaultState = defaultStateProp ?? fallbackDefaultState;
  const state = React.useState(/* @__PURE__ */ new Map());
  const storeConsumer = React.useMemo(
    () => makeStoreConsumer(defaultState, ...state),
    [state, defaultState]
  );
  return /* @__PURE__ */ jsxRuntime.jsx(PlayroomStateContext.Provider, { value: storeConsumer, children });
};
const usePlayroomStore = () => {
  const storeConsumer = React.useContext(PlayroomStateContext);
  if (storeConsumer === null) {
    throw new Error("Must be within a PlayroomStateProvider");
  }
  return storeConsumer;
};
const noop = () => {
};
function useFallbackState(stateKey, value, onChange, defaultValue) {
  const playroomState = usePlayroomStore();
  const [internalStateValue, setInternalStateValue] = React.useState(defaultValue);
  const wrapChangeHandler = (handler) => (...args) => {
    if (value === void 0) {
      (stateKey ? playroomState.setState(stateKey) : setInternalStateValue)(
        unwrapValue(args[0])
      );
    }
    return (handler || noop)(...args);
  };
  const handleChange = wrapChangeHandler(onChange || noop);
  const resolvedValue = value ?? (stateKey ? playroomState.getState(stateKey) ?? defaultValue : internalStateValue);
  return [resolvedValue, handleChange];
}
const PlayroomLink = sideEffects_lib_components_BraidProvider_BraidProvider_cjs.makeLinkComponent(
  ({ href, onClick, ...restProps }, ref) => /* @__PURE__ */ jsxRuntime.jsx(
    "a",
    {
      ref,
      href: href ?? "#",
      onClick: (e) => {
        if (!href) {
          e.preventDefault();
        }
        if (onClick) {
          onClick(e);
        }
      },
      ...restProps
    }
  )
);
const ResponsiveReady = ({ children }) => {
  const responsiveReady = styles_lib_components_Autosuggest_Autosuggest_cjs.useResponsiveValue()({ mobile: true }) ?? false;
  return /* @__PURE__ */ jsxRuntime.jsx(jsxRuntime.Fragment, { children: responsiveReady ? children : null });
};
const FrameComponent = ({ theme, children }) => {
  React.useEffect(() => {
    let code = "";
    const darkTrigger = "braiddark";
    const lightTrigger = "braidlight";
    const longestTrigger = Math.max(lightTrigger.length, darkTrigger.length);
    const colorModeToggle = (ev) => {
      code += ev.key;
      if (code.substr(code.length - darkTrigger.length) === darkTrigger) {
        document.documentElement.classList.add(styles_lib_css_atoms_sprinkles_css_cjs.darkMode);
        code = "";
      }
      if (code.substr(code.length - lightTrigger.length) === lightTrigger) {
        document.documentElement.classList.remove(styles_lib_css_atoms_sprinkles_css_cjs.darkMode);
        code = "";
      }
      if (code.length > longestTrigger) {
        code = code.substr(code.length - longestTrigger);
      }
    };
    window.addEventListener("keydown", colorModeToggle);
    return () => {
      window.removeEventListener("keydown", colorModeToggle);
    };
  }, []);
  return /* @__PURE__ */ jsxRuntime.jsxs(React.Fragment, { children: [
    /* @__PURE__ */ jsxRuntime.jsx(
      "div",
      {
        dangerouslySetInnerHTML: {
          __html: theme.webFonts.map((font) => font.linkTag).join("")
        }
      }
    ),
    /* @__PURE__ */ jsxRuntime.jsx(PlayroomStateProvider, { children: /* @__PURE__ */ jsxRuntime.jsx(sideEffects_lib_components_BraidProvider_BraidProvider_cjs.BraidProvider, { theme, linkComponent: PlayroomLink, children: /* @__PURE__ */ jsxRuntime.jsx(styles_lib_components_private_Placeholder_Placeholder_cjs.ToastProvider, { children: /* @__PURE__ */ jsxRuntime.jsx(ResponsiveReady, { children }) }) }) })
  ] });
};
exports.FrameComponent = FrameComponent;
exports.useFallbackState = useFallbackState;
exports.usePlayroomStore = usePlayroomStore;
