/* COPYRIGHT Esri - https://js.arcgis.com/5.1/LICENSE.txt */
import { c as $ } from "../../chunks/runtime.js";
import { css as M, html as c } from "lit";
import { LitElement as _, createEvent as C, safeClassMap as h, nothing as v, safeStyleMap as u } from "@arcgis/lumina";
import { formatNumber as z } from "@arcgis/core/intl.js";
const n = {
  container: "container",
  ticksContainer: "ticks-container",
  labelsContainer: "labels-container",
  tick: "tick",
  tickMin: "tick--min",
  tickMax: "tick--max",
  tickLabel: "tick-label",
  baseline: "baseline"
}, A = M`:host{display:flex;--arcgis-ticks-tick-length: .5rem;--arcgis-ticks-tick-color: var(--calcite-color-border-input);--arcgis-ticks-gap: 1em;--arcgis-ticks-font-size: var(--calcite-font-size--2)}.container{align-items:stretch;display:flex;color:var(--calcite-color-text-2);flex:1;font-weight:var(--calcite-font-weight-medium);font-size:var(--arcgis-ticks-font-size);line-height:1em;position:relative;user-select:none;word-wrap:normal;word-break:normal}.baseline{background-color:var(--arcgis-ticks-tick-color, var(--calcite-color-border-input))}.tick{position:absolute;background-color:var(--arcgis-ticks-tick-color, var(--calcite-color-border-input))}.tick-label{position:absolute;text-align:var(--arcgis-ticks-label-text-align, center);width:var(--arcgis-ticks-label-width, var(--labels-container-width, "auto"))}:host([layout="horizontal"]){.ticks-container{height:var(--arcgis-ticks-tick-length)}.labels-container{height:var(--labels-container-height, "auto")}.tick{width:.125rem;height:var(--arcgis-ticks-tick-length);transform:translate(-50%)}.tick-label{transform:translate(-50%)}.tick--min{transform:unset}.tick--max{transform:translate(-100%)}}:host([layout="vertical"]){.ticks-container{width:var(--arcgis-ticks-tick-length)}.labels-container{width:var(--labels-container-width)}.tick{width:var(--arcgis-ticks-tick-length);height:.125rem;transform:translateY(50%)}.tick-label{text-align:right;transform:translateY(50%)}.tick--min{transform:unset}.tick--max{transform:translateY(100%)}}:host([layout="horizontal"][mirrored]){.tick,.tick-label{transform:translate(50%)}.tick--min{transform:unset}.tick--max{transform:translate(100%)}}:host([layout="vertical"][mirrored]){.tick,.tick-label{transform:translateY(-50%)}.tick--min{transform:unset}.tick--max{transform:translateY(-100%)}}:host([layout="horizontal"][label-placement="start"]){.container{flex-direction:column;justify-content:flex-end}.labels-container{margin-bottom:var(--arcgis-ticks-gap)}}:host([layout="horizontal"][label-placement="end"]){.container{flex-direction:column-reverse;justify-content:flex-end}.labels-container{margin-top:var(--arcgis-ticks-gap)}}:host([layout="vertical"][label-placement="start"]){.container{flex-direction:row;justify-content:flex-end}:dir(ltr){.labels-container{margin-right:var(--arcgis-ticks-gap)}.tick-label{text-align:right}}:dir(rtl){.labels-container{margin-left:var(--arcgis-ticks-gap)}.tick-label{text-align:left;unicode-bidi:plaintext}}}:host([layout="vertical"][label-placement="end"]){.container{flex-direction:row-reverse;justify-content:flex-end}:dir(ltr){.labels-container{margin-left:var(--arcgis-ticks-gap)}.tick-label{text-align:left}}:dir(rtl){.labels-container{margin-right:var(--arcgis-ticks-gap)}.tick-label{text-align:right;unicode-bidi:plaintext}}}:host([interactive]){pointer-events:none;.tick,.tick-label{pointer-events:auto;cursor:pointer}.tick-label:hover{text-decoration:underline}}`;
class F extends _ {
  constructor() {
    super(...arguments), this._formatValue = (e) => z(e, this._formatOptions), this._formatOptions = {
      style: "decimal",
      useGrouping: !0,
      minimumFractionDigits: 0,
      maximumFractionDigits: 2
    }, this.interactive = !1, this.interpolationExponent = 0, this.labelPlacement = "end", this.layout = "horizontal", this.max = 100, this.min = 0, this.mirrored = !1, this.mode = "count", this.showLabels = !1, this.showBaseline = !1, this.values = [], this.arcgisTickClick = C({ cancelable: !1 });
  }
  static {
    this.properties = { _formatOptions: 16, interactive: 7, interpolationExponent: 9, labelFormatter: 0, labelPlacement: 3, layout: 3, max: 9, min: 9, mirrored: 7, mode: 1, showLabels: 5, showBaseline: 5, values: [9, { converter: {
      fromAttribute(e) {
        if (e.includes(","))
          return e.split(",").filter((t) => t.trim() !== "").map(parseFloat).filter((t) => Number.isFinite(t));
        {
          const t = parseFloat(e);
          return Number.isFinite(t) ? [t] : [];
        }
      }
    } }] };
  }
  static {
    this.styles = A;
  }
  updated() {
    const e = this.renderRoot.querySelector(`.${n.labelsContainer}`);
    if (e) {
      let t = 0, i = 0;
      for (const r of this.renderRoot.querySelectorAll(`.${n.tickLabel}`)) {
        const a = r.getBoundingClientRect();
        t = Math.max(t, a.width), i = Math.max(i, a.height);
      }
      t = Math.ceil(t), i = Math.ceil(i), !t || !i ? (e.style.removeProperty("--labels-container-width"), e.style.removeProperty("--labels-container-height")) : (e.style.setProperty("--labels-container-width", `${t}px`), e.style.setProperty("--labels-container-height", `${i}px`));
    }
  }
  _handleClick(e) {
    if (!this.interactive)
      return;
    const i = e.target.dataset.value;
    i && this.arcgisTickClick.emit({ value: Number(i) });
  }
  _internalLabelFormatter(e) {
    const t = this.labelFormatter;
    if (!t)
      return this._formatValue(e);
    const i = t(e, this._formatValue);
    return i ?? this._formatValue(e);
  }
  render() {
    return c`<div class=${h(n.container)} touch-action=none @click=${this._handleClick}>${this._renderContent()}${this._renderBaseline()}</div>`;
  }
  _renderContent() {
    const { interpolationExponent: e, max: t, min: i, mode: r, values: a, layout: o, mirrored: l, showLabels: x } = this, g = [], b = [];
    for (const [f, d] of L(r, i, t, e, a)) {
      const p = { [o === "horizontal" ? l ? "right" : "left" : l ? "top" : "bottom"]: `${f}%` }, w = {
        [n.tick]: !0,
        [n.tickMin]: f === 0,
        [n.tickMax]: f === 100
      };
      g.push(c`<div class=${h(w)} style=${u(p)} data-value=${d ?? v}></div>`), x && b.push(c`<div class=${h(n.tickLabel)} style=${u(p)} data-value=${d ?? v}>${this._internalLabelFormatter(d)}</div>`);
    }
    return [c`<div class=${h(n.labelsContainer)}>${b}</div>`, c`<div class=${h(n.ticksContainer)}>${g}</div>`];
  }
  _renderBaseline() {
    const { showBaseline: e, layout: t } = this;
    return e && c`<div class=${h(n.baseline)} style=${u({
      [t === "horizontal" ? "width" : "height"]: "100%",
      [t === "horizontal" ? "height" : "width"]: "0.125rem"
    })}></div>` || "";
  }
}
function* L(s, e, t, i, r) {
  switch (s) {
    case "value": {
      const a = Array.isArray(r) ? r : [r];
      for (const o of a) {
        const l = P(e, t, o, i) * 100;
        l >= 0 && l <= 100 && (yield [l, o]);
      }
      break;
    }
    case "percent": {
      if (Array.isArray(r))
        for (const a of r)
          a >= 0 && a <= 100 && (yield [a, m(e, t, a / 100, i)]);
      else if (r > 0)
        for (let a = 0; a <= 100; a += r)
          yield [a, m(e, t, a / 100, i)];
      break;
    }
    case "count": {
      const a = Array.isArray(r) ? r[0] : r;
      if (a <= 1)
        yield [50, m(e, t, 0.5, i)];
      else
        for (let o = 0; o < a; o++) {
          const l = o * 100 / (a - 1);
          yield [l, m(e, t, l / 100, i)];
        }
      break;
    }
  }
}
function k(s, e, t) {
  return t < s ? s : t > e ? e : t;
}
function y(s, e, t) {
  return s + (e - s) * t;
}
function P(s, e, t, i = 0) {
  let r = (t - s) / (e - s);
  if (r = k(0, 1, r), Math.abs(i) < 1e-9)
    return r;
  const a = 1 + Math.expm1(i) * r;
  return r = Math.log(a) / i, k(0, 1, r);
}
function m(s, e, t, i = 0) {
  if (t = k(0, 1, t), Math.abs(i) < 1e-9)
    return y(s, e, t);
  const r = Math.expm1(i * t) / Math.expm1(i);
  return y(s, e, r);
}
$("arcgis-ticks", F);
export {
  F as ArcgisTicks
};
