/* COPYRIGHT Esri - https://js.arcgis.com/5.2/LICENSE.txt */
import { c as T } from "../../chunks/runtime.js";
import { css as x, html as c } from "lit";
import { LitElement as y } from "@arcgis/lumina";
import { watch as k, when as $ } from "@arcgis/core/core/reactiveUtils.js";
import { a as p } from "../../chunks/maybe.js";
import { c as v, t as m } from "../../chunks/async-utils.js";
import { throttle as g } from "lodash-es";
import I from "@arcgis/core/popup/content/MediaContent.js";
import w from "@arcgis/core/popup/content/FieldsContent.js";
import E from "@arcgis/core/popup/content/TextContent.js";
import { compileExpression as L } from "@arcgis/core/applications/Components/arcadeFeatureUtils.js";
import { s as A } from "../../chunks/feature-utils.js";
const b = x`.loading-container{display:flex;justify-content:center;padding:var(--calcite-spacing-md) 0;width:100%}`, _ = 1;
class C extends y {
  constructor() {
    super(...arguments), this._compileThrottled = g(this._startCompile, _), this._evaluateThrottled = g(this._startEvaluate, _), 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 = b;
  }
  loaded() {
    this.manager.onLifecycle(() => [
      k(() => [this.expressionInfo, this.graphic], () => this._compileThrottled(), { initial: !0 }),
      $(() => {
        if (!this._compileTask?.finished)
          return null;
        const t = this._compileTask.value, s = t?.dependencies, { view: e } = this;
        return [
          t,
          this.spatialReference,
          this.map,
          e,
          s?.has("view-scale") && e && "scale" in e ? e.scale : null,
          s?.has("view-time-extent") ? e?.timeExtent?.start : null,
          s?.has("view-time-extent") ? e?.timeExtent?.end : null
        ];
      }, ([t]) => this._evaluateThrottled(t)),
      {
        remove: () => this._cancelThrottles()
      }
    ]);
  }
  _cancelThrottles() {
    this._compileThrottled.cancel(), this._evaluateThrottled.cancel();
  }
  _startCompile() {
    this._evaluateTask = p(this._evaluateTask), this._compileTask = p(this._compileTask), this._compileTask = v(async (t) => {
      const { expressionInfo: s, graphic: e } = this, i = s?.expression;
      if (!i || !e)
        return null;
      const a = await L({
        expression: i,
        graphic: e
      });
      return m(t), a;
    });
  }
  _startEvaluate(t) {
    this._evaluateTask = p(this._evaluateTask), this._evaluateTask = v(async (s) => {
      const { graphic: e } = this;
      if (!t || !e)
        return null;
      const { interceptor: i, spatialReference: a, map: r, location: l, view: o } = this, h = await t.evaluate({
        graphic: e,
        interceptor: i,
        location: l,
        map: r,
        options: { signal: s },
        spatialReference: a,
        view: o
      });
      m(s);
      const f = h && "castAsJsonAsync" in h && typeof h.castAsJsonAsync == "function" ? h : null;
      if (!f)
        return null;
      const d = await f.castAsJsonAsync(s);
      m(s);
      const u = d?.type, n = u === "media" ? I.fromJSON(d) : u === "text" ? E.fromJSON(d) : u === "fields" ? w.fromJSON(d) : null;
      return !n || n.type === "media" && !n.mediaInfos || n.type === "fields" && !n.fieldInfos || n.type === "text" && !n.text ? null : n;
    });
  }
  render() {
    const t = !this._compileTask?.finished || !this._evaluateTask?.finished;
    return c`<div class="root">${t ? this._renderLoading() : this._evaluateTask?.value ? this._renderContent() : null}</div>`;
  }
  _renderContent() {
    const { values: t, fieldInfoMap: s } = this, e = this._evaluateTask?.value, i = e?.type, a = { ...t, ...e?.attributes };
    if (!i)
      return null;
    if (i === "text") {
      const r = A({
        values: a,
        fieldInfoMap: s ?? /* @__PURE__ */ new Map(),
        globalValues: a,
        layer: void 0,
        text: e.text
      });
      return c`<arcgis-feature-content .creator=${r} .graphic=${this.graphic}></arcgis-feature-content>`;
    } else if (i === "media") {
      const { title: r, description: l, mediaInfos: o } = e;
      return o?.length ? c`<arcgis-feature-media .heading=${r ?? ""} .values=${a} .formattedValues=${a} .description=${l ?? ""} .mediaInfos=${o} .messages=${this.messages} .headingLevel=${this.headingLevel}></arcgis-feature-media>` : null;
    } else if (i === "fields") {
      const { title: r, description: l, fieldInfos: o } = e;
      return o?.length ? c`<arcgis-feature-fields .fieldInfos=${o} .heading=${r ?? ""} .values=${a} .description=${l ?? ""} .headingLevel=${this.headingLevel} .messages=${this.messages}></arcgis-feature-fields>` : null;
    }
    return null;
  }
  _renderLoading() {
    return c`<div class="loading-container"><calcite-loader inline label></calcite-loader></div>`;
  }
}
T("arcgis-feature-expression", C);
export {
  C as ArcgisFeatureExpression
};
