import { replace as u } from "@arcgis/core/applications/Components/stringUtils.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 */
function a(e) {
  return e != null && typeof e == "object" && "type" in e && e.type === "feature";
}
function F(e) {
  return a(e) && e?.type === "feature" && e.source?.type === "feature-layer";
}
function c(e) {
  return a(e) && F(e);
}
const f = /^\s*expression\//iu, g = /href=(""|'')/giu, L = /(\{([^{\r\n]+)\})/gu, y = /href\s*=\s*(?:"([^"]+)"|'([^']+)')/giu, N = /'/gu, h = "relationships/", C = /^(?:mailto:|tel:)/u;
function j(e, t) {
  const n = /* @__PURE__ */ new Map();
  if (!e)
    return n;
  for (const r of e) {
    if (!r.fieldName)
      continue;
    const i = l(r.fieldName, t);
    r.fieldName = i, n.set(i.toLowerCase(), r);
  }
  return n;
}
function z(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 H(e, t) {
  return e?.getEffectivePopupTemplate(t) ?? void 0;
}
function M(e) {
  if (e != null)
    return "sourceLayer" in e && e.sourceLayer ? e.sourceLayer : e.layer ?? void 0;
}
function A(e = "") {
  return e ? e.includes(h) : !1;
}
function O(e = "") {
  if (e)
    return !C.test(e.trim().toLowerCase());
}
function l(e, t) {
  const n = m(t, e);
  return n ? n.name : e;
}
function P(e, t) {
  return e?.map((n) => l(n, t));
}
function d(e) {
  return e ? f.test(e) : !1;
}
function w(e, t) {
  if (!t || !d(t) || !e)
    return;
  const n = t.replace(f, "").toLowerCase();
  return e.find(({ name: r }) => r.toLowerCase() === n);
}
function $(e, t) {
  if (!(!e?.length || !t))
    return e.find((n) => n.fieldName?.toLowerCase() === t.toLowerCase());
}
function b({
  fieldInfo: e,
  graphic: t
}) {
  return !!e?.fieldName && !d(e.fieldName) && !A(e.fieldName) && !t?.isAggregate && !t?.popupTemplate;
}
function U({
  expressionInfos: e,
  fieldInfo: t,
  graphic: n,
  isContentFieldInfos: r,
  layer: i
}) {
  if (!t?.fieldName)
    return null;
  const o = w(e, t?.fieldName);
  if (o)
    return o.title || null;
  if (c(i) && b({ fieldInfo: t, graphic: n })) {
    const s = i.getFieldAlias(t.fieldName);
    return r ? t.label || s || t.fieldName : s || t.fieldName;
  }
  return t.label || t.fieldName;
}
function W({
  fieldInfo: e,
  isContentFieldInfos: t,
  layer: n
}) {
  if (!e?.fieldName)
    return null;
  if (c(n)) {
    const r = n?.getFieldConfiguration?.(e.fieldName)?.fieldFormat;
    return t && e.fieldFormat || r;
  }
  return null;
}
function p(e) {
  return e.trim();
}
function x(e) {
  return e.replaceAll(g, "");
}
function R(e, t) {
  return `{${t.get(e.toLowerCase())?.fieldName || e}}`;
}
function T({
  formattedValues: e,
  template: t,
  fieldInfoMap: n
}) {
  return p(
    x(
      u(
        u(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 k(e, t) {
  return e.replaceAll(L, (n, r, i) => {
    const o = m(t, i);
    return o ? `{${o.name}}` : r;
  });
}
function E(e, t, n = !1) {
  const r = t[e];
  if (typeof r == "string") {
    const o = (n ? encodeURIComponent(r) : r).replaceAll(
      N,
      "%27"
    );
    t[e] = o;
  }
}
function v(e, t = !1) {
  const n = { ...e };
  return Object.keys(n).forEach((r) => E(r, n, t)), n;
}
function I(e, t, n) {
  t = p(t);
  const r = t && !t.startsWith("{");
  return u(e, v(n, r || !1));
}
function V(e, t, n) {
  const r = k(e, n);
  return r && r.replaceAll(
    y,
    (i, o, s) => I(i, o || s, t)
  );
}
function q({
  values: e,
  globalValues: t,
  layer: n,
  text: r,
  fieldInfoMap: i
}) {
  return r ? T({
    formattedValues: t,
    template: V(
      r,
      {
        ...t,
        ...e
      },
      n
    ),
    fieldInfoMap: i
  }) : "";
}
async function _({
  type: e,
  value: t,
  event: n
}) {
  try {
    return typeof t == "function" ? t(n) : await t;
  } catch (r) {
    console.error(`[arcgis-feature] An error occurred when calling the "${e}" function`, {
      error: r,
      graphic: n.graphic,
      value: t
    });
    return;
  }
}
export {
  l as a,
  T as b,
  P as c,
  d,
  $ as e,
  k as f,
  M as g,
  U as h,
  A as i,
  O as j,
  c as k,
  b as l,
  W as m,
  _ as n,
  H as o,
  j as p,
  z as q,
  q as s
};
