"use client";
import * as React from "react";
import { useRef } from "react";
import { serializeStyles } from "@emotion/serialize";
import { insertStyles } from "@emotion/utils";
import { useCache } from "./root-style-registry";
const isServer = typeof window === "undefined";
const useInsertionEffectSpecifier = "useInsertionEffect";
const useInsertionEffect = React[useInsertionEffectSpecifier] ?? React.useLayoutEffect;
function useGlobalStyle(...args) {
  const cache = useCache();
  const serialized = serializeStyles(args, cache.registered);
  if (isServer) {
    if (cache.inserted[serialized.name] === void 0) {
      cache.insert("", serialized, cache.sheet, true);
    }
  }
  const sheetRef = useRef(null);
  useInsertionEffect(() => {
    const key = `${cache.key}-global`;
    const cacheSheet = cache.sheet;
    const sheet = new cacheSheet.constructor({
      key,
      nonce: cacheSheet.nonce,
      container: cacheSheet.container,
      speedy: cacheSheet.isSpeedy
    });
    const node = document.querySelector(
      `style[data-emotion="${key} ${serialized.name}"]`
    );
    if (cache.sheet.tags.length > 0) {
      sheet.before = cache.sheet.tags[0];
    }
    let rehydrating = false;
    if (node != null) {
      rehydrating = true;
      node.setAttribute("data-emotion", key);
      sheet.hydrate([node]);
    }
    sheetRef.current = [sheet, rehydrating];
    return () => {
      sheet.flush();
    };
  });
  useInsertionEffect(() => {
    const [sheet, rehydrating] = sheetRef.current ?? [];
    if (sheet == null || rehydrating == null)
      return;
    if (rehydrating) {
      sheetRef.current = [sheet, false];
      return;
    }
    if (serialized.next != null) {
      insertStyles(cache, serialized.next, true);
    }
    if (sheet.tags.length > 0) {
      const element = sheet.tags[sheet.tags.length - 1].nextElementSibling;
      sheet.before = element;
      sheet.flush();
    }
    cache.insert("", serialized, sheet, false);
  });
}
export {
  useGlobalStyle
};
//# sourceMappingURL=use-global-style.js.map