import { c as G } from "../../chunks/runtime.js";
import { css as X, html as v } from "lit";
import { LitElement as D, safeClassMap as O } from "@arcgis/lumina";
import { watch as H, when as q } from "@arcgis/core/core/reactiveUtils.js";
import { a as $ } from "../../chunks/maybe.js";
import { c as w, t as I } from "../../chunks/async-utils.js";
import z from "@arcgis/core/popup/content/MediaContent.js";
import K from "@arcgis/core/popup/content/FieldsContent.js";
import Q from "@arcgis/core/popup/content/TextContent.js";
import { compileExpression as Y } from "@arcgis/core/applications/Components/arcadeFeatureUtils.js";
import { s as Z } from "../../chunks/feature-utils.js";
/*! All material copyright Esri, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.34/esri/copyright.txt for details.
v4.34.7 */
var ee = typeof global == "object" && global && global.Object === Object && global, te = typeof self == "object" && self && self.Object === Object && self, W = ee || te || Function("return this")(), b = W.Symbol, J = Object.prototype, ne = J.hasOwnProperty, ie = J.toString, T = b ? b.toStringTag : void 0;
function re(e) {
  var t = ne.call(e, T), i = e[T];
  try {
    e[T] = void 0;
    var n = !0;
  } catch {
  }
  var r = ie.call(e);
  return n && (t ? e[T] = i : delete e[T]), r;
}
var ae = Object.prototype, se = ae.toString;
function oe(e) {
  return se.call(e);
}
var le = "[object Null]", ce = "[object Undefined]", L = b ? b.toStringTag : void 0;
function fe(e) {
  return e == null ? e === void 0 ? ce : le : L && L in Object(e) ? re(e) : oe(e);
}
function ue(e) {
  return e != null && typeof e == "object";
}
var de = "[object Symbol]";
function me(e) {
  return typeof e == "symbol" || ue(e) && fe(e) == de;
}
var pe = /\s/;
function he(e) {
  for (var t = e.length; t-- && pe.test(e.charAt(t)); )
    ;
  return t;
}
var ge = /^\s+/;
function ve(e) {
  return e && e.slice(0, he(e) + 1).replace(ge, "");
}
function x(e) {
  var t = typeof e;
  return e != null && (t == "object" || t == "function");
}
var C = NaN, Te = /^[-+]0x[0-9a-f]+$/i, ye = /^0b[01]+$/i, be = /^0o[0-7]+$/i, xe = parseInt;
function A(e) {
  if (typeof e == "number")
    return e;
  if (me(e))
    return C;
  if (x(e)) {
    var t = typeof e.valueOf == "function" ? e.valueOf() : e;
    e = x(t) ? t + "" : t;
  }
  if (typeof e != "string")
    return e === 0 ? e : +e;
  e = ve(e);
  var i = ye.test(e);
  return i || be.test(e) ? xe(e.slice(2), i ? 2 : 8) : Te.test(e) ? C : +e;
}
var k = function() {
  return W.Date.now();
}, _e = "Expected a function", $e = Math.max, Ie = Math.min;
function ke(e, t, i) {
  var n, r, o, l, a, c, u = 0, h = !1, d = !1, p = !0;
  if (typeof e != "function")
    throw new TypeError(_e);
  t = A(t) || 0, x(i) && (h = !!i.leading, d = "maxWait" in i, o = d ? $e(A(i.maxWait) || 0, t) : o, p = "trailing" in i ? !!i.trailing : p);
  function f(s) {
    var m = n, g = r;
    return n = r = void 0, u = s, l = e.apply(g, m), l;
  }
  function P(s) {
    return u = s, a = setTimeout(y, t), h ? f(s) : l;
  }
  function V(s) {
    var m = s - c, g = s - u, E = t - m;
    return d ? Ie(E, o - g) : E;
  }
  function S(s) {
    var m = s - c, g = s - u;
    return c === void 0 || m >= t || m < 0 || d && g >= o;
  }
  function y() {
    var s = k();
    if (S(s))
      return j(s);
    a = setTimeout(y, V(s));
  }
  function j(s) {
    return a = void 0, p && n ? f(s) : (n = r = void 0, l);
  }
  function B() {
    a !== void 0 && clearTimeout(a), u = 0, n = c = r = a = void 0;
  }
  function U() {
    return a === void 0 ? l : j(k());
  }
  function _() {
    var s = k(), m = S(s);
    if (n = arguments, r = this, c = s, m) {
      if (a === void 0)
        return P(c);
      if (d)
        return clearTimeout(a), a = setTimeout(y, t), f(c);
    }
    return a === void 0 && (a = setTimeout(y, t)), l;
  }
  return _.cancel = B, _.flush = U, _;
}
var Se = "Expected a function";
function M(e, t, i) {
  var n = !0, r = !0;
  if (typeof e != "function")
    throw new TypeError(Se);
  return x(i) && (n = "leading" in i ? !!i.leading : n, r = "trailing" in i ? !!i.trailing : r), ke(e, t, {
    leading: n,
    maxWait: t,
    trailing: r
  });
}
const je = X`.arcgis-feature-expression__loading-container{display:flex;justify-content:center;padding:var(--calcite-spacing-md) 0;width:100%}`, R = 1, N = "arcgis-feature", F = {
  // widget
  base: `${N}-expression`,
  // loading
  loadingSpinnerContainer: `${N}__loading-container`
};
class Ee extends D {
  constructor() {
    super(...arguments), this._compileThrottled = M(this._startCompile, R), this._evaluateThrottled = M(this._startEvaluate, R), this.headingLevel = 2;
  }
  static {
    this.properties = { _compileTask: 16, _evaluateTask: 16, values: 0, fieldInfoMap: 0, expressionInfo: 0, graphic: 0, headingLevel: 9, interceptor: 0, location: 0, messages: 0, spatialReference: 0, map: 0, view: 0 };
  }
  static {
    this.styles = je;
  }
  loaded() {
    this.manager.onLifecycle(() => [
      H(() => [this.expressionInfo, this.graphic], () => this._compileThrottled(), { initial: !0 }),
      q(() => {
        if (!this._compileTask?.finished)
          return null;
        const t = this._compileTask.value, i = t?.dependencies, { view: n } = this;
        return [
          t,
          this.spatialReference,
          this.map,
          n,
          i?.has("view-scale") && n && "scale" in n ? n.scale : null,
          i?.has("view-time-extent") ? n?.timeExtent?.start : null,
          i?.has("view-time-extent") ? n?.timeExtent?.end : null
        ];
      }, ([t]) => this._evaluateThrottled(t)),
      {
        remove: () => this._cancelThrottles()
      }
    ]);
  }
  _cancelThrottles() {
    this._compileThrottled.cancel(), this._evaluateThrottled.cancel();
  }
  _startCompile() {
    this._evaluateTask = $(this._evaluateTask), this._compileTask = $(this._compileTask), this._compileTask = w(async (t) => {
      const { expressionInfo: i, graphic: n } = this, r = i?.expression;
      if (!r || !n)
        return null;
      const o = await Y({
        expression: r,
        graphic: n
      });
      return I(t), o;
    });
  }
  _startEvaluate(t) {
    this._evaluateTask = $(this._evaluateTask), this._evaluateTask = w(async (i) => {
      const { graphic: n } = this;
      if (!t || !n)
        return null;
      const { interceptor: r, spatialReference: o, map: l, location: a, view: c } = this, u = await t.evaluate({
        graphic: n,
        interceptor: r,
        location: a,
        map: l,
        options: { signal: i },
        spatialReference: o,
        view: c
      });
      I(i);
      const h = u && "castAsJsonAsync" in u && typeof u.castAsJsonAsync == "function" ? u : null;
      if (!h)
        return null;
      const d = await h.castAsJsonAsync(i);
      I(i);
      const p = d?.type, f = p === "media" ? z.fromJSON(d) : p === "text" ? Q.fromJSON(d) : p === "fields" ? K.fromJSON(d) : null;
      return !f || f.type === "media" && !f.mediaInfos || f.type === "fields" && !f.fieldInfos || f.type === "text" && !f.text ? null : f;
    });
  }
  render() {
    const t = !this._compileTask?.finished || !this._evaluateTask?.finished;
    return v`<div class=${O(F.base)}>${t ? this._renderLoading() : this._evaluateTask?.value ? this._renderContent() : null}</div>`;
  }
  _renderContent() {
    const { values: t, fieldInfoMap: i } = this, n = this._evaluateTask?.value, r = n?.type;
    if (!r)
      return null;
    if (r === "text") {
      const o = Z({
        values: t,
        fieldInfoMap: i ?? /* @__PURE__ */ new Map(),
        globalValues: t ?? {},
        layer: void 0,
        text: n.text
      });
      return v`<arcgis-feature-content .creator=${o} .graphic=${this.graphic}></arcgis-feature-content>`;
    } else if (r === "media") {
      const { title: o, description: l, mediaInfos: a } = n;
      if (!a?.length)
        return null;
      const c = { ...t, ...n.attributes };
      return v`<arcgis-feature-media .heading=${o ?? ""} .values=${c} .formattedValues=${c} .description=${l ?? ""} .mediaInfos=${a} .messages=${this.messages} .headingLevel=${this.headingLevel}></arcgis-feature-media>`;
    } else if (r === "fields") {
      const { title: o, description: l, fieldInfos: a } = n;
      return a?.length ? v`<arcgis-feature-fields .fieldInfos=${a} .heading=${o ?? ""} .description=${l ?? ""} .headingLevel=${this.headingLevel} .messages=${this.messages}></arcgis-feature-fields>` : null;
    }
    return null;
  }
  _renderLoading() {
    return v`<div class=${O(F.loadingSpinnerContainer)}><calcite-loader inline label></calcite-loader></div>`;
  }
}
G("arcgis-feature-expression", Ee);
export {
  Ee as ArcgisFeatureExpression
};
