/* COPYRIGHT Esri - https://js.arcgis.com/5.1/LICENSE.txt */
import { _ as c } from "./downloadUtils.js";
import * as l from "@arcgis/core/arcade.js";
import * as f from "@arcgis/core/intl.js";
class T {
  /**
   * Get the popup title that honors arcade expressions
   *
   * @param graphic selected graphic
   * @param map Map
   * @returns Promise resolving with the popup title
   * @protected
   */
  async getPopupTitle(t, u) {
    let a = {};
    for (const [e, r] of Object.entries(t.attributes))
      a = {
        ...a,
        [`{${e.toUpperCase()}}`]: r
      };
    const o = t.layer, i = this._removeTags(o?.popupTemplate?.title);
    if (i.includes("{expression/expr") && o?.popupTemplate?.expressionInfos != null)
      for (let e = 0; e < o.popupTemplate?.expressionInfos.length; e++) {
        const r = o.popupTemplate.expressionInfos[e], n = l.createArcadeProfile("popup");
        try {
          const p = await (await l.createArcadeExecutor(r.expression, n)).executeAsync({ $feature: t, $layer: o, $map: u });
          (p !== null || p !== "") && (a[`{expression/${r.name}}`.toUpperCase()] = p);
        } catch (s) {
          console.error(s);
          continue;
        }
      }
    return o.popupTemplate?.fieldInfos && o.fields.forEach((e) => {
      const r = t.attributes[e.name], n = (o?.popupTemplate?.fieldInfos || []).find((p) => p.fieldName.toLowerCase() === e.name.toLowerCase()), s = c(r, e.type, e?.domain, n?.format, f);
      a[`{${e.name.toUpperCase()}}`] = s ?? r;
    }), i?.replace(/{.*?}/g, (e) => a[e.toUpperCase()] != null ? a[e.toUpperCase()] : "");
  }
  /**
   * Remove any tags from the title
   *
   * @returns title string without tags
   * @protected
   */
  _removeTags(t) {
    return t == null || t === "" ? "" : t.toString().replace(/(<([^>]+)>)/gi, "");
  }
}
export {
  T as P
};
