import { c as I } from "../../chunks/runtime.js";
import { u as F } from "../../chunks/useT9n.js";
import { substitute as S } from "@arcgis/core/intl.js";
import { LitElement as k, safeClassMap as p, nothing as u, safeStyleMap as w } from "@arcgis/lumina";
import { css as R, html as L, svg as b } from "lit";
/*! All material copyright Esri, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/5.0/esri/copyright.txt for details.
v5.0.0-next.137 */
const f = {
  content: "content",
  svg: "svg",
  label: "label",
  dataLine: "data-line",
  averageDataLine: "average-data-line",
  dataLineSymbol: "data-line-symbol"
}, z = R`:host{display:flex;--arcgis-histogram-bar-color: var(--calcite-color-border-1);--arcgis-histogram-stop-color: color-mix(in srgb, var(--calcite-color-brand) 80%, transparent)}.content{display:flex;flex:1;direction:ltr}.svg{display:inline-block;flex:1;overflow:hidden}.data-line{stroke:#888;stroke-width:1px;shape-rendering:crispEdges}.average-data-line{stroke-width:1px;shape-rendering:crispEdges;stroke:#444}.label,.data-line-symbol{font-size:12px;stroke:#fff;stroke-width:2px;paint-order:stroke;unicode-bidi:plaintext}.data-line-symbol{font-family:"Georgia, serif",sans-serif;font-style:italic}`;
function O(t, i, n, e, r) {
  const [s, a] = r;
  if (!t || a === 0 || s === 0 || n == null || i == null)
    return null;
  const o = n - i, c = (t.length > 1 ? t[t.length - 1].maxValue - t[0].minValue : 0) / o, [h, g] = e === "vertical" ? [a * c, s] : [a, s * c], x = t ? t.map((d) => d.count) : [], m = Math.max(...x);
  return t.map((d) => {
    const [$, v] = e === "vertical" ? [h / t.length, m !== 0 ? d.count / m * g : 0] : [m !== 0 ? d.count / m * h : 0, g / t.length], { maxValue: M } = d, y = n - M, [_, E] = e === "vertical" ? [0, y * (a / o)] : [s - v - y * (s / o), a - $];
    return [_, E, v, $];
  });
}
function B(t, i, n, e, r) {
  const s = [];
  return t != null && s.push({
    value: t,
    symbol: "x̄",
    lineClass: f.averageDataLine
  }), i != null && n != null && e != null && s.push(...G(n, e, i, t)), r && s.push(...r), s;
}
function N(t, i, n, e, r) {
  if (n == null)
    return [0, 0, 0, 0];
  const s = Math.round((r - n) / e * 100) / 100, [a, o] = t;
  if (i === "vertical") {
    let c = o - s * o || 1;
    return c = Math.round(c) + 0.5, [0, "100%", c, c];
  }
  let l = s * a || 1;
  return l = Math.round(l) + 0.5, [l, l, "100%", 0];
}
function P(t, i, n, e, r) {
  if (n == null)
    return [0, 0];
  const s = Math.round((r - n) / e * 100) / 100, [a, o] = t;
  return i === "vertical" ? [a, o - s * o] : [0, s * a];
}
function T(t, i, n) {
  if (!t || t.length === 0)
    return null;
  if (C(t)) {
    const e = t.map((r) => ({
      ...r,
      color: D(r.color ?? n)
    }));
    return e.unshift({
      color: i,
      minValue: Number.NEGATIVE_INFINITY,
      maxValue: e[0].minValue
    }), e.push({
      color: i,
      minValue: e[e.length - 1].maxValue,
      maxValue: Number.POSITIVE_INFINITY
    }), e;
  } else
    return t.map((e) => ({
      ...e,
      color: D(e.color ?? n)
    }));
}
function A(t, i, n, e) {
  if (!t || t.length === 0)
    return n;
  if (C(t)) {
    const r = H(t, i);
    if (r.length === 0)
      return n;
    if (e) {
      const { color: s = n, weight: a } = r[0], { color: o = n, weight: l } = r[1] ?? r[0], c = a + l;
      return `color-mix(in srgb, ${s} ${a / c * 100}%, ${o} ${l / c * 100}%)`;
    }
    return r[0].color ?? n;
  }
  return W(t, i);
}
function H(t, i) {
  const n = [];
  for (const e of t) {
    const r = Math.max(0, Math.min(i.maxValue, e.maxValue) - Math.max(i.minValue, e.minValue));
    r > 0 && n.push({ color: e.color, weight: r });
  }
  return n.sort((e, r) => r.weight - e.weight);
}
function W(t, i) {
  const n = (i.maxValue - i.minValue) / 2 + i.minValue;
  if (n > t[t.length - 1].value)
    return t[t.length - 1].color;
  for (let e = 0; e < t.length - 1; e++) {
    const r = t[e], s = t[e + 1];
    if (n <= r.value)
      return r.color;
    if (n < s.value) {
      const a = (n - r.value) / (s.value - r.value) * 100;
      return `color-mix(in srgb, ${r.color}, ${s.color} ${a}%)`;
    }
  }
  return t[t.length - 1].color;
}
function C(t) {
  return t[0] != null && "minValue" in t[0];
}
function D(t) {
  if (!t)
    return;
  if (typeof t == "string")
    return t;
  if (Array.isArray(t))
    return `rgba(${t[0]}, ${t[1]}, ${t[2]}, ${t[3] ?? 1})`;
  const { r: i, g: n, b: e, a: r = 1 } = t;
  return `rgba(${i}, ${n}, ${e}, ${r})`;
}
function G(t, i, n, e) {
  const r = 0.06 * (i - t);
  return Y(n, e).filter((s) => Math.abs(e - s) > r).map((s, a, o) => {
    const l = o.length / 2, c = s > e ? "+" : "-", h = a >= l ? a - l + 1 : l - a;
    return {
      symbol: `${c}${h === 1 ? "" : h}σ`,
      value: s
    };
  });
}
function Y(t, i) {
  if (i == null || t == null)
    return [];
  const n = 1, e = [];
  for (let r = n * -1; r <= n; r++)
    r !== 0 && e.push(i + r * t);
  return e;
}
const V = "var(--arcgis-histogram-bar-color)", U = "var(--arcgis-histogram-stop-color)";
class j extends k {
  constructor() {
    super(...arguments), this._messages = F(), this._normalizedStops = null, this.containerDimensions = [0, 0], this.icon = "graph-histogram", this.layout = "horizontal", this.colorBlendingEnabled = !1;
  }
  static {
    this.properties = { containerDimensions: 16, colorStops: 0, icon: 3, average: 11, messageOverrides: 0, standardDeviation: 11, bins: 0, dataLines: 0, label: 1, labelFormatFunction: 0, layout: 3, max: 11, min: 11, colorBlendingEnabled: 5 };
  }
  static {
    this.styles = z;
  }
  loaded() {
    const i = new ResizeObserver((r) => {
      for (const s of r) {
        const { width: a, height: o } = s.contentRect;
        this.containerDimensions = [a, o];
      }
    }), { width: n, height: e } = this.el.getBoundingClientRect();
    this.containerDimensions = [n, e], i.observe(this.el);
  }
  willUpdate(i) {
    i.has("colorStops") && (this._normalizedStops = T(this.colorStops, V, U));
  }
  render() {
    const { label: i } = this;
    return this.containerDimensions ? L`<div role=img .ariaLabel=${i ?? void 0} class=${p(f.content)}>${this._renderContent()}</div>` : null;
  }
  _renderContent() {
    const { containerDimensions: [i, n] } = this, e = "bg-fill";
    return L`<svg class=${p(f.svg)} xmlns=http://www.w3.org/2000/svg>${b`<defs><clipPath id=${e}><rect height=${n ?? u} width=${i ?? u} x=0 y=0 /></clipPath></defs><g style=${w({ clipPath: `url(#${e})` })}><g>${this._renderBars()}</g></g><g>${this._renderLines()}</g>`}</svg>`;
  }
  _renderBars() {
    const { bins: i, _messages: n } = this, e = O(this.bins, this.min, this.max, this.layout, this.containerDimensions);
    return !e || !i ? null : e.map((r, s) => {
      const [a, o, l, c] = r, h = i[s], g = n?.barLabel ? S(n.barLabel, h) : "";
      return b`<rect style=${w({
        fill: A(this._normalizedStops, h, V, this.colorBlendingEnabled)
      })} .ariaLabel=${g} data-index=${s ?? u} height=${c ?? u} role=img shape-rendering=crispEdges stroke-width=0 width=${l ?? u} x=${a ?? u} y=${o ?? u} />`;
    });
  }
  _renderLines() {
    return B(this.average, this.standardDeviation, this.min, this.max, this.dataLines)?.map((n) => q({ dataLine: n, containerDimensions: this.containerDimensions, layout: this.layout, min: this.min, max: this.max, labelFormatFunction: this.labelFormatFunction }));
  }
}
const q = (t) => {
  const { containerDimensions: i, layout: n, min: e, max: r, dataLine: s, labelFormatFunction: a } = t, o = r != null && e != null ? r - e : 0, [l, c, h, g] = N(i, n, e, o, s.value), [x, m] = P(i, n, e, o, s.value), d = "symbol" in s ? [
    b`<tspan class=${p(f.dataLineSymbol)}>${s.symbol}</tspan>`,
    b`<tspan>${a ? a(s.value) : s.value}</tspan>`
  ] : s.label, $ = "lineClass" in s && s.lineClass ? s.lineClass : f.dataLine;
  return b`<g><title>${s.value}</title><line class=${p($)} shape-rendering=crispEdges x1=${l ?? u} x2=${c ?? u} y1=${h ?? u} y2=${g ?? u} /><text class=${p(f.label)} text-anchor=end transform=${(n === "horizontal" ? "rotate(270)" : void 0) ?? u} x=${x ?? u} y=${m - 2}>${d}</text></g>`;
};
I("arcgis-histogram", j);
export {
  j as ArcgisHistogram
};
