import { __require as k } from "./comment.js";
import { __require as D } from "./declaration.js";
import { __require as P } from "./node.js";
import { __require as q } from "./symbols.js";
var y, v;
function j() {
  if (v) return y;
  v = 1;
  let h = k(), p = D(), R = P(), { isClean: x, my: m } = q(), a, d, w, l;
  function O(s) {
    return s.map((e) => (e.nodes && (e.nodes = O(e.nodes)), delete e.source, e));
  }
  function g(s) {
    if (s[x] = !1, s.proxyOf.nodes)
      for (let e of s.proxyOf.nodes)
        g(e);
  }
  class f extends R {
    get first() {
      if (this.proxyOf.nodes)
        return this.proxyOf.nodes[0];
    }
    get last() {
      if (this.proxyOf.nodes)
        return this.proxyOf.nodes[this.proxyOf.nodes.length - 1];
    }
    append(...e) {
      for (let t of e) {
        let r = this.normalize(t, this.last);
        for (let i of r) this.proxyOf.nodes.push(i);
      }
      return this.markDirty(), this;
    }
    cleanRaws(e) {
      if (super.cleanRaws(e), this.nodes)
        for (let t of this.nodes) t.cleanRaws(e);
    }
    each(e) {
      if (!this.proxyOf.nodes) return;
      let t = this.getIterator(), r, i;
      for (; this.indexes[t] < this.proxyOf.nodes.length && (r = this.indexes[t], i = e(this.proxyOf.nodes[r], r), i !== !1); )
        this.indexes[t] += 1;
      return delete this.indexes[t], i;
    }
    every(e) {
      return this.nodes.every(e);
    }
    getIterator() {
      this.lastEach || (this.lastEach = 0), this.indexes || (this.indexes = {}), this.lastEach += 1;
      let e = this.lastEach;
      return this.indexes[e] = 0, e;
    }
    getProxyProcessor() {
      return {
        get(e, t) {
          return t === "proxyOf" ? e : e[t] ? t === "each" || typeof t == "string" && t.startsWith("walk") ? (...r) => e[t](
            ...r.map((i) => typeof i == "function" ? (n, o) => i(n.toProxy(), o) : i)
          ) : t === "every" || t === "some" ? (r) => e[t](
            (i, ...n) => r(i.toProxy(), ...n)
          ) : t === "root" ? () => e.root().toProxy() : t === "nodes" ? e.nodes.map((r) => r.toProxy()) : t === "first" || t === "last" ? e[t].toProxy() : e[t] : e[t];
        },
        set(e, t, r) {
          return e[t] === r || (e[t] = r, (t === "name" || t === "params" || t === "selector") && e.markDirty()), !0;
        }
      };
    }
    index(e) {
      return typeof e == "number" ? e : (e.proxyOf && (e = e.proxyOf), this.proxyOf.nodes.indexOf(e));
    }
    insertAfter(e, t) {
      let r = this.index(e), i = this.normalize(t, this.proxyOf.nodes[r]).reverse();
      r = this.index(e);
      for (let o of i) this.proxyOf.nodes.splice(r + 1, 0, o);
      let n;
      for (let o in this.indexes)
        n = this.indexes[o], r < n && (this.indexes[o] = n + i.length);
      return this.markDirty(), this;
    }
    insertBefore(e, t) {
      let r = this.index(e), i = r === 0 ? "prepend" : !1, n = this.normalize(
        t,
        this.proxyOf.nodes[r],
        i
      ).reverse();
      r = this.index(e);
      for (let u of n) this.proxyOf.nodes.splice(r, 0, u);
      let o;
      for (let u in this.indexes)
        o = this.indexes[u], r <= o && (this.indexes[u] = o + n.length);
      return this.markDirty(), this;
    }
    normalize(e, t) {
      if (typeof e == "string")
        e = O(d(e).nodes);
      else if (typeof e > "u")
        e = [];
      else if (Array.isArray(e)) {
        e = e.slice(0);
        for (let i of e)
          i.parent && i.parent.removeChild(i, "ignore");
      } else if (e.type === "root" && this.type !== "document") {
        e = e.nodes.slice(0);
        for (let i of e)
          i.parent && i.parent.removeChild(i, "ignore");
      } else if (e.type)
        e = [e];
      else if (e.prop) {
        if (typeof e.value > "u")
          throw new Error("Value field is missed in node creation");
        typeof e.value != "string" && (e.value = String(e.value)), e = [new p(e)];
      } else if (e.selector || e.selectors)
        e = [new l(e)];
      else if (e.name)
        e = [new a(e)];
      else if (e.text)
        e = [new h(e)];
      else
        throw new Error("Unknown node type in node creation");
      return e.map((i) => (i[m] || f.rebuild(i), i = i.proxyOf, i.parent && i.parent.removeChild(i), i[x] && g(i), i.raws || (i.raws = {}), typeof i.raws.before > "u" && t && typeof t.raws.before < "u" && (i.raws.before = t.raws.before.replace(/\S/g, "")), i.parent = this.proxyOf, i));
    }
    prepend(...e) {
      e = e.reverse();
      for (let t of e) {
        let r = this.normalize(t, this.first, "prepend").reverse();
        for (let i of r) this.proxyOf.nodes.unshift(i);
        for (let i in this.indexes)
          this.indexes[i] = this.indexes[i] + r.length;
      }
      return this.markDirty(), this;
    }
    push(e) {
      return e.parent = this, this.proxyOf.nodes.push(e), this;
    }
    removeAll() {
      for (let e of this.proxyOf.nodes) e.parent = void 0;
      return this.proxyOf.nodes = [], this.markDirty(), this;
    }
    removeChild(e) {
      e = this.index(e), this.proxyOf.nodes[e].parent = void 0, this.proxyOf.nodes.splice(e, 1);
      let t;
      for (let r in this.indexes)
        t = this.indexes[r], t >= e && (this.indexes[r] = t - 1);
      return this.markDirty(), this;
    }
    replaceValues(e, t, r) {
      return r || (r = t, t = {}), this.walkDecls((i) => {
        t.props && !t.props.includes(i.prop) || t.fast && !i.value.includes(t.fast) || (i.value = i.value.replace(e, r));
      }), this.markDirty(), this;
    }
    some(e) {
      return this.nodes.some(e);
    }
    walk(e) {
      return this.each((t, r) => {
        let i;
        try {
          i = e(t, r);
        } catch (n) {
          throw t.addToError(n);
        }
        return i !== !1 && t.walk && (i = t.walk(e)), i;
      });
    }
    walkAtRules(e, t) {
      return t ? e instanceof RegExp ? this.walk((r, i) => {
        if (r.type === "atrule" && e.test(r.name))
          return t(r, i);
      }) : this.walk((r, i) => {
        if (r.type === "atrule" && r.name === e)
          return t(r, i);
      }) : (t = e, this.walk((r, i) => {
        if (r.type === "atrule")
          return t(r, i);
      }));
    }
    walkComments(e) {
      return this.walk((t, r) => {
        if (t.type === "comment")
          return e(t, r);
      });
    }
    walkDecls(e, t) {
      return t ? e instanceof RegExp ? this.walk((r, i) => {
        if (r.type === "decl" && e.test(r.prop))
          return t(r, i);
      }) : this.walk((r, i) => {
        if (r.type === "decl" && r.prop === e)
          return t(r, i);
      }) : (t = e, this.walk((r, i) => {
        if (r.type === "decl")
          return t(r, i);
      }));
    }
    walkRules(e, t) {
      return t ? e instanceof RegExp ? this.walk((r, i) => {
        if (r.type === "rule" && e.test(r.selector))
          return t(r, i);
      }) : this.walk((r, i) => {
        if (r.type === "rule" && r.selector === e)
          return t(r, i);
      }) : (t = e, this.walk((r, i) => {
        if (r.type === "rule")
          return t(r, i);
      }));
    }
  }
  return f.registerParse = (s) => {
    d = s;
  }, f.registerRule = (s) => {
    l = s;
  }, f.registerAtRule = (s) => {
    a = s;
  }, f.registerRoot = (s) => {
    w = s;
  }, y = f, f.default = f, f.rebuild = (s) => {
    s.type === "atrule" ? Object.setPrototypeOf(s, a.prototype) : s.type === "rule" ? Object.setPrototypeOf(s, l.prototype) : s.type === "decl" ? Object.setPrototypeOf(s, p.prototype) : s.type === "comment" ? Object.setPrototypeOf(s, h.prototype) : s.type === "root" && Object.setPrototypeOf(s, w.prototype), s[m] = !0, s.nodes && s.nodes.forEach((e) => {
      f.rebuild(e);
    });
  }, y;
}
export {
  j as __require
};
//# sourceMappingURL=container.js.map
