import { D as M, F as k, H as w, x as b, I as T, q as v, w as W, J as L, C as R, f as I, L as P, T as N, b as H, m as V, K as j, e as x, r as S, i as K } from "./am-chart.H2jnOzFc.js";
class O extends M {
  setupDefaultRules() {
    super.setupDefaultRules();
    const t = this._root.interfaceColors, u = this.rule.bind(this);
    u("WordCloud").setAll({
      width: b,
      height: b,
      minFontSize: w(2),
      maxFontSize: w(15),
      excludeWords: [],
      angles: [0, -90],
      minWordLength: 1,
      step: 15,
      randomness: 0,
      autoFit: !0,
      animationEasing: k(T)
    });
    {
      const o = u("Label", ["wordcloud"]);
      o.setAll({
        text: "{category}",
        centerX: v,
        centerY: v,
        position: "absolute",
        lineHeight: b,
        populateText: !0
      }), o.setup = (i) => {
        i.set("background", W.new(this._root, {
          fill: t.get("background"),
          fillOpacity: 0
        }));
      };
    }
  }
}
class y extends L {
  constructor() {
    super(...arguments), this._currentIndex = 0, this._ghostContainer = this.children.push(R.new(this._root, { layer: 99, opacity: 0.01 })), this._pointSets = [], this._sets = 3, this._process = !1, this._buffer = [], this.labels = this.addDisposer(this._makeLabels());
  }
  _afterNew() {
    this._defaultThemes.push(O.new(this._root)), this.fields.push("category", "fill"), this._setDefault("valueField", "value"), this._setDefault("categoryField", "category"), this._setDefault("fillField", "fill"), super._afterNew(), this._root.events.on("frameended", () => {
      this.set("progress", this._currentIndex / this.dataItems.length);
    });
  }
  /**
   * @ignore
   */
  makeLabel(t) {
    const u = this.children.push(this.labels.make());
    u._setDataItem(t);
    const o = t.get("fill");
    o != null && u.set("fill", o), u.set("x", -999999), t.set("label", u), this.labels.push(u);
    const i = this._ghostContainer.children.push(this.labels.make());
    return i._setDataItem(t), i.setAll({ fill: I.fromHex(0), fontWeight: "900" }), t.set("ghostLabel", i), this.labels.push(i), u;
  }
  _makeLabels() {
    return new P(N.new({}), () => H._new(this._root, {
      themeTags: V(this.labels.template.get("themeTags", []), ["wordcloud", "series"])
    }, [this.labels.template]));
  }
  processDataItem(t) {
    if (super.processDataItem(t), t.get("fill") == null) {
      let u = this.get("colors");
      u && t.setRaw("fill", u.next());
    }
    this.makeLabel(t);
  }
  _prepareChildren() {
    super._prepareChildren(), this.isDirty("text") && (this.data.setAll(this._getWords(this.get("text"))), this._dirty.text = !1);
  }
  _updateChildren() {
    super._updateChildren();
    const t = this._root._renderer.resolution, u = Math.round(this._root.width() * t);
    let o = this.get("step", 1) * 2;
    if (this._valuesDirty || this._sizeDirty || this.isPrivateDirty("adjustedFontSize")) {
      const a = this.getPrivate("adjustedFontSize", 1), s = this.innerWidth(), r = this.innerHeight(), C = Math.min(s, r), F = Math.max(s, r);
      this._buffer = Array(Math.ceil(this._root.width() * this._root.height() * t * t)).fill(0), C < 800 && (o = o / 2), this._ghostContainer._display.clear(), this._pointSets = [];
      for (let l = 0; l < this._sets; l++) {
        const e = o * (this._sets - l), g = j(s / 2, r / 2, s, r, 0, e * r / F, e * s / F, 0, 0);
        for (let A = g.length - 1; A >= 0; A--) {
          let B = g[A];
          if (B.x < 0 || B.x > s || B.y < 0 || B.y > r) {
            g.splice(A, 1);
            continue;
          }
        }
        this._pointSets.push(g);
      }
      let c = 0, n = 0, h = 0, D = 1 / 0, d = 0;
      x(this._dataItems, (l) => {
        const e = l.get("valueWorking", 0);
        c += e, n += Math.abs(e);
      }), this._dataItems.sort((l, e) => {
        let g = l.get("value", 0), A = e.get("value", 0);
        return g > A ? -1 : g < A ? 1 : 0;
      }), x(this._dataItems, (l) => {
        const e = l.get("valueWorking", 0);
        e >= n && (c = l.get("value", 0)), e > h && (h = e), e < D && (D = e), d++;
      }), this.setPrivateRaw("valueLow", D), this.setPrivateRaw("valueHigh", h), this.setPrivateRaw("valueSum", c), this.setPrivateRaw("valueAverage", c / d), this.setPrivateRaw("valueAbsoluteSum", n);
      const E = Math.min(s, r), p = S(this.get("minFontSize", 10), E) * a, _ = S(this.get("maxFontSize", 100), E) * a, f = this.get("angles", [0]);
      x(this._dataItems, (l) => {
        const e = l.get("valueWorking", 0), g = l.get("ghostLabel");
        let A = p + (_ - p) * (e - D) / (h - D);
        K(A) && (A = _);
        const B = this._sets - 1 - Math.floor((A - p) / (_ - p) * (this._sets - 1));
        l.setRaw("set", B), l.setRaw("fontSize", A);
        let m = f[Math.floor(Math.random() * f.length)];
        l.setRaw("angle", m), g.setAll({ fontSize: A, rotation: m, x: -1e4 });
      }), this._process = !1, this._currentIndex = 0, this._root.events.once("frameended", () => {
        this.setTimeout(() => {
          this._process = !0, this._markDirtyKey("progress");
        }, 50);
      });
    }
    const i = this._boundsToAdd;
    if (i) {
      const a = this._ghostContainer._display.getLayer().context, s = Math.round(i.top), r = Math.round(i.left), C = Math.round(i.right - i.left), F = Math.round(i.bottom - i.top), c = a.getImageData(r, s, C, F).data, n = this._buffer;
      let h = 3;
      for (let D = s; D < s + F; D++)
        for (let d = r; d < r + C; d++) {
          let E = (D + 1) * u - (u - d);
          c[h] != 0 && (n[E] = 1), h += 4;
        }
      this._boundsToAdd = void 0;
    }
    this._process && this.isDirty("progress") && this._processItem();
  }
  _processItem() {
    if (this._boundsToAdd = void 0, this._currentIndex < this.dataItems.length) {
      const t = this.dataItems[this._currentIndex], u = t.get("label"), o = t.get("ghostLabel"), i = this._root._renderer.resolution;
      let a = o.width(), s = o.height();
      const r = o._display.getLayer().context, C = t.get("set"), F = this._pointSets[C], c = this.innerWidth(), n = this.innerHeight(), h = Math.round(this._root.width() * i), D = this.x(), d = this.y(), E = this.get("angles", [0]);
      c > n && a >= c / 2 && x(E, (f) => {
        f == 0 && t.get("angle") != 0 && (t.setRaw("angle", 0), o.set("rotation", 0), [a, s] = [s, a]);
      }), n > c && a >= c / 2 && x(E, (f) => {
        Math.abs(f) == 90 && t.get("angle") == 0 && (t.setRaw("angle", f), o.set("rotation", f), [a, s] = [s, a]);
      });
      const p = Math.ceil(a * i), _ = Math.ceil(s * i);
      if (r && a > 0 && s > 0) {
        let f = Math.round(Math.random() * F.length * this.get("randomness", 0)), l = !0;
        for (; l; ) {
          let e = F[f];
          if (e) {
            if (l = !1, this._currentIndex > 0) {
              let g = Math.round((e.x + D) * i - p / 2), A = Math.round((e.y + d) * i - _ / 2);
              l = this._hasColor(g, A, p, _, h);
            }
            if (e.x - a / 2 < 0 || e.x + a / 2 > c || e.y - s / 2 < 0 || e.y + s / 2 > n)
              f++, l = !0;
            else if (l)
              f += 2;
            else {
              const g = t.get("angle", 0), A = t.get("fontSize", 0);
              u.get("x") != -999999 ? (u.animate({ key: "x", to: e.x, duration: this.get("animationDuration", 0), easing: this.get("animationEasing") }), u.animate({ key: "y", to: e.y, duration: this.get("animationDuration", 0), easing: this.get("animationEasing") }), u.animate({ key: "rotation", to: g, duration: this.get("animationDuration", 0), easing: this.get("animationEasing") }), u.animate({ key: "fontSize", to: A, duration: this.get("animationDuration", 0), easing: this.get("animationEasing") })) : (u.setAll({ x: e.x, y: e.y, rotation: g, fontSize: A }), u.appear()), o.setAll({ x: e.x, y: e.y });
              for (let B = F.length - 1; B >= 0; B--) {
                let m = F[B];
                m.x >= e.x - a / 2 && m.x <= e.x + a / 2 && m.y >= e.y - s / 2 && m.y <= e.y + s / 2 && F.splice(B, 1);
              }
              this._boundsToAdd = { left: (e.x + D - a / 2) * i, right: (e.x + D + a / 2) * i, top: (e.y + d - s / 2) * i, bottom: (e.y + d + s / 2) * i };
            }
          } else {
            if (this.get("autoFit")) {
              this.setTimeout(() => {
                this.setPrivate("adjustedFontSize", this.getPrivate("adjustedFontSize", 1) * 0.9);
              }, 50);
              return;
            }
            u.set("x", -999999), l = !1;
          }
        }
      }
      this._currentIndex++;
    }
  }
  /**
  * @ignore
  */
  disposeDataItem(t) {
    super.disposeDataItem(t);
    const u = t.get("label");
    u && (this.labels.removeValue(u), u.dispose());
    const o = t.get("ghostLabel");
    o && (this.labels.removeValue(o), o.dispose());
  }
  /**
  * Extracts words and number of their appearances from a text.
  *
  * @ignore
  * @param  input  Source text
  */
  _getWords(t) {
    let u = [];
    if (t) {
      const o = "A-Za-zªµºÀ-ÖØ-öø-ˁˆ-ˑˠ-ˤˬˮͰ-ʹͶ-ͷͺ-ͽΆΈ-ΊΌΎ-ΡΣ-ϵϷ-ҁҊ-ԣԱ-Ֆՙա-ևא-תװ-ײء-يٮ-ٯٱ-ۓەۥ-ۦۮ-ۯۺ-ۼۿܐܒ-ܯݍ-ޥޱߊ-ߪߴ-ߵߺऄ-हऽॐक़-ॡॱ-ॲॻ-ॿঅ-ঌএ-ঐও-নপ-রলশ-হঽৎড়-ঢ়য়-ৡৰ-ৱਅ-ਊਏ-ਐਓ-ਨਪ-ਰਲ-ਲ਼ਵ-ਸ਼ਸ-ਹਖ਼-ੜਫ਼ੲ-ੴઅ-ઍએ-ઑઓ-નપ-રલ-ળવ-હઽૐૠ-ૡଅ-ଌଏ-ଐଓ-ନପ-ରଲ-ଳଵ-ହଽଡ଼-ଢ଼ୟ-ୡୱஃஅ-ஊஎ-ஐஒ-கங-சஜஞ-டண-தந-பம-ஹௐఅ-ఌఎ-ఐఒ-నప-ళవ-హఽౘ-ౙౠ-ౡಅ-ಌಎ-ಐಒ-ನಪ-ಳವ-ಹಽೞೠ-ೡഅ-ഌഎ-ഐഒ-നപ-ഹഽൠ-ൡൺ-ൿඅ-ඖක-නඳ-රලව-ෆก-ะา-ำเ-ๆກ-ຂຄງ-ຈຊຍດ-ທນ-ຟມ-ຣລວສ-ຫອ-ະາ-ຳຽເ-ໄໆໜ-ໝༀཀ-ཇཉ-ཬྈ-ྋက-ဪဿၐ-ၕၚ-ၝၡၥ-ၦၮ-ၰၵ-ႁႎႠ-Ⴥა-ჺჼᄀ-ᅙᅟ-ᆢᆨ-ᇹሀ-ቈቊ-ቍቐ-ቖቘቚ-ቝበ-ኈኊ-ኍነ-ኰኲ-ኵኸ-ኾዀዂ-ዅወ-ዖዘ-ጐጒ-ጕጘ-ፚᎀ-ᎏᎠ-Ᏼᐁ-ᙬᙯ-ᙶᚁ-ᚚᚠ-ᛪᛮ-ᛰᜀ-ᜌᜎ-ᜑᜠ-ᜱᝀ-ᝑᝠ-ᝬᝮ-ᝰក-ឳៗៜᠠ-ᡷᢀ-ᢨᢪᤀ-ᤜᥐ-ᥭᥰ-ᥴᦀ-ᦩᧁ-ᧇᨀ-ᨖᬅ-ᬳᭅ-ᭋᮃ-ᮠᮮ-ᮯᰀ-ᰣᱍ-ᱏᱚ-ᱽᴀ-ᶿḀ-ἕἘ-Ἕἠ-ὅὈ-Ὅὐ-ὗὙὛὝὟ-ώᾀ-ᾴᾶ-ᾼιῂ-ῄῆ-ῌῐ-ΐῖ-Ίῠ-Ῥῲ-ῴῶ-ῼⁱⁿₐ-ₔℂℇℊ-ℓℕℙ-ℝℤΩℨK-ℭℯ-ℹℼ-ℿⅅ-ⅉⅎⅠ-ↈⰀ-Ⱞⰰ-ⱞⱠ-Ɐⱱ-ⱽⲀ-ⳤⴀ-ⴥⴰ-ⵥⵯⶀ-ⶖⶠ-ⶦⶨ-ⶮⶰ-ⶶⶸ-ⶾⷀ-ⷆⷈ-ⷎⷐ-ⷖⷘ-ⷞⸯ々-〇〡-〩〱-〵〸-〼ぁ-ゖゝ-ゟァ-ヺー-ヿㄅ-ㄭㄱ-ㆎㆠ-ㆷㇰ-ㇿ㐀䶵一鿃ꀀ-ꒌꔀ-ꘌꘐ-ꘟꘪ-ꘫꙀ-ꙟꙢ-ꙮꙿ-ꚗꜗ-ꜟꜢ-ꞈꞋ-ꞌꟻ-ꠁꠃ-ꠅꠇ-ꠊꠌ-ꠢꡀ-ꡳꢂ-ꢳꤊ-ꤥꤰ-ꥆꨀ-ꨨꩀ-ꩂꩄ-ꩋ가-힣豈-鶴侮-頻並-龎ﬀ-ﬆﬓ-ﬗיִײַ-ﬨשׁ-זּטּ-לּמּנּ-סּףּ-פּצּ-ﮱﯓ-ﴽﵐ-ﶏﶒ-ﷇﷰ-ﷻﹰ-ﹴﹶ-ﻼＡ-Ｚａ-ｚｦ-ﾾￂ-ￇￊ-ￏￒ-ￗￚ-ￜ0-9@+", i = new RegExp("([" + o + "]+[-" + o + "]*[" + o + "]+)|([" + o + "]+)", "ig");
      let a = t.match(i);
      if (!a)
        return [];
      let s;
      for (; s = a.pop(), !!s; ) {
        let n;
        for (let h = 0; h < u.length; h++)
          if (u[h].category.toLowerCase() == s.toLowerCase()) {
            n = u[h];
            break;
          }
        n ? (n.value++, this.isCapitalized(s) || (n.category = s)) : u.push({
          category: s,
          value: 1
        });
      }
      let r = this.get("excludeWords");
      const C = this.get("minValue", 1), F = this.get("minWordLength", 1);
      if (C > 1 || F > 1 || r && r.length > 0)
        for (let n = u.length - 1; n >= 0; n--) {
          let h = u[n], D = h.category;
          h.value < C && u.splice(n, 1), D.length < F && u.splice(n, 1), r && r.indexOf(D) !== -1 && u.splice(n, 1);
        }
      u.sort(function(n, h) {
        return n.value == h.value ? 0 : n.value > h.value ? -1 : 1;
      });
      const c = this.get("maxCount", 1 / 0);
      u.length > c && (u = u.slice(0, c));
    }
    return u;
  }
  /**
  * Checks if word is capitalized (starts with an uppercase) or not.
  *
  * @ignore
  * @param   word  Word
  * @return        Capitalized?
  */
  isCapitalized(t) {
    let u = t.toLowerCase();
    return t[0] != u[0] && t.substr(1) == u.substr(1) && t != u;
  }
  _hasColor(t, u, o, i, a) {
    const s = this._buffer;
    if (s)
      for (let r = u; r < u + i; r += 4)
        for (let C = t; C < t + o; C += 4) {
          let F = (r + 1) * a - (a - C);
          if (s[F] != 0)
            return !0;
        }
    return !1;
  }
}
y.className = "WordCloud";
y.classNames = L.classNames.concat([y.className]);
export {
  O as DefaultTheme,
  y as WordCloud
};
