import { A, m as w, e as P, V as k, a as v, t as L, u as F } from "./am-chart.H2jnOzFc.js";
import { E as S } from "./EditableLabel.BWeq6fgX.js";
class T extends A {
  constructor() {
    super(...arguments), this._frequency = 1, this._itemMap = {};
  }
  _afterNew() {
    this._settings.themeTags = w(this._settings.themeTags, ["axis"]), this.fields.push("category", "id", "cellSize"), this.setPrivateRaw("name", "category"), this.addTag("category"), super._afterNew();
  }
  _afterDataChange() {
    super._afterDataChange();
    const t = this.dataItems.length;
    t > 0 && this.setPrivateRaw("maxZoomFactor", t), this.setPrivateRaw("startIndex", Math.min(this.getPrivate("startIndex", 0), t)), this.setPrivateRaw("endIndex", Math.min(this.getPrivate("endIndex", t), 1));
  }
  _prepareChildren() {
    super._prepareChildren();
    const t = this.dataItems.length;
    let e = 0;
    this._valuesDirty && (this._itemMap = {}, P(this.dataItems, (n) => {
      n.setRaw("index", e), this._itemMap[n.get("category")] = n, e++;
    }), this.setPrivateRaw("maxZoomFactor", t));
    let a = this.get("start", 0), i = this.get("end", 1), s = this._getIndices(a, i);
    this.setPrivateRaw("startIndex", s.startIndex), this.setPrivateRaw("endIndex", s.endIndex), (this._sizeDirty || this._valuesDirty || this.isDirty("start") || this.isDirty("end") || this.isPrivateDirty("endIndex") || this.isPrivateDirty("startIndex") || this.isPrivateDirty("width") || this.isPrivateDirty("height")) && this.dataItems.length > 0 && (this._handleRangeChange(), this._prepareAxisItems(), this._updateAxisRanges());
  }
  /**
   * @ignore
   */
  adjustZoom() {
    const t = this.dataItems.length;
    if (t > 1) {
      let e = this.get("maxZoomCount", this.dataItems.length), a = this.get("minZoomCount", 1), i = 0, s = this.getPrivate("startIndex", 0), n = this.getPrivate("endIndex", 0);
      const d = this._sAnimation, r = this._eAnimation;
      let l = this.get("start", 0), o = !1;
      d && !d.stopped && (l = Number(d.to), o = !0);
      let m = this.get("end", 1);
      if (r && !r.stopped && (m = Number(r.to), o = !0), o) {
        const h = this._getIndices(l, m);
        s = h.startIndex, n = h.endIndex;
      }
      for (let h = s; h < n; h++) {
        const g = this.dataItems[h];
        i += g.get("finalCellSize", 1);
      }
      if (i <= a) {
        let h = 0;
        for (let g = s; g < t; g++) {
          const I = this.dataItems[g];
          if (h += I.get("finalCellSize", 1), n = g + 1, h >= a)
            break;
        }
        if (h < a)
          for (let g = s - 1; g >= 0; g--) {
            const I = this.dataItems[g];
            if (h += I.get("finalCellSize", 1), s = g, h >= a)
              break;
          }
        this.zoomToIndexes(s, n);
      } else if (i >= e) {
        let h = i;
        for (let g = n - 1; g >= s; g--) {
          const I = this.dataItems[g];
          if (h -= I.get("finalCellSize", 1), h <= e) {
            n = g + 1;
            break;
          }
        }
        this.zoomToIndexes(s, n);
      }
    }
  }
  _handleRangeChange() {
    P(this.series, (t) => {
      let e = this.dataItems[this.startIndex()].get("category"), a = this.dataItems[this.endIndex() - 1].get("category"), i = t.get("baseAxis"), s = t.get("xAxis"), n = t.get("yAxis");
      if (s instanceof T && n instanceof T)
        t._markDirtyAxes();
      else if (i === this) {
        let d, r, l = n;
        if (s === i ? (t.get("categoryXField") && (d = "categoryX"), t.get("openCategoryXField") && (r = "openCategoryX")) : n === i && (t.get("categoryYField") && (d = "categoryY"), t.get("openCategoryYField") && (r = "openCategoryY"), l = s), l instanceof k && (d || r)) {
          let h = function(x, p, f, _, R, z) {
            let c, D = p.categoryToIndex(R);
            for (; !c && D >= 0 && D < p.dataItems.length; ) {
              let C = p.dataItems[D].get("category");
              if (z == "previous")
                for (let u = x.dataItems.length - 1; u >= 0; u--) {
                  let y = x.dataItems[u];
                  if (f && y.get(f) === C) {
                    c = y;
                    break;
                  }
                  if (_ && y.get(_) === C) {
                    c = y;
                    break;
                  }
                }
              else
                for (let u = 0, y = x.dataItems.length; u < y; u++) {
                  let M = x.dataItems[u];
                  if (f && M.get(f) === C) {
                    c = M;
                    break;
                  }
                  if (_ && M.get(_) === C) {
                    c = M;
                    break;
                  }
                }
              c || (D += z === "previous" ? -1 : 1);
            }
            return c;
          }, o, m;
          o = h(t, this, d, r, e, "next"), m = h(t, this, d, r, a, "previous");
          let g = 0, I = t.dataItems.length;
          o && (g = t.dataItems.indexOf(o)), m && (I = t.dataItems.indexOf(m) + 1), t.setPrivate("startIndex", g), t.setPrivate("endIndex", I);
          let b = !1;
          for (let x = g; x < I; x++) {
            const p = t.dataItems[x];
            if (P(t.__valueXShowFields, (f) => {
              p.get(f) != null && (b = !0);
            }), P(t.__valueYShowFields, (f) => {
              p.get(f) != null && (b = !0);
            }), b)
              break;
          }
          t.setPrivate("outOfSelection", !b);
        }
        t._markDirtyAxes();
      }
    });
  }
  _prepareAxisItems() {
    var t;
    const e = this.get("renderer"), a = this.dataItems.length;
    let i = this.startIndex();
    i > 0 && i--;
    let s = this.endIndex();
    s < a && s++;
    const n = e.get("minorLabelsEnabled"), d = e.get("minorGridEnabled", n);
    let r = e.axisLength() / Math.max(e.get("minGridDistance"), 1), l = Math.max(1, Math.min(a, Math.ceil((s - i) / r)));
    i = Math.floor(i / l) * l, this._frequency = l;
    for (let m = 0; m < a; m++)
      this._toggleDataItem(this.dataItems[m], !1);
    let o = this.dataItems[i].get("index", 0);
    for (let m = i; m < s; m = m + l) {
      let h = this.dataItems[m];
      this._createAssets(h, []), this._toggleDataItem(h, !0);
      let g = l;
      d && (g = 1), this._prepareDataItem(h, o, g), o++;
    }
    if (e.get("minorGridEnabled"))
      for (let m = i; m < s; m++) {
        let h = this.dataItems[m];
        m % l != 0 && (this._createAssets(h, ["minor"], !0), this._toggleDataItem(h, !0), this._prepareDataItem(h, 0, 1), n || (t = h.get("label")) === null || t === void 0 || t.setPrivate("visible", !1));
      }
    this._updateGhost();
  }
  _prepareDataItem(t, e, a) {
    let i = this.get("renderer"), s = t.get("categoryLocation", 0), n = t.get("endCategoryLocation", 1), d = t.get("index");
    v(d) || (d = this.categoryToIndex(t.get("category")));
    let r = this.indexToPosition(d, s), l = t.get("endCategory"), o;
    l ? (o = this.categoryToIndex(l), v(o) || (o = d)) : o = d;
    let m = this.indexToPosition(o, n), h, g;
    if (t.get("isRange")) {
      if (h = o, !v(d)) {
        this._toggleDataItem(t, !1);
        return;
      }
    } else
      h = d + this._frequency - 1;
    g = this.indexToPosition(h, n), i.updateLabel(t.get("label"), r, m, a), i.updateGrid(t.get("grid"), r, m), i.updateTick(t.get("tick"), r, m, a), i.updateFill(t.get("axisFill"), r, g), this._processBullet(t), i.updateBullet(t.get("bullet"), r, m);
    const I = this.get("fillRule");
    I && I(t, e);
  }
  startIndex() {
    let t = this.dataItems.length;
    return Math.min(Math.max(this.getPrivate("startIndex", 0), 0), t - 1);
  }
  endIndex() {
    let t = this.dataItems.length;
    return Math.max(1, Math.min(this.getPrivate("endIndex", t), t));
  }
  /**
   * @ignore
   */
  baseValue() {
  }
  /**
   * @ignore
   */
  basePosition() {
    return 0;
  }
  /**
   * Returns X coordinate in pixels corresponding to specific category index.
   *
   * @param   value  Index
   * @return         X coordinate
   */
  getX(t) {
    let e = this._itemMap[t];
    return e ? this._settings.renderer.positionToCoordinate(this.indexToPosition(e.get("index", 0))) : NaN;
  }
  /**
   * Returns Y coordinate in pixels corresponding to specific category index.
   *
   * @param   value  Index
   * @return         Y coordinate
   */
  getY(t) {
    let e = this._itemMap[t];
    return e ? this._settings.renderer.positionToCoordinate(this.indexToPosition(e.get("index", 0))) : NaN;
  }
  /**
   * @ignore
   */
  getDataItemPositionX(t, e, a, i) {
    const s = t.get(e), n = this._itemMap[s];
    return n ? this.indexToPosition(n.get("index", 0), a) : NaN;
  }
  /**
   * @ignore
   */
  getDataItemCoordinateX(t, e, a, i) {
    return this._settings.renderer.positionToCoordinate(this.getDataItemPositionX(t, e, a, i));
  }
  /**
   * @ignore
   */
  getDataItemPositionY(t, e, a, i) {
    const s = t.get(e), n = this._itemMap[s];
    return n ? this.indexToPosition(n.get("index", 0), a) : NaN;
  }
  /**
   * @ignore
   */
  getDataItemCoordinateY(t, e, a, i) {
    return this._settings.renderer.positionToCoordinate(this.getDataItemPositionY(t, e, a, i));
  }
  /**
   * Converts category index to a relative position.
   *
   * `location` indicates relative position within category: 0 - start, 1 - end.
   *
   * If not set, will use middle (0.5) of the category.
   *
   * @param   index     Index
   * @param   location  Location
   * @return            Index
   */
  indexToPosition(t, e, a) {
    let i = this.dataItems.length, s = 0;
    if (i == 0 || (t >= i && (t = i - 1, e = 1), v(e) || (e = 0.5), !v(t)))
      return 0;
    let n = this.get("startLocation", 0), d = this.get("endLocation", 1);
    if (!this.get("cellSizeField"))
      i -= n, i -= 1 - d, s = (t + e - n) / i;
    else {
      let r = a ? "finalCellSize" : "cellSize";
      const l = this.dataItems;
      let o = 0;
      P(this.dataItems, (g) => {
        o += g.get(r, 1);
      }), o -= n * l[0].get(r, 1), o -= (1 - d) * l[l.length - 1].get(r, 1);
      let m = 0;
      for (let g = 0; g < t; g++)
        m += l[g].get(r, 1);
      let h = l[t].get(r, 1);
      s = (m + e * h - n * l[0].get(r, 1)) / o;
    }
    if (!a) {
      let r = this.dataItems[t];
      r && (s += r.get("deltaPosition", 0));
    }
    return s;
  }
  /**
   * Returns a position of a category.
   *
   * @param   category  Category to look up
   * @return            Position
   */
  categoryToPosition(t) {
    let e = this._itemMap[t];
    return e ? this.indexToPosition(e.get("index")) : NaN;
  }
  /**
   * Returns an index of a category.
   *
   * @param   category  Category to look up
   * @return            Index
   */
  categoryToIndex(t) {
    let e = this._itemMap[t];
    return e ? e.get("index") : NaN;
  }
  /**
   * @ignore
   */
  dataItemToPosition(t) {
    return this.indexToPosition(t.get("index"));
  }
  /**
   * @ignore
   */
  roundAxisPosition(t, e) {
    return t += (0.5 - e) / this.dataItems.length, this.indexToPosition(this.axisPositionToIndex(t), e);
  }
  /**
   * Returns an index of the category that corresponds to specific pixel
   * position within axis.
   *
   * @param position  Position (px)
   * @return Category index
   */
  axisPositionToIndex(t) {
    let e = this.dataItems.length;
    if (e === 0)
      return 0;
    if (this.get("cellSizeField")) {
      let a = 0, i = [];
      P(this.dataItems, (l) => {
        const o = l.get("cellSize", 1);
        i.push(o), a += o;
      });
      let s = this.get("startLocation", 0), n = this.get("endLocation", 1);
      a -= s, a -= 1 - n;
      let d = t * a + s, r = 0;
      for (let l = 0; l < e; l++) {
        const o = i[l];
        if (d < r + o)
          return l;
        r += o;
      }
      return e - 1;
    } else
      return L(Math.floor(t * e), 0, e - 1);
  }
  /**
   * Returns text to be used in an axis tooltip for specific relative position.
   *
   * @param   position  Position
   * @return            Tooltip text
   */
  getTooltipText(t, e) {
    const a = this.dataItems[this.axisPositionToIndex(t)];
    if (a) {
      const i = a.get("label");
      if (i)
        return F(i, this.get("tooltipText", ""));
    }
  }
  _updateTooltipText(t, e) {
    t._setDataItem(this.dataItems[this.axisPositionToIndex(e)]), t.label.text.markDirtyText();
  }
  /**
   * Returns a data item from series that is closest to the `position`.
   *
   * @param   series    Series
   * @param   position  Relative position
   * @return            Data item
   */
  getSeriesItem(t, e) {
    if (this.dataItems.length > 0) {
      let a = this.getPrivate("name") + this.get("renderer").getPrivate("letter"), i = this.axisPositionToIndex(e), s = t.dataItems[i], n = this.dataItems[i], d = n.get("category");
      if (s && n && s.get(a) === d)
        return s;
      for (let r = 0, l = t.dataItems.length; r < l; r++) {
        let o = t.dataItems[r];
        if (o.get(a) === d)
          return o;
      }
    }
  }
  _getIndices(t, e) {
    let a = this.dataItems.length, i = 0, s = a, n = "cellSize";
    if (this.get("cellSizeField")) {
      let d = 0;
      const r = this.dataItems;
      if (r.length == 0)
        return { startIndex: 0, endIndex: 0 };
      P(r, (o) => {
        d += o.get(n, 1);
      }), d -= this.get("startLocation", 0) * r[0].get(n, 1), d -= (1 - this.get("endLocation", 1)) * r[r.length - 1].get(n, 1);
      let l = 0;
      for (let o = 0; o < a; o++)
        if (l += this.dataItems[o].get(n, 1), Math.round(l) > Math.round(t * d)) {
          i = o;
          break;
        }
      for (let o = i + 1; o < a; o++)
        if (l += this.dataItems[o].get(n, 1), Math.round(l) >= Math.round(e * d)) {
          s = o + 1;
          break;
        }
      i = Math.max(i, 0), s = Math.min(s, a);
    } else
      i = Math.max(Math.round(this.get("start", 0) * a), 0), s = Math.min(Math.round(this.get("end", 1) * a), a);
    return { startIndex: i, endIndex: s };
  }
  /**
   * Zooms the axis to specific `start` and `end` indexes.
   *
   * Optional `duration` specifies duration of zoom animation in milliseconds.
   *
   * @param  start     Start index
   * @param  end       End index
   * @param  duration  Duration in milliseconds
   */
  zoomToIndexes(t, e, a) {
    let i = this.dataItems.length;
    this.get("cellSizeField") ? (t = Math.min(Math.max(t, 0), i), e = Math.max(Math.min(e, i), 1), this.setPrivateRaw("startIndex", t), this.setPrivateRaw("endIndex", e), this.zoom(this.indexToPosition(t, 0, !0), this.indexToPosition(e, 0, !0), a)) : this.zoom(t / i, e / i, a);
  }
  zoomToCategories(t, e, a) {
    this.zoomToIndexes(this.categoryToIndex(t), this.categoryToIndex(e) + 1, a);
  }
  /**
   * Returns position span between start and end of a single cell in axis.
   *
   * @since 5.2.30
   * @return Position
   */
  getCellWidthPosition() {
    return this._frequency / this.dataItems.length / (this.get("end", 1) - this.get("start", 0));
  }
  /**
   * @ignore
   */
  nextPosition(t) {
    t == null && (t = 1), this.get("renderer").getPrivate("letter") == "Y" && (t *= -1);
    const e = this.getPrivate("tooltipPosition", 0), a = L(this.axisPositionToIndex(e) + t, 0, this.dataItems.length - 1);
    return this.toGlobalPosition(this.indexToPosition(a));
  }
}
T.className = "CategoryAxis";
T.classNames = A.classNames.concat([T.className]);
class N extends S {
  constructor() {
    super(...arguments), this._tickPoints = [];
  }
}
N.className = "EditableAxisLabel";
N.classNames = S.classNames.concat([N.className]);
export {
  T as C,
  N as E
};
