import { E as d, ab as z, e as p, aL as y, y as c, T as o, az as b, f as S, P as m, bq as u, a as O, a9 as x, z as h } from "./am-chart.H2jnOzFc.js";
import { bs as C } from "./am-chart.H2jnOzFc.js";
import { G as k } from "./Gradient.CdcsPIru.js";
class _ extends d {
  constructor() {
    super(...arguments), this._refs = {};
  }
  /**
   * Serializes target object into a simple object or JSON string.
   *
   * @param   source      Target object
   * @param   depth       Current depth
   * @param   full        Serialize object in full (ignoring maxDepth)
   * @param   forceParse  If true, will add __parse to serialized objects
   * @return              Serialized data
   */
  serialize(e, i = 0, a = !1, r = !1) {
    if (i > this.get("maxDepth", 2))
      return;
    if (e === !1 || e === !0)
      return e;
    if (z(e)) {
      const n = [];
      return p(e, (s) => {
        n.push(this.serialize(s, i, a, r));
      }), n;
    } else if (e instanceof y) {
      const n = [];
      return p(e.values, (s) => {
        n.push(this.serialize(s, i, a, r));
      }), n;
    }
    const t = {}, g = e instanceof d || e instanceof o || e instanceof S || e instanceof m, l = this.get("fullSettings", []);
    if (e instanceof d) {
      t.type = e.className;
      let n = this._filterSettings(c(e._settings));
      n = n.filter((s) => e.isUserSetting(s)), n.length && (t.settings = {}, p(n, (s) => {
        const f = e.get(s);
        f !== void 0 && (t.settings[s] = this.serialize(f, i + 1, a || l.indexOf(s) !== -1), e instanceof k && s === "stops" && (t.settings[s] = this.serialize(f, 0, !0)));
      })), this._processAdapters(e, t), this._processStates(e, t);
    } else if (e instanceof o) {
      t.type = "Template";
      let n = this._filterSettings(c(e._settings));
      return n.length && (t.settings = {}, p(n, (s) => {
        e._settings[s] !== void 0 && (t.settings[s] = this.serialize(e.get(s), i + 1, l.indexOf(s) !== -1));
      })), this._processAdapters(e, t), this._processStates(e, t), t;
    }
    if (e instanceof b && e.data.length && (this.get("excludeProperties") || []).indexOf("data") === -1 && (t.properties = {
      data: this.serialize(e.data.values, i, !0)
    }), e instanceof S)
      return {
        type: "Color",
        value: e.toCSSHex()
      };
    if (e instanceof m)
      return {
        type: "Percent",
        value: e.percent
      };
    if (u(e))
      return this._escapeRefs(e);
    if (O(e))
      return e;
    if (x(e)) {
      if (a && !g) {
        const n = this.get("excludeProperties", []);
        h(e, (s, f) => {
          n.indexOf(s) === -1 && f !== void 0 && (t[s] = this.serialize(f, i + 1, a, r));
        }), r && h(t, (s, f) => {
          x(f) && (t.__parse = !0);
        });
      } else if (g) {
        const n = this.get("includeProperties", []);
        p(n, (s) => {
          const f = e[s];
          f !== void 0 && (t.properties === void 0 && (t.properties = {}), t.properties[s] = this.serialize(f, i + 1, a, r));
        });
      }
    }
    return i == 0 && c(this._refs).length && (t.refs = this._refs), t;
  }
  _processAdapters(e, i) {
    if (this.get("includeAdapters", !1)) {
      const a = this.get("functionsAs", "string") == "string", r = e.adapters._callbacks;
      Object.keys(r).length > 0 && (i.adapters = [], h(r, (t, g) => {
        g.forEach((l) => {
          i.adapters.push({
            key: t,
            callback: a ? l.toString() : l
          });
        });
      }));
    }
  }
  _processStates(e, i) {
    if (this.get("includeStates", !1)) {
      const a = e.states._states;
      Object.keys(a).length > 0 && (i.states = [], h(a, (r, t) => {
        Object.keys(t._settings).length > 0 && i.states.push({
          key: r,
          settings: this.serialize(t._settings, 0, !0)
        });
      }));
    }
  }
  _filterSettings(e) {
    const i = this.get("includeSettings", []), a = this.get("excludeSettings", []);
    return i.length ? e = i : a.length && (e = e.filter((r) => a.indexOf(r) === -1)), e;
  }
  _escapeRefs(e) {
    return e.replace(/^#/, "##").replace(/^@/, "@@");
  }
}
_.className = "Serializer";
_.classNames = d.classNames.concat([_.className]);
export {
  C as JsonParser,
  _ as Serializer
};
