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