/* COPYRIGHT Esri - https://js.arcgis.com/5.0/LICENSE.txt */
import D from "@arcgis/core/Color.js";
import { createClassBreaksRenderer as E } from "@arcgis/core/smartMapping/renderers/color.js";
import { az as R, ah as x } from "./index.js";
import { compact as A, intersection as N, flatMap as O, sortBy as H } from "lodash-es";
import "@arcgis/toolkit/type";
import { W as V } from "./series-types.js";
import { a as P, c as K, f as $ } from "./rest-js-object-literals.js";
import { getSchemes as L } from "@arcgis/core/smartMapping/symbology/color.js";
import { b as W } from "./index3.js";
import * as j from "@arcgis/core/smartMapping/symbology/support/colorRamps.js";
function S(e, s, o) {
  const t = Math.max(0, Math.min(1, o));
  return [
    Math.round(e[0] + (s[0] - e[0]) * t),
    Math.round(e[1] + (s[1] - e[1]) * t),
    Math.round(e[2] + (s[2] - e[2]) * t),
    Math.round(e[3] + (s[3] - e[3]) * t)
  ];
}
function z(e) {
  const { colorRamp: s, numberOfClassBreaks: o, isFlipped: t } = e;
  let l = [], a = [], c = !1;
  if (s.colorsForClassBreaks && s.colorsForClassBreaks.length > 0) {
    const r = s.colorsForClassBreaks.find(
      (n) => n.numClasses === o
    );
    if (r?.colors?.length === o) {
      const n = r.colors.map((m) => g(m));
      n.every((m) => m !== void 0) && (a = t ? n.reverse() : n, c = !0);
    }
  }
  if (!c) {
    const r = s.colors || [];
    if (r.length > 0) {
      for (let n = 0; n < o; n++) {
        const i = o > 1 ? n / (o - 1) : 0, m = Math.floor(i * (r.length - 1)), d = Math.min(m + 1, r.length - 1), p = i * (r.length - 1) - m, u = g(r[m]), h = g(r[d]);
        if (!u || !h) {
          l = [];
          break;
        }
        const C = S(u, h, p);
        l.push(C);
      }
      a = l;
    }
  }
  return a;
}
function k(e) {
  const { numberOfClassBreaks: s, selectedColorRamp: o, colorRamp: t, isFlipped: l = !1 } = e;
  let a = [], c = [136, 65, 157, 255], r = [237, 248, 251, 255];
  if (t && t.length > 0 && t.length === s ? a = t : o && (a = z({ colorRamp: o, numberOfClassBreaks: s, isFlipped: l })), a.length !== s) {
    r = a[0] ?? t?.[0] ?? r, c = a.length > 1 && a[a.length - 1] !== void 0 ? a[a.length - 1] : c;
    const n = [r];
    if (s > 1) {
      for (let i = 1; i < s; i++) {
        const m = i / (s - 1), d = S(r, c, m);
        n.push(d);
      }
      n.push(c);
    }
    a = n;
  }
  return a;
}
function q(e, s = 2) {
  return `≤ ${((t) => {
    let l = "";
    return t % 1 === 0 ? l = t.toString() : l = t.toFixed(s), l;
  })(e)}`;
}
function re(e) {
  let s = "#000000";
  if (e && Array.isArray(e) && e.length >= 3) {
    const o = f(e[0] || 0), t = f(e[1] || 0), l = f(e[2] || 0), a = o.toString(16).padStart(2, "0"), c = t.toString(16).padStart(2, "0"), r = l.toString(16).padStart(2, "0");
    s = `#${a}${c}${r}`;
  }
  return s;
}
const le = {
  all: ["*"],
  light: ["light"],
  dark: ["dark"],
  redsAndYellows: ["oranges", "reds", "yellows"],
  greens: ["greens"],
  blues: ["blues"],
  purplesAndPinks: ["purples", "pinks"],
  browns: ["browns"],
  grays: ["grays"],
  bright: ["bright"],
  subdued: ["subdued"],
  colorblindFriendly: ["colorblind-friendly"]
}, ne = {
  all: "all"
}, G = (e) => {
  let s = 0;
  const o = (a) => {
    let c = 0;
    return a.forEach((r) => {
      const n = r.toString().split(".")[1];
      n && (c = Math.max(c, n.length));
    }), c;
  }, t = [...e].sort((a, c) => a - c), l = o(t);
  for (; s <= l && t.some(
    (c, r) => r > 0 && x(c, s) <= x(t[r - 1], s)
  ); )
    s++;
  return Math.min(s, l);
};
function f(e) {
  return Math.max(0, Math.min(255, Math.round(e)));
}
function g(e) {
  let s;
  if (e instanceof D) {
    const o = e.toRgba();
    s = [f(o[0]), f(o[1]), f(o[2]), f(o[3] * 255)];
  } else if (Array.isArray(e) && e.every((o) => typeof o == "number") && (e.length === 3 || e.length === 4)) {
    const o = e.length >= 4 ? e[3] ?? 255 : 255, t = f(o <= 1 ? o * 255 : o);
    return [f(e[0] ?? 0), f(e[1] ?? 0), f(e[2] ?? 0), t];
  }
  return s;
}
function Y(e, s) {
  const o = {
    exactN: 100,
    generated: 10,
    anyOverlap: 1
  }, t = (r) => r.join(","), l = (r) => A(r.map(g)), a = (r, n) => N([...r], [...n]).length;
  let c;
  if (e?.length) {
    const r = l(e.map((p) => p.symbol?.color)), n = new Set(r.map(t)), i = r.length, m = s.map((p) => {
      const u = new Set(
        l(k({ numberOfClassBreaks: i, selectedColorRamp: p })).map(t)
      ), h = a(n, u), C = p.colorsForClassBreaks?.find((y) => y.numClasses === i)?.colors ?? [], B = new Set(l(C).map(t)), M = a(n, B), v = [
        ...p.colors ?? [],
        ...O(p.colorsForClassBreaks ?? [], (y) => y.colors ?? [])
      ], I = new Set(l(v).map(t)), w = a(n, I), T = M * o.exactN + h * o.generated + w * o.anyOverlap;
      return { ramp: p, score: T };
    }), d = H(m, [
      (p) => -p.score,
      // descending score
      (p) => p.ramp.name
      // ascending name
    ]);
    d[0]?.score > 0 && (c = d[0].ramp);
  }
  return c;
}
function ce(e, s) {
  let o = !0;
  if (!e?.length || s.length < 2)
    o = !1;
  else {
    const t = e.map(g), l = s.map((a) => a.symbol.color);
    if (t.length !== l.length)
      o = !1;
    else {
      const a = (n, i) => !!n && !!i && n.length === i.length && n.every((m, d) => m === i[d]);
      let c = 0, r = l.length - 1;
      for (; c < t.length && r >= 0; ) {
        if (!a(t[c], l[r])) {
          o = !1;
          break;
        }
        c++, r--;
      }
    }
  }
  return o;
}
function ie(e) {
  const { numberOfClassBreaks: s, selectedColorRamp: o, existingClassBreakInfos: t, isFlipped: l } = e;
  return k({
    numberOfClassBreaks: s,
    selectedColorRamp: o,
    colorRamp: t?.map((a) => a.symbol?.color),
    isFlipped: l
  });
}
function me(e) {
  const { classBreakInfos: s, colors: o } = e, t = [];
  for (let l = 0; l < s.length; l++) {
    const a = s[l], c = !!a.symbol, r = o[l] ?? a.symbol?.color, n = c && r ? {
      ...a,
      symbol: {
        ...a.symbol,
        color: r
      }
    } : a;
    t.push(n);
  }
  return t;
}
async function J(e) {
  const { data: s, breaksCount: o, classificationMethod: t, layer: l, xAxisField: a, colorsForClassBreaks: c } = e;
  let r = [];
  const n = await W({
    categoryField: a ?? "",
    categoryType: V.DateAxisFormat,
    numericFields: [R.valueField],
    inlineData: s
  });
  if (n.layer && l) {
    const m = L({ geometryType: "point", theme: "high-to-low" })?.primaryScheme, d = await E({
      layer: n.layer,
      field: R.valueField,
      classificationMethod: t,
      numClasses: o,
      colorScheme: m
    });
    if (d?.classBreaksResult?.classBreakInfos?.length) {
      const p = G(
        d.classBreaksResult.classBreakInfos.map((u) => u.maxValue)
      );
      r = d.classBreaksResult.classBreakInfos.map((u, h) => ({
        classMinValue: u.minValue,
        classMaxValue: u.maxValue,
        label: q(u.maxValue, p),
        symbol: {
          type: $.SFS,
          style: K.Solid,
          color: c?.[h] || [0, 0, 0, 255],
          outline: {
            style: P.Solid,
            width: 0.5,
            color: [50, 50, 50, 0.6]
          }
        }
      }));
    }
  }
  return r;
}
function Q(e) {
  const { selectedColorRamp: s, existingClassBreakInfos: o, availableColorRamps: t } = e;
  if (s)
    return s;
  const l = t?.length ? t : F.esriColorRamps;
  return (o?.length ? Y(o, l) : void 0) ?? F.defaultColorRampForCharts;
}
async function pe(e) {
  const {
    data: s,
    breaksCount: o,
    classificationMethod: t,
    layer: l,
    xAxisField: a,
    selectedColorRamp: c,
    availableColorRamps: r,
    existingClassBreakInfos: n,
    useExistingColors: i,
    isFlipped: m
  } = e, d = Q({
    selectedColorRamp: c,
    existingClassBreakInfos: n,
    availableColorRamps: r
  }), p = k({
    numberOfClassBreaks: o,
    selectedColorRamp: d,
    colorRamp: i ? n?.map((u) => u.symbol?.color) : void 0,
    isFlipped: m
  });
  return await J({
    data: s,
    classificationMethod: t,
    breaksCount: o,
    layer: l,
    xAxisField: a,
    colorsForClassBreaks: p
  });
}
const b = j.all(), F = {
  esriColorRamps: b,
  defaultColorRampForCharts: b.find((e) => e.name === "Blue 3") ?? b[0]
};
export {
  ne as C,
  me as a,
  pe as b,
  F as c,
  re as d,
  le as e,
  ie as g,
  ce as i,
  Y as p,
  Q as r
};
