"use strict";
"use client";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var use_global_style_exports = {};
__export(use_global_style_exports, {
  useGlobalStyle: () => useGlobalStyle
});
module.exports = __toCommonJS(use_global_style_exports);
var React = __toESM(require("react"));
var import_react = require("react");
var import_serialize = require("@emotion/serialize");
var import_utils = require("@emotion/utils");
var import_root_style_registry = require("./root-style-registry");
const isServer = typeof window === "undefined";
const useInsertionEffectSpecifier = "useInsertionEffect";
const useInsertionEffect = React[useInsertionEffectSpecifier] ?? React.useLayoutEffect;
function useGlobalStyle(...args) {
  const cache = (0, import_root_style_registry.useCache)();
  const serialized = (0, import_serialize.serializeStyles)(args, cache.registered);
  if (isServer) {
    if (cache.inserted[serialized.name] === void 0) {
      cache.insert("", serialized, cache.sheet, true);
    }
  }
  const sheetRef = (0, import_react.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) {
      (0, import_utils.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);
  });
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  useGlobalStyle
});
//# sourceMappingURL=use-global-style.js.map