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