import er from "../../../../@babel/runtime/helpers/esm/extends.mjs";
import W from "../../../../@babel/runtime/helpers/esm/defineProperty.mjs";
import X from "../../../../@babel/runtime/helpers/esm/objectSpread2.mjs";
import b from "../../../../@babel/runtime/helpers/esm/slicedToArray.mjs";
import G from "../../../../@babel/runtime/helpers/esm/toConsumableArray.mjs";
import F from "../../../../@babel/runtime/helpers/esm/typeof.mjs";
import ar from "../../../../@emotion/hash/dist/hash.browser.esm.mjs";
import { updateCSS as Z, removeCSS as nr } from "../../../../rc-util/es/Dom/dynamicCSS.mjs";
import * as M from "react";
import or from "../../../../@emotion/unitless/dist/unitless.browser.esm.mjs";
import ir from "../linters/contentQuotesLinter.mjs";
import cr from "../linters/hashedAnimationLinter.mjs";
import "../../../../rc-util/es/warning.mjs";
import lr, { CSS_IN_JS_INSTANCE as sr, ATTR_TOKEN as B, ATTR_CACHE_PATH as fr, ATTR_MARK as J } from "../StyleContext.mjs";
import { toStyleStr as K, isClientSide as V, supportLayer as ur } from "../util/index.mjs";
import { CSS_FILE_STYLE as mr, existPath as vr, getStyleAndHash as hr } from "../util/cacheMapUtil.mjs";
import pr from "./useGlobalCache.mjs";
import { serialize as dr, stringify as yr } from "../../../../stylis/src/Serializer.mjs";
import { compile as _r } from "../../../../stylis/src/Parser.mjs";
var rr = "_skip_check_", tr = "_multi_value_";
function Q(t) {
  var s = dr(_r(t), yr);
  return s.replace(/\{%%%\:[^;];}/g, ";");
}
function Sr(t) {
  return F(t) === "object" && t && (rr in t || tr in t);
}
function Er(t, s, i) {
  if (!s)
    return t;
  var _ = ".".concat(s), c = i === "low" ? ":where(".concat(_, ")") : _, I = t.split(",").map(function(p) {
    var e, m = p.trim().split(/\s+/), d = m[0] || "", a = ((e = d.match(/^\w+/)) === null || e === void 0 ? void 0 : e[0]) || "";
    return d = "".concat(a).concat(c).concat(d.slice(a.length)), [d].concat(G(m.slice(1))).join(" ");
  });
  return I.join(",");
}
var Ar = function t(s) {
  var i = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {}, _ = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : {
    root: !0,
    parentSelectors: []
  }, c = _.root, I = _.injectHash, p = _.parentSelectors, e = i.hashId, m = i.layer, d = i.path, a = i.hashPriority, x = i.transformers, k = x === void 0 ? [] : x, N = i.linters, H = N === void 0 ? [] : N, n = "", A = {};
  function z(y) {
    var f = y.getName(e);
    if (!A[f]) {
      var v = t(y.style, i, {
        root: !1,
        parentSelectors: p
      }), h = b(v, 1), r = h[0];
      A[f] = "@keyframes ".concat(y.getName(e)).concat(r);
    }
  }
  function D(y) {
    var f = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : [];
    return y.forEach(function(v) {
      Array.isArray(v) ? D(v, f) : v && f.push(v);
    }), f;
  }
  var q = D(Array.isArray(s) ? s : [s]);
  if (q.forEach(function(y) {
    var f = typeof y == "string" && !c ? {} : y;
    if (typeof f == "string")
      n += "".concat(f, `
`);
    else if (f._keyframe)
      z(f);
    else {
      var v = k.reduce(function(h, r) {
        var j;
        return (r == null || (j = r.visit) === null || j === void 0 ? void 0 : j.call(r, h)) || h;
      }, f);
      Object.keys(v).forEach(function(h) {
        var r = v[h];
        if (F(r) === "object" && r && (h !== "animationName" || !r._keyframe) && !Sr(r)) {
          var j = !1, o = h.trim(), l = !1;
          (c || I) && e ? o.startsWith("@") ? j = !0 : o = Er(h, e, a) : c && !e && (o === "&" || o === "") && (o = "", l = !0);
          var u = t(r, i, {
            root: l,
            injectHash: j,
            parentSelectors: [].concat(G(p), [o])
          }), C = b(u, 2), $ = C[0], L = C[1];
          A = X(X({}, A), L), n += "".concat(o).concat($);
        } else {
          let O = function(P, E) {
            process.env.NODE_ENV !== "production" && (F(r) !== "object" || !(r != null && r[rr])) && [ir, cr].concat(G(H)).forEach(function(Y) {
              return Y(P, E, {
                path: d,
                hashId: e,
                parentSelectors: p
              });
            });
            var U = P.replace(/[A-Z]/g, function(Y) {
              return "-".concat(Y.toLowerCase());
            }), w = E;
            !or[P] && typeof w == "number" && w !== 0 && (w = "".concat(w, "px")), P === "animationName" && E !== null && E !== void 0 && E._keyframe && (z(E), w = E.getName(e)), n += "".concat(U, ":").concat(w, ";");
          };
          var T, S = (T = r == null ? void 0 : r.value) !== null && T !== void 0 ? T : r;
          F(r) === "object" && r !== null && r !== void 0 && r[tr] && Array.isArray(S) ? S.forEach(function(P) {
            O(h, P);
          }) : O(h, S);
        }
      });
    }
  }), !c)
    n = "{".concat(n, "}");
  else if (m && ur()) {
    var g = m.split(","), R = g[g.length - 1].trim();
    n = "@layer ".concat(R, " {").concat(n, "}"), g.length > 1 && (n = "@layer ".concat(m, "{%%%:%}").concat(n));
  }
  return [n, A];
};
function Cr(t, s) {
  return ar("".concat(t.join("%")).concat(s));
}
function Ir() {
  return null;
}
var Nr = "style";
function qr(t, s) {
  var i = t.token, _ = t.path, c = t.hashId, I = t.layer, p = t.nonce, e = t.clientOnly, m = t.order, d = m === void 0 ? 0 : m, a = M.useContext(lr), x = a.autoClear, k = a.mock, N = a.defaultCache, H = a.hashPriority, n = a.container, A = a.ssrInline, z = a.transformers, D = a.linters, q = a.cache, g = i._tokenKey, R = [g].concat(G(_)), y = V;
  process.env.NODE_ENV !== "production" && k !== void 0 && (y = k === "client");
  var f = pr(
    Nr,
    R,
    // Create cache if needed
    function() {
      var o = R.join("|");
      if (vr(o)) {
        var l = hr(o), u = b(l, 2), C = u[0], $ = u[1];
        if (C)
          return [C, g, $, {}, e, d];
      }
      var L = s(), T = Ar(L, {
        hashId: c,
        hashPriority: H,
        layer: I,
        path: _.join("-"),
        transformers: z,
        linters: D
      }), S = b(T, 2), O = S[0], P = S[1], E = Q(O), U = Cr(R, E);
      return [E, g, U, P, e, d];
    },
    // Remove cache if no need
    function(o, l) {
      var u = b(o, 3), C = u[2];
      (l || x) && V && nr(C, {
        mark: J
      });
    },
    // Effect: Inject style here
    function(o) {
      var l = b(o, 4), u = l[0];
      l[1];
      var C = l[2], $ = l[3];
      if (y && u !== mr) {
        var L = {
          mark: J,
          prepend: "queue",
          attachTo: n,
          priority: d
        }, T = typeof p == "function" ? p() : p;
        T && (L.csp = {
          nonce: T
        });
        var S = Z(u, C, L);
        S[sr] = q.instanceId, S.setAttribute(B, g), process.env.NODE_ENV !== "production" && S.setAttribute(fr, R.join("|")), Object.keys($).forEach(function(O) {
          Z(Q($[O]), "_effect-".concat(O), L);
        });
      }
    }
  ), v = b(f, 3), h = v[0], r = v[1], j = v[2];
  return function(o) {
    var l;
    if (!A || y || !N)
      l = /* @__PURE__ */ M.createElement(Ir, null);
    else {
      var u;
      l = /* @__PURE__ */ M.createElement("style", er({}, (u = {}, W(u, B, r), W(u, J, j), u), {
        dangerouslySetInnerHTML: {
          __html: h
        }
      }));
    }
    return /* @__PURE__ */ M.createElement(M.Fragment, null, l, o);
  };
}
var Ur = function(s, i, _) {
  var c = b(s, 6), I = c[0], p = c[1], e = c[2], m = c[3], d = c[4], a = c[5], x = _ || {}, k = x.plain;
  if (d)
    return null;
  var N = I, H = {
    "data-rc-order": "prependQueue",
    "data-rc-priority": "".concat(a)
  };
  return N = K(I, p, e, H, k), m && Object.keys(m).forEach(function(n) {
    if (!i[n]) {
      i[n] = !0;
      var A = Q(m[n]);
      N += K(A, p, "_effect-".concat(n), H, k);
    }
  }), [a, e, N];
};
export {
  Nr as STYLE_PREFIX,
  qr as default,
  Ur as extract,
  Q as normalizeStyle,
  Ar as parseStyle,
  Cr as uniqueHash
};
