import { C as y, m as b, R as w, B as v, G as x, k as D, a as g } from "./am-chart.H2jnOzFc.js";
class c extends y {
  constructor() {
    super(...arguments), this.thumb = this._makeThumb(), this.startGrip = this._makeButton(), this.endGrip = this._makeButton(), this._thumbBusy = !1, this._startDown = !1, this._endDown = !1, this._thumbDown = !1;
  }
  _addOrientationClass() {
    this._settings.themeTags = b(this._settings.themeTags, ["scrollbar", this._settings.orientation]), this._settings.background || (this._settings.background = w.new(this._root, {
      themeTags: b(this._settings.themeTags, ["main", "background"])
    }));
  }
  _makeButton() {
    return this.children.push(v.new(this._root, {
      themeTags: ["resize", "button", this.get("orientation")],
      icon: x.new(this._root, {
        themeTags: ["icon"]
      })
    }));
  }
  _makeThumb() {
    return this.children.push(w.new(this._root, {
      themeTags: ["thumb", this.get("orientation")]
    }));
  }
  _handleAnimation(t) {
    t && this._disposers.push(t.events.on("stopped", () => {
      this.setPrivateRaw("isBusy", !1), this._thumbBusy = !1;
    }));
  }
  _afterNew() {
    this._addOrientationClass(), super._afterNew();
    const t = this.startGrip, e = this.endGrip, i = this.thumb, r = this.get("background");
    r && this._disposers.push(r.events.on("click", (s) => {
      this.setPrivateRaw("isBusy", !0);
      const a = this._display.toLocal(s.point), h = this.width(), o = this.height(), d = this.get("orientation");
      let n;
      d == "vertical" ? n = (a.y - i.height() / 2) / o : n = (a.x - i.width() / 2) / h;
      let u, p;
      d == "vertical" ? (u = n * o, p = "y") : (u = n * h, p = "x");
      const l = this.get("animationDuration", 0);
      l > 0 ? (this._thumbBusy = !0, this._handleAnimation(this.thumb.animate({ key: p, to: u, duration: l, easing: this.get("animationEasing") }))) : (this.thumb.set(p, u), this._root.events.once("frameended", () => {
        this.setPrivateRaw("isBusy", !1);
      }));
    })), this._disposers.push(i.events.on("dblclick", (s) => {
      if (!D(s.originalEvent, this))
        return;
      const a = this.get("animationDuration", 0), h = this.get("animationEasing");
      this.animate({ key: "start", to: 0, duration: a, easing: h }), this.animate({ key: "end", to: 1, duration: a, easing: h });
    })), this._disposers.push(t.events.on("pointerdown", () => {
      this.setPrivateRaw("isBusy", !0), this._startDown = !0, this._gripDown = "start";
    })), this._disposers.push(e.events.on("pointerdown", () => {
      this.setPrivateRaw("isBusy", !0), this._endDown = !0, this._gripDown = "end";
    })), this._disposers.push(i.events.on("pointerdown", () => {
      this.setPrivateRaw("isBusy", !0), this._thumbDown = !0, this._gripDown = void 0;
    })), this._disposers.push(t.events.on("globalpointerup", () => {
      this._startDown && (this.setPrivateRaw("isBusy", !1), this._released()), this._startDown = !1;
    })), this._disposers.push(e.events.on("globalpointerup", () => {
      this._endDown && (this.setPrivateRaw("isBusy", !1), this._released()), this._endDown = !1;
    })), this._disposers.push(i.events.on("globalpointerup", () => {
      this._thumbDown && (this.setPrivateRaw("isBusy", !1), this._released()), this._thumbDown = !1;
    })), this._disposers.push(t.on("x", () => {
      this._updateThumb();
    })), this._disposers.push(e.on("x", () => {
      this._updateThumb();
    })), this._disposers.push(t.on("y", () => {
      this._updateThumb();
    })), this._disposers.push(e.on("y", () => {
      this._updateThumb();
    })), this._disposers.push(i.events.on("positionchanged", () => {
      this._updateGripsByThumb();
    })), this.get("orientation") == "vertical" ? (t.set("x", 0), e.set("x", 0), this._disposers.push(i.adapters.add("y", (s) => Math.max(Math.min(Number(s), this.height() - i.height()), 0))), this._disposers.push(i.adapters.add("x", (s) => this.width() / 2)), this._disposers.push(t.adapters.add("x", (s) => this.width() / 2)), this._disposers.push(e.adapters.add("x", (s) => this.width() / 2)), this._disposers.push(t.adapters.add("y", (s) => Math.max(Math.min(Number(s), this.height()), 0))), this._disposers.push(e.adapters.add("y", (s) => Math.max(Math.min(Number(s), this.height()), 0)))) : (t.set("y", 0), e.set("y", 0), this._disposers.push(i.adapters.add("x", (s) => Math.max(Math.min(Number(s), this.width() - i.width()), 0))), this._disposers.push(i.adapters.add("y", (s) => this.height() / 2)), this._disposers.push(t.adapters.add("y", (s) => this.height() / 2)), this._disposers.push(e.adapters.add("y", (s) => this.height() / 2)), this._disposers.push(t.adapters.add("x", (s) => Math.max(Math.min(Number(s), this.width()), 0))), this._disposers.push(e.adapters.add("x", (s) => Math.max(Math.min(Number(s), this.width()), 0))));
  }
  _updateChildren() {
    super._updateChildren(), (this.isDirty("end") || this.isDirty("start") || this._sizeDirty) && this.updateGrips();
  }
  _changed() {
    if (super._changed(), this.isDirty("start") || this.isDirty("end")) {
      const t = "rangechanged";
      this.events.isEnabled(t) && this.events.dispatch(t, { type: t, target: this, start: this.get("start", 0), end: this.get("end", 1), grip: this._gripDown });
    }
  }
  _released() {
    const t = "released";
    this.events.isEnabled(t) && this.events.dispatch(t, { type: t, target: this });
  }
  /**
   * @ignore
   */
  updateGrips() {
    const t = this.startGrip, e = this.endGrip, i = this.get("orientation"), r = this.height(), s = this.width();
    i == "vertical" ? (t.set("y", r * this.get("start", 0)), e.set("y", r * this.get("end", 1))) : (t.set("x", s * this.get("start", 0)), e.set("x", s * this.get("end", 1)));
    const a = this.getPrivate("positionTextFunction"), h = Math.round(this.get("start", 0) * 100), o = Math.round(this.get("end", 0) * 100);
    let d, n;
    a ? (d = a.call(this, this.get("start", 0)), n = a.call(this, this.get("end", 0))) : (d = h + "%", n = o + "%"), t.set("ariaLabel", this._t("From %1", void 0, d)), t.set("ariaValueNow", "" + h), t.set("ariaValueText", h + "%"), t.set("ariaValueMin", "0"), t.set("ariaValueMax", "100"), e.set("ariaLabel", this._t("To %1", void 0, n)), e.set("ariaValueNow", "" + o), e.set("ariaValueText", o + "%"), e.set("ariaValueMin", "0"), e.set("ariaValueMax", "100");
  }
  _updateThumb() {
    const t = this.thumb, e = this.startGrip, i = this.endGrip, r = this.height(), s = this.width();
    let a = e.x(), h = i.x(), o = e.y(), d = i.y(), n = 0, u = 1;
    this.get("orientation") == "vertical" ? g(o) && g(d) && (!this._thumbBusy && !t.isDragging() && (t.set("height", d - o), t.set("y", o)), n = o / r, u = d / r) : g(a) && g(h) && (!this._thumbBusy && !t.isDragging() && (t.set("width", h - a), t.set("x", a)), n = a / s, u = h / s), this.getPrivate("isBusy") && (this.get("start") != n || this.get("end") != u) && (this.set("start", n), this.set("end", u));
    const p = this.getPrivate("positionTextFunction"), l = Math.round(this.get("start", 0) * 100), f = Math.round(this.get("end", 0) * 100);
    let _, m;
    p ? (_ = p.call(this, this.get("start", 0)), m = p.call(this, this.get("end", 0))) : (_ = l + "%", m = f + "%"), t.set("ariaLabel", this._t("From %1 to %2", void 0, _, m)), t.set("ariaValueNow", "" + l), t.set("ariaValueText", l + "%");
  }
  _updateGripsByThumb() {
    const t = this.thumb, e = this.startGrip, i = this.endGrip;
    if (this.get("orientation") == "vertical") {
      const r = t.height();
      e.set("y", t.y()), i.set("y", t.y() + r);
    } else {
      const r = t.width();
      e.set("x", t.x()), i.set("x", t.x() + r);
    }
  }
}
c.className = "Scrollbar";
c.classNames = y.classNames.concat([c.className]);
export {
  c as S
};
