'use client';
'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var style = require('@keystar/ui/style');
var core = require('@keystar/ui/core');
var navigation = require('next/navigation');
var React = require('react');
var UniversalNextLink = require('./UniversalNextLink-0467f79d.cjs.js');
var useRootColorScheme = require('./useRootColorScheme-47bb22c0.cjs.js');
var jsxRuntime = require('react/jsx-runtime');
require('next/link');
require('@keystar/ui/link');

style.cache.compat = true;
function NextRootProvider(props) {
  return /*#__PURE__*/jsxRuntime.jsx(useRootColorScheme.ColorSchemeProvider, {
    children: /*#__PURE__*/jsxRuntime.jsx(InnerProvider, {
      ...props
    })
  });
}
const insertedKeys = Object.keys(style.cache.inserted);
const prevInsert = style.cache.insert;
style.cache.insert = function () {
  const serialized = arguments.length <= 1 ? undefined : arguments[1];
  if (style.cache.inserted[serialized.name] === undefined) {
    insertedKeys.push(serialized.name);
  }
  return prevInsert(...arguments);
};
function InnerProvider(props) {
  let lastIndexRef = React.useRef(0);
  let {
    colorScheme
  } = useRootColorScheme.useRootColorScheme();
  navigation.useServerInsertedHTML(() => {
    const names = insertedKeys.slice(lastIndexRef.current);
    lastIndexRef.current = insertedKeys.length;
    if (names.length === 0) return null;
    let styles = '';
    for (const name of names) {
      styles += style.cache.inserted[name];
    }
    return /*#__PURE__*/jsxRuntime.jsx("style", {
      "data-emotion": `${style.cache.key} ${names.join(' ')}`,
      dangerouslySetInnerHTML: {
        __html: styles
      }
    }, Math.random().toString(36));
  });
  return /*#__PURE__*/jsxRuntime.jsx(core.VoussoirProvider, {
    ...props,
    UNSAFE_className: props.fontClassName,
    colorScheme: colorScheme,
    elementType: "html",
    linkComponent: UniversalNextLink.UniversalNextLink
  });
}

exports.NextRootProvider = NextRootProvider;
