import { createArcadeExecutor as C } from "@arcgis/core/arcade.js";
/*! 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.94 */
const O = "https://developers.arcgis.com/arcade/", P = 100, A = 100;
function g(t) {
  return !!t && typeof t == "object" && "declaredRootClass" in t && typeof t.declaredRootClass == "string" && t.declaredRootClass === "esri.arcade.featureset.support.FeatureSet";
}
function u(t) {
  return !!t && typeof t == "object" && "arcadeDeclaredClass" in t && typeof t.arcadeDeclaredClass == "string" && t.arcadeDeclaredClass === "esri.arcade.Feature";
}
function b(t) {
  return !!t && typeof t == "object" && "declaredClass" in t && typeof t.declaredClass == "string" && t.declaredClass === "esri.arcade.Dictionary";
}
function R(t) {
  return !!t && typeof t == "object" && "declaredClass" in t && typeof t.declaredClass == "string" && t.declaredClass.startsWith("esri.geometry.");
}
function w(t) {
  return !!t && typeof t == "object" && "declaredClass" in t && typeof t.declaredClass == "string" && t.declaredClass.startsWith("esri.rest.knowledgeGraph.KnowledgeGraph");
}
function D(t) {
  return !!t && typeof t == "object" && "declaredClass" in t && typeof t.declaredClass == "string" && t.declaredClass === "esri.arcade.Portal";
}
function h(t) {
  return !!t && typeof t == "object" && "declaredClass" in t && typeof t.declaredClass == "string" && t.declaredClass === "esri.arcade.Attachment";
}
function x(t) {
  return !!t && typeof t == "object" && "declaredRootClass" in t && typeof t.declaredRootClass == "string" && t.declaredRootClass === "esri.arcade.arcadedate";
}
function S(t) {
  return !!t && typeof t == "object" && "declaredRootClass" in t && typeof t.declaredRootClass == "string" && t.declaredRootClass === "esri.core.sql.dateonly";
}
function F(t) {
  return !!t && typeof t == "object" && "declaredRootClass" in t && typeof t.declaredRootClass == "string" && t.declaredRootClass === "esri.core.sql.timeonly";
}
function T(t) {
  return !!t && typeof t == "object" && "arcadeDeclaredClass" in t && typeof t.arcadeDeclaredClass == "string" && t.arcadeDeclaredClass === "esri.arcade.Pixel";
}
function j(t) {
  return !!t && typeof t == "object" && "length" in t && typeof t.length == "function" && "get" in t && typeof t.get == "function" && "toArray" in t && typeof t.toArray == "function" && "_elements" in t && Array.isArray(t._elements);
}
function Z(t) {
  return t == null ? { type: "null" } : Array.isArray(t) ? { type: "array", value: t } : j(t) ? { type: "array", value: t.toArray() } : t instanceof Date ? { type: "date", value: t } : typeof t == "string" ? { type: "text", value: t } : typeof t == "number" ? { type: "number", value: t } : typeof t == "boolean" ? { type: "boolean", value: t } : typeof t != "object" ? { type: "unknown" } : x(t) ? { type: "date", value: t } : S(t) ? { type: "dateOnly", value: t } : F(t) ? { type: "time", value: t } : h(t) ? { type: "attachment", value: t } : D(t) ? { type: "portal", value: t } : g(t) ? { type: "featureSet", value: t, features: [], iterator: null } : u(t) ? { type: "feature", value: t } : T(t) ? { type: "pixel", value: t } : b(t) ? { type: "dictionary", value: t } : R(t) ? { type: "geometry", value: t } : w(t) ? { type: "knowledgeGraph", value: t } : { type: "unknown" };
}
function I(t, r = "en") {
  return t.value instanceof Date ? new Intl.DateTimeFormat(r, { dateStyle: "medium", timeStyle: "long" }).format(t.value) : t.value.toDateTime().setLocale(r).toFormat(t.value.isUnknownTimeZone ? "FF" : "FF ZZZZZ");
}
function B(t) {
  return t.value.toString();
}
function E(t) {
  return t.value.toString();
}
async function k(t, r = "system") {
  const e = Z(t);
  return e.type !== "featureSet" || (await e.value.load(), e.iterator = e.value.iterator({ aborted: !1 }), e.features = await e.iterator.nextBatchAsArcadeFeatures(A, r) ?? []), e;
}
async function W(t, r, e, i) {
  const a = /* @__PURE__ */ new Date();
  i ??= () => {
  };
  try {
    const n = performance.now(), o = await C(r ?? "", {
      variables: t?.variables ?? []
    }), c = performance.now(), { profileVariableInstances: f, spatialReference: y, timeZone: d, services: p } = e, m = {
      spatialReference: y,
      timeZone: d,
      rawOutput: !0,
      console: i,
      services: p
    }, l = await o.executeAsync(
      f,
      // TODO: the core type is updated and we should be able to remove this cast
      m
    ), s = await k(l, d);
    return s.timeStamp = a, s.compilationTime = c - n, s.executionTime = performance.now() - c, s;
  } catch (n) {
    let o = "Unknown Error";
    return n instanceof Error && (o = n.message), {
      type: "error",
      value: o,
      error: n,
      timeStamp: a,
      compilationTime: 0,
      executionTime: 0
    };
  }
}
export {
  A as B,
  O as D,
  P as M,
  B as a,
  I as b,
  W as e,
  E as f,
  Z as v
};
