/* COPYRIGHT Esri - https://js.arcgis.com/5.2/LICENSE.txt */
import { replace as a } from "@arcgis/core/applications/Components/stringUtils.js";
import { fieldFormatFromFieldInfo as p } from "@arcgis/core/applications/Components/fieldFormatUtils.js";
import { g as F } from "./layer-utils.js";
import { log as g } from "@arcgis/toolkit/log";
const u = /^\s*expression\//iu, C = /href=(""|'')/giu, y = /(\{([^{\r\n]+)\})/gu, L = /href\s*=\s*(?:"([^"]+)"|'([^']+)')/giu, h = /'/gu, N = "relationships/", b = /^(?:mailto:|tel:)/u;
function $(e, t) {
  const n = /* @__PURE__ */ new Map();
  if (!e)
    return n;
  for (const r of e) {
    if (!r.fieldName)
      continue;
    const i = f(r.fieldName, t);
    r.fieldName = i, n.set(i.toLowerCase(), r);
  }
  return n;
}
function j(e) {
  const t = [];
  if (!e)
    return t;
  const { fieldInfos: n, content: r } = e;
  return n && t.push(...n), !r || !Array.isArray(r) || r.forEach((i) => {
    if (i.type === "fields") {
      const o = i?.fieldInfos;
      o && t.push(...o);
    }
  }), t;
}
function U(e, t) {
  return e?.getEffectivePopupTemplate(t) ?? void 0;
}
function W(e) {
  if (e != null)
    return "sourceLayer" in e && e.sourceLayer ? e.sourceLayer : e.layer ?? void 0;
}
function w(e = "") {
  return e ? e.includes(N) : !1;
}
function _(e = "") {
  if (e)
    return !b.test(e.trim().toLowerCase());
}
function f(e, t) {
  const n = m(t, e);
  return n ? n.name : e;
}
function q(e, t) {
  return e?.map((n) => f(n, t));
}
function c(e) {
  return e ? u.test(e) : !1;
}
function A(e, t) {
  if (!t || !c(t) || !e)
    return;
  const n = t.replace(u, "").toLowerCase();
  return e.find(({ name: r }) => r.toLowerCase() === n);
}
function B(e, t) {
  if (!(!e?.length || !t))
    return e.find((n) => n.fieldName?.toLowerCase() === t.toLowerCase());
}
function I(e) {
  const { fieldInfo: t, graphic: n, layer: r } = e;
  return x({ fieldInfo: t, graphic: n }) ? F(r, { graphic: n }) : null;
}
function x({ fieldInfo: e, graphic: t }) {
  return !!e?.fieldName && !c(e.fieldName) && !w(e.fieldName) && !t?.popupTemplate;
}
function D({
  expressionInfos: e,
  fieldInfo: t,
  graphic: n,
  isContentFieldInfos: r,
  layer: i
}) {
  if (!t?.fieldName)
    return null;
  const o = A(e, t?.fieldName);
  if (o)
    return o.title || null;
  const s = I({ fieldInfo: t, graphic: n, layer: i });
  if (s) {
    const l = s.getFieldAlias(t.fieldName);
    return r ? t.label || l || t.fieldName : l || t.fieldName;
  }
  return t.label || t.fieldName;
}
function G(e) {
  const { configurableFieldsContainer: t, fieldInfo: n, isContentFieldInfos: r } = e;
  if (!n?.fieldName)
    return null;
  const i = !t.popupTemplate && !t.fieldConfigurations ? p(n, t.getField(n.fieldName)) : t.getFieldConfiguration(n.fieldName)?.fieldFormat;
  return r && n.fieldFormat || i;
}
function d(e) {
  return e.trim();
}
function T(e) {
  return e.replaceAll(C, "");
}
function R(e, t) {
  return `{${t.get(e.toLowerCase())?.fieldName || e}}`;
}
function k({
  formattedValues: e,
  template: t,
  fieldInfoMap: n
}) {
  return d(
    T(
      a(
        a(t, (r) => R(r, n)),
        e
      )
    )
  );
}
function m(e, t) {
  return !(e && "getField" in e && typeof e.getField == "function") || !t ? null : e.getField(t) ?? null;
}
function E(e, t) {
  return e.replaceAll(y, (n, r, i) => {
    const o = m(t, i);
    return o ? `{${o.name}}` : r;
  });
}
function V(e, t, n = !1) {
  const r = t[e];
  if (typeof r == "string") {
    const o = (n ? encodeURIComponent(r) : r).replaceAll(
      h,
      "%27"
    );
    t[e] = o;
  }
}
function v(e, t = !1) {
  const n = { ...e };
  return Object.keys(n).forEach((r) => V(r, n, t)), n;
}
function z(e, t, n) {
  t = d(t);
  const r = t && !t.startsWith("{");
  return a(e, v(n, r || !1));
}
function H(e, t, n) {
  const r = E(e, n);
  return r && r.replaceAll(
    L,
    (i, o, s) => z(i, o || s, t)
  );
}
function J({
  values: e,
  globalValues: t,
  layer: n,
  text: r,
  fieldInfoMap: i
}) {
  return r ? k({
    formattedValues: t,
    template: H(
      r,
      {
        ...t,
        ...e
      },
      n
    ),
    fieldInfoMap: i
  }) : "";
}
async function K({
  type: e,
  value: t,
  event: n
}) {
  try {
    return typeof t == "function" ? t(n) : await t;
  } catch (r) {
    g("error", "arcgis-feature", `An error occurred when calling the "${e}" function`, {
      detail: {
        error: r,
        graphic: n.graphic,
        value: t
      }
    });
    return;
  }
}
export {
  U as a,
  j as b,
  $ as c,
  w as d,
  K as e,
  _ as f,
  W as g,
  D as h,
  c as i,
  I as j,
  G as k,
  E as l,
  k as m,
  q as n,
  f as o,
  B as p,
  J as s
};
