'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 jsxRuntime = require('react/jsx-runtime');
require('next/link');
require('@keystar/ui/link');

style.cache.compat = true;
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);
};

/** @deprecated — use `NextRootProvider` instead. */
const RootVoussoirProvider = /*#__PURE__*/React.forwardRef(function RootVoussoirProvider(props, ref) {
  const lastIndexRef = React.useRef(0);
  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}${props.UNSAFE_className ? ` ${props.UNSAFE_className}` : ''}`,
    ref: ref,
    elementType: "html",
    linkComponent: UniversalNextLink.UniversalNextLink
  });
});

exports.RootVoussoirProvider = RootVoussoirProvider;
