/* COPYRIGHT Esri - https://js.arcgis.com/5.1/LICENSE.txt */
import * as m from "@arcgis/core/geometry/operators/unionOperator.js";
import * as l from "@arcgis/core/geometry/operators/projectOperator.js";
import * as q from "@arcgis/core/geometry/support/webMercatorUtils.js";
async function F(t, e) {
  let n = [];
  const r = t ? t.map((s) => w(s, e)) : [Promise.resolve()];
  return (await Promise.all(r)).forEach((s) => {
    n = [...n, ...s || []];
  }), n;
}
async function j(t, e, n, r, o, s) {
  const c = e.capabilities?.query.maxRecordCount || 1e3, d = 0;
  t.length === 0 && (t = await e.queryObjectIds());
  const u = e.createQuery();
  if (u.returnGeometry = r, u.objectIds = t.slice(d, c), o && (u.outSpatialReference = o), s) {
    const p = e.fields.map((a) => a.name.toLowerCase());
    u.outFields = s.filter((a) => p.indexOf(a.toLowerCase()) > -1);
  }
  const y = await e.queryFeatures(u);
  n = n.concat(y.features);
  const f = t.slice(c, t.length);
  return f.length > 0 ? j(f, e, n, r, o, s) : Promise.resolve(n);
}
async function I(t, e) {
  const n = e.globalIdField;
  if (!n)
    return [];
  const r = e.createQuery();
  return r.returnGeometry = !1, r.outFields = [e.objectIdField], r.where = t.map((s) => `${n} = '${s}'`).join(" or "), (await e.queryFeatures(r)).features;
}
async function b(t, e, n, r) {
  const o = e.capabilities?.query.maxRecordCount || 1e3, s = e.createQuery();
  s.start = t, s.num = o, s.geometry = n;
  const c = await e.queryFeatures(s);
  return r[e.id] = r[e.id].concat(c.features), c.exceededTransferLimit ? b(t += o, e, n, r) : Promise.resolve(r);
}
async function x(t, e) {
  const n = e.createQuery();
  return n.objectIds = t, e.queryExtent(n);
}
function h(t) {
  const e = [], n = i(t, "polygon");
  n && e.push(n);
  const r = i(t, "polyline");
  r && e.push(r);
  const o = i(t, "point");
  return o && e.push(o), e;
}
function i(t, e) {
  const n = (t || []).filter((r) => r.type === e);
  return n.length <= 1 ? n[0] : m.executeMany(n);
}
async function w(t, e) {
  const n = e.createQuery();
  return n.spatialRelationship = "intersects", n.geometry = t, e.queryObjectIds(n);
}
async function P(t, e) {
  return e ? t.spatialReference ? t.spatialReference.equals(e) || e.isWGS84 || e.wkid === 4326 ? t : t.spatialReference.isWGS84 || t.spatialReference.wkid === 4326 ? q.project(t, e) : (await l.load(), l.execute(t, e)) : (console.error("Input geometry does not have a spatial reference"), t) : (console.error("Target spatial reference is undefined"), t);
}
export {
  j as a,
  I as b,
  F as c,
  b as d,
  h as g,
  P as p,
  x as q
};
