import { createFilterExpression as d } from "@arcgis/toolkit/string";
import { format as f } from "@arcgis/arcade-languageservice";
/*! All material copyright Esri, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/5.0/esri/copyright.txt for details.
v5.0.0-next.96 */
function b(e, r, s, u) {
  if (!r)
    return [];
  const t = Array.isArray(r) ? r : [r];
  if (!t.length)
    return [];
  const m = d(s);
  return e.filter((o) => t.some((a) => m.test(String(o[a] ?? ""))));
}
function L(e) {
  return e ? `${e.portal.url}/home/item.html?id=${e.id}` : "";
}
function p(e, r, s = "add", u) {
  if (!e)
    return;
  const t = e.getSelection(), m = t && (t.startLineNumber !== t.endLineNumber || t.startColumn !== t.endColumn);
  let o = [];
  const a = e.getValue() ?? "";
  if (s === "replace") {
    const l = `${u}
${r}`, i = e.getModel(), n = i?.getLineCount() ?? 1, c = i?.getLineMaxColumn(n) ?? 1;
    o = [
      {
        range: {
          startLineNumber: 1,
          startColumn: 1,
          endLineNumber: n,
          endColumn: c
        },
        text: l
      }
    ];
  } else {
    const l = m ? {
      startLineNumber: t.startLineNumber,
      startColumn: t.startColumn,
      endLineNumber: t.endLineNumber,
      endColumn: t.endColumn
    } : (() => {
      const n = e.getPosition();
      return {
        startLineNumber: n?.lineNumber ?? 1,
        startColumn: n?.column ?? 1,
        endLineNumber: n?.lineNumber ?? 1,
        endColumn: n?.column ?? 1
      };
    })();
    let i = !1;
    if (u) {
      const n = u.replace(/[.*+?^${}()|[\]\\]/gu, "\\$&");
      i = !new RegExp(`^\\s*${n}\\s*$`, "mu").test(a);
    }
    i && o.push({
      range: {
        startLineNumber: 1,
        startColumn: 1,
        endLineNumber: 1,
        endColumn: 1
      },
      text: `${u}
`
    }), o.push({
      range: l,
      text: r
    });
  }
  try {
    e.pushUndoStop(), e.executeEdits("insertText", o), e.pushUndoStop(), e.focus();
  } catch (l) {
    console.error("Failed to edit model:", l);
  }
}
function N(e) {
  try {
    return f(e);
  } catch {
    return e;
  }
}
export {
  N as a,
  p as e,
  b as f,
  L as p
};
