import { COMMENT as e, DECLARATION as t, RULESET as n } from "./Enum.js";
import { abs as r, append as i, charat as a, from as o, indexof as s, replace as c, sizeof as l, strlen as u, substr as d, trim as f } from "./Utility.js";
import { alloc as p, caret as m, char as h, commenter as g, dealloc as _, delimit as v, escaping as y, identifier as b, next as x, node as S, peek as C, prev as w, token as T, whitespace as E } from "./Tokenizer.js";
//#region ../../node_modules/stylis/src/Parser.js
function D(e) {
	return _(O("", null, null, null, [""], e = p(e), 0, [0], e));
}
function O(e, t, n, l, f, p, h, _, S) {
	for (var D = 0, M = 0, N = h, P = 0, F = 0, I = 0, L = 1, R = 1, z = 1, B = 0, V = "", H = f, U = p, W = l, G = V; R;) switch (I = B, B = x()) {
		case 40: if (I != 108 && a(G, N - 1) == 58) {
			s(G += c(v(B), "&", "&\f"), "&\f", r(D ? _[D - 1] : 0)) != -1 && (z = -1);
			break;
		}
		case 34:
		case 39:
		case 91:
			G += v(B);
			break;
		case 9:
		case 10:
		case 13:
		case 32:
			G += E(I);
			break;
		case 92:
			G += y(m() - 1, 7);
			continue;
		case 47:
			switch (C()) {
				case 42:
				case 47:
					i(A(g(x(), m()), t, n, S), S), (T(I || 1) == 5 || T(C() || 1) == 5) && u(G) && d(G, -1, void 0) !== " " && (G += " ");
					break;
				default: G += "/";
			}
			break;
		case 123 * L: _[D++] = u(G) * z;
		case 125 * L:
		case 59:
		case 0:
			switch (B) {
				case 0:
				case 125: R = 0;
				case 59 + M:
					z == -1 && (G = c(G, /\f/g, "")), F > 0 && (u(G) - N || L === 0 && I === 47) && i(F > 32 ? j(G + ";", l, n, N - 1, S) : j(c(G, " ", "") + ";", l, n, N - 2, S), S);
					break;
				case 59: G += ";";
				default: if (i(W = k(G, t, n, D, M, f, _, V, H = [], U = [], N, p), p), B === 123) if (M === 0) O(G, t, W, W, H, p, N, _, U);
				else {
					switch (P) {
						case 99: if (a(G, 3) === 110) break;
						case 108: if (a(G, 2) === 97) break;
						default: M = 0;
						case 100:
						case 109:
						case 115:
					}
					M ? O(e, W, W, l && i(k(e, W, W, 0, 0, f, _, V, f, H = [], N, U), U), f, U, N, _, l ? H : U) : O(G, W, W, W, [""], U, 0, _, U);
				}
			}
			D = M = F = 0, L = z = 1, V = G = "", N = h;
			break;
		case 58: N = 1 + u(G), F = I;
		default:
			if (L < 1) {
				if (B == 123) --L;
				else if (B == 125 && L++ == 0 && w() == 125) continue;
			}
			switch (G += o(B), B * L) {
				case 38:
					z = M > 0 ? 1 : (G += "\f", -1);
					break;
				case 44:
					_[D++] = (u(G) - 1) * z, z = 1;
					break;
				case 64:
					C() === 45 && (G += v(x())), P = C(), M = N = u(V = G += b(m())), B++;
					break;
				case 45: I === 45 && u(G) == 2 && (L = 0);
			}
	}
	return p;
}
function k(e, t, i, a, o, s, u, p, m, h, g, _) {
	for (var v = o - 1, y = o === 0 ? s : [""], b = l(y), x = 0, C = 0, w = 0; x < a; ++x) for (var T = 0, E = d(e, v + 1, v = r(C = u[x])), D = e; T < b; ++T) (D = f(C > 0 ? y[T] + " " + E : c(E, /&\f/g, y[T]))) && (m[w++] = D);
	return S(e, t, i, o === 0 ? n : p, m, h, g, _);
}
function A(t, n, r, i) {
	return S(t, n, r, e, o(h()), d(t, 2, -2), 0, i);
}
function j(e, n, r, i, a) {
	return S(e, n, r, t, d(e, 0, i), d(e, i + 1, -1), i, a);
}
//#endregion
export { D as compile };
