"use strict";
const jsxRuntime = require("react/jsx-runtime");
const assert = require("assert");
const dedent = require("dedent");
const React = require("react");
const sideEffects_lib_css_reset_resetTracker_cjs = require("../../css/reset/resetTracker.cjs");
const styles_lib_components_private_hideFocusRings_hideFocusRingsDataAttribute_cjs = require("../../../../styles/lib/components/private/hideFocusRings/hideFocusRingsDataAttribute.cjs");
const styles_lib_css_breakpoints_cjs = require("../../../../styles/lib/css/breakpoints.cjs");
const styles_lib_css_atoms_sprinkles_css_cjs = require("../../../../styles/lib/css/atoms/sprinkles.css.cjs");
const styles_lib_components_BraidProvider_VanillaThemeContainer_cjs = require("../../../../styles/lib/components/BraidProvider/VanillaThemeContainer.cjs");
const _interopDefaultCompat = (e) => e && typeof e === "object" && "default" in e ? e : { default: e };
const assert__default = /* @__PURE__ */ _interopDefaultCompat(assert);
const dedent__default = /* @__PURE__ */ _interopDefaultCompat(dedent);
const hideFocusRings = () => document.body.setAttribute(styles_lib_components_private_hideFocusRings_hideFocusRingsDataAttribute_cjs.hideFocusRingsDataAttribute, "true");
const showFocusRings = () => document.body.removeAttribute(styles_lib_components_private_hideFocusRings_hideFocusRingsDataAttribute_cjs.hideFocusRingsDataAttribute);
const useHideFocusRings = (enabled = true) => {
  React.useEffect(() => {
    if (enabled) {
      window.addEventListener("keydown", showFocusRings);
      window.addEventListener("mousemove", hideFocusRings);
      return () => {
        window.removeEventListener("keydown", showFocusRings);
        window.removeEventListener("mousemove", hideFocusRings);
      };
    }
  }, [enabled]);
};
const BraidTestProviderContext = React.createContext(false);
const minWidthQuery = (breakpoint) => window.matchMedia(`(min-width: ${breakpoint}px)`);
const getCurrentBreakpoint = (tabletQuery, desktopQuery, wideQuery) => {
  if (wideQuery.matches) {
    return "wide";
  }
  if (desktopQuery.matches) {
    return "desktop";
  }
  if (tabletQuery.matches) {
    return "tablet";
  }
  return "mobile";
};
const breakpointContext = React.createContext(null);
function BreakpointProvider({ children }) {
  const { tablet, desktop, wide } = styles_lib_css_breakpoints_cjs.breakpoints;
  const [state, setState] = React.useState(null);
  React.useEffect(() => {
    let mounted = true;
    const tabletQuery = minWidthQuery(tablet);
    const desktopQuery = minWidthQuery(desktop);
    const wideQuery = minWidthQuery(wide);
    const onChange = () => {
      if (!mounted) {
        return;
      }
      const newBreakPoint = getCurrentBreakpoint(
        tabletQuery,
        desktopQuery,
        wideQuery
      );
      if (newBreakPoint !== state) {
        setState(newBreakPoint);
      }
    };
    tabletQuery.addListener(onChange);
    desktopQuery.addListener(onChange);
    wideQuery.addListener(onChange);
    onChange();
    return () => {
      mounted = false;
      tabletQuery.removeListener(onChange);
      desktopQuery.removeListener(onChange);
      wideQuery.removeListener(onChange);
    };
  }, [tablet, desktop, wide, state]);
  return /* @__PURE__ */ jsxRuntime.jsx(breakpointContext.Provider, { value: state, children });
}
const BraidThemeContext = React.createContext(null);
const useBraidTheme = () => {
  const braidTheme = React.useContext(BraidThemeContext);
  if (braidTheme === null) {
    throw new Error("No Braid theme available on context");
  }
  return braidTheme;
};
if (process.env.NODE_ENV === "development") {
  sideEffects_lib_css_reset_resetTracker_cjs.ensureResetImported();
}
const makeLinkComponent = (render) => ({ __forwardRef__: React.forwardRef(render) });
const DefaultLinkComponent = makeLinkComponent((props, ref) => /* @__PURE__ */ jsxRuntime.jsx("a", { ref, ...props }));
const LinkComponentContext = React.createContext(DefaultLinkComponent);
const useLinkComponent = (ref) => {
  const linkComponent = React.useContext(LinkComponentContext);
  assert__default.default(
    !ref || "__forwardRef__" in linkComponent,
    dedent__default.default`
      You're passing a ref to a Braid link, but your app is providing a custom link component to 'BraidProvider' that doesn't appear to support refs.

      To fix this, you need to use Braid's 'makeLinkComponent' helper function when creating your custom link component. This ensures that refs are forwarded correctly, and allows us to silence this error message.

      For more information and an example implementation, check out the documentation for 'BraidProvider': https://seek-oss.github.io/braid-design-system/components/BraidProvider
    `
  );
  if ("__forwardRef__" in linkComponent) {
    return linkComponent.__forwardRef__;
  }
  return linkComponent;
};
const BraidProvider = ({
  theme,
  styleBody = true,
  linkComponent,
  children
}) => {
  const alreadyInBraidProvider = Boolean(React.useContext(BraidThemeContext));
  const inTestProvider = React.useContext(BraidTestProviderContext);
  const linkComponentFromContext = React.useContext(LinkComponentContext);
  useHideFocusRings(!(alreadyInBraidProvider || inTestProvider));
  assert__default.default(
    inTestProvider || typeof navigator === "undefined" || navigator.userAgent === void 0 || navigator.userAgent.indexOf("jsdom") === -1,
    `Rendering 'BraidProvider' in Jest is not supported as it expects a browser environment. Please switch to 'BraidTestProvider'. See the docs for more info: https://seek-oss.github.io/braid-design-system/components/BraidTestProvider`
  );
  return /* @__PURE__ */ jsxRuntime.jsxs(BraidThemeContext.Provider, { value: theme, children: [
    styleBody ? /* @__PURE__ */ jsxRuntime.jsx("style", { type: "text/css", children: `
            html,body{margin:0;padding:0;background:${theme.background.lightMode}}
            html.${styles_lib_css_atoms_sprinkles_css_cjs.darkMode},html.${styles_lib_css_atoms_sprinkles_css_cjs.darkMode} body{color-scheme:dark;background:${theme.background.darkMode}}
          ` }) : null,
    /* @__PURE__ */ jsxRuntime.jsx(
      styles_lib_components_BraidProvider_VanillaThemeContainer_cjs.VanillaThemeContainer,
      {
        theme: theme.vanillaTheme,
        setDefaultTextTones: !alreadyInBraidProvider,
        children: /* @__PURE__ */ jsxRuntime.jsx(
          LinkComponentContext.Provider,
          {
            value: linkComponent || linkComponentFromContext,
            children: alreadyInBraidProvider || inTestProvider ? children : /* @__PURE__ */ jsxRuntime.jsx(BreakpointProvider, { children })
          }
        )
      }
    )
  ] });
};
exports.BraidProvider = BraidProvider;
exports.BraidTestProviderContext = BraidTestProviderContext;
exports.breakpointContext = breakpointContext;
exports.makeLinkComponent = makeLinkComponent;
exports.useBraidTheme = useBraidTheme;
exports.useLinkComponent = useLinkComponent;
