import { b as p, C as u, R as v, M as m, s as x, c as f, g as c, e as _, d as b, P as w, a as y } from "./am-chart.H2jnOzFc.js";
class d extends p {
  _afterNew() {
    super._afterNew();
    const e = this.children.push(u.new(this._root, {
      html: '<textarea class="am5-editable-label"></textarea>',
      isMeasured: !1
    }));
    e.hide(), this.setPrivate("input", e);
    let t = this.get("background");
    t ? t.set("themeTags", ["editablelabel", "background"]) : t = this.set("background", v.new(this._root, {
      themeTags: ["editablelabel", "background"]
    }));
  }
  _beforeChanged() {
    if (super._beforeChanged(), this.isDirty("editOn")) {
      this._editOnEvents && this._editOnEvents.dispose();
      const e = this.get("editOn", "click");
      e != "none" && (this._editOnEvents = new m([
        this.getPrivate("input").events.on(e, (t) => {
        }),
        this.events.on(e, (t) => {
          this.set("active", !0);
        })
      ]));
    }
  }
  _prepareChildren() {
    super._prepareChildren(), this._maybeInitTextarea();
  }
  _updateChildren() {
    if (super._updateChildren(), this.isDirty("active")) {
      const e = this.get("active", !1);
      e ? this._startEditing() : this._stopEditing();
      const t = this.get("background");
      t && t.set("active", e);
    }
    this._syncText(), this._syncStyle();
  }
  _maybeInitTextarea() {
    if (!this._isInited()) {
      const e = this.getPrivate("input");
      if (e && e.getPrivate("htmlElement")) {
        const i = e.getPrivate("htmlElement").querySelector(".am5-editable-label");
        i && (this.setPrivate("textarea", i), i.addEventListener("input", (s) => {
          this.get("multiLine") === !1 && (i.value = i.value.replace(/\n/g, " ")), this.set("text", i.value), this._syncStyle();
        }), i.addEventListener("blur", (s) => this.set("active", !1)), x("keyboardevents") && this._disposers.push(f(document, "keydown", (s) => {
          c(s) == "Escape" && this.set("active", !1);
        })), this._disposers.push(f(document, "keydown", (s) => {
          c(s) == "Enter" && this.get("multiLine") === !1 && (s.preventDefault(), this.set("active", !1));
        })), this.events.dispatch("inited", {
          type: "inited",
          target: this
        }));
      }
    }
  }
  _isInited() {
    return !!this.getPrivate("textarea");
  }
  _startEditing() {
    if (!this._isInited()) {
      this.events.once("inited", () => {
        this._startEditing();
      });
      return;
    }
    this._text.set("opacity", 0);
    const e = this.getPrivate("input"), t = this.getPrivate("textarea");
    t && (this.get("text", "") == "" && this.set("text", " "), e.show(0), this.setTimeout(() => {
      this._syncStyle(), t.focus();
    }, 100));
  }
  _stopEditing() {
    if (!this._isInited()) {
      this.events.once("inited", () => {
        this._stopEditing();
      });
      return;
    }
    const e = this.getPrivate("input"), t = this.getPrivate("textarea");
    t && (this.set("text", t.value), e.hide(0), this._text.set("opacity", 1));
  }
  _syncStyle() {
    const e = this.getPrivate("input"), t = this.getPrivate("textarea"), i = e.getPrivate("htmlElement");
    if (t && i) {
      const s = window.getComputedStyle(t);
      _(s, (h) => {
        t.style[h] = "initial";
      }), t.style.color = this.get("fill", b(0)).toCSS(this.get("fillOpacity", 1)), t.style.backgroundColor = "rgba(0, 0, 0, 0)", t.style.border = "none", t.style.outline = "none", t.style.padding = "0", t.wrap = "off", t.style.resize = "none", t.style.overflow = "hidden";
      const g = this.get("maxWidth", 0) - this.get("paddingLeft", 0) - this.get("paddingRight", 0);
      g > 0 ? t.style.maxWidth = g + "px" : t.style.minWidth = "", t.style.height = "auto", t.style.minHeight = t.scrollHeight + "px", this.get("width") && (t.style.width = this.width() - this.get("paddingLeft", 0) - this.get("paddingRight", 0) + "px", t.style.minWidth = "");
      const n = this.get("lineHeight");
      n ? n instanceof w ? t.style.lineHeight = n.value + "" : y(n) && (t.style.lineHeight = n + "") : t.style.lineHeight = "1.2";
      let o = this.get("fontFamily");
      o || (o = getComputedStyle(i, "font-family").getPropertyValue("font-family")), t.style.fontFamily = o;
      let a = this.get("fontSize");
      a ? y(a) ? a = a + "px" : a = a : a = getComputedStyle(i, "font-size").getPropertyValue("font-size"), t.style.fontSize = a;
      let l = this.get("fontWeight");
      l ? l = l : l = getComputedStyle(i, "font-weight").getPropertyValue("font-weight"), t.style.fontWeight = l;
      let r = this.get("fontStyle");
      r ? r = r : r = getComputedStyle(i, "font-style").getPropertyValue("font-style"), t.style.fontStyle = r, this.get("oversizedBehavior") == "wrap" ? t.style.whiteSpace = "pre-wrap" : t.style.whiteSpace = "nowrap", this._root.events.on("frameended", () => {
        t.style.minWidth == "" && (t.style.minWidth = t.scrollWidth + 20 + "px");
        const h = this.get("textAlign", "start");
        h == "center" ? (t.style.textAlign = "center", !i.style.transform.match(/translateX/) && !this.get("width") && (i.style.transform += " translateX(-50%)")) : h == "end" ? (t.style.textAlign = "right", !i.style.transform.match(/translateX/) && !this.get("width") && (i.style.transform += " translateX(-100%)")) : t.style.textAlign = h;
      });
    }
  }
  _syncText() {
    const e = this.getPrivate("textarea");
    let t = this.get("text", "");
    t == " " && (t = ""), e && (e.value = t);
  }
}
d.className = "EditableLabel";
d.classNames = p.classNames.concat([d.className]);
export {
  d as E
};
