import "@arcgis/lumina";
import "lit/static-html.js";
import "@arcgis/core/intl.js";
import f from "@arcgis/core/support/actions/ActionButton.js";
import b from "@arcgis/core/support/actions/ActionToggle.js";
import { i as y } from "./async-utils.js";
import { g as d } from "./feature-utils.js";
import { isZoomScreenSize as z } from "@arcgis/core/applications/Components/actionUtils.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 */
const s = new f({
  icon: "magnifying-glass-plus",
  id: "zoom-to-feature",
  title: "{messages.zoom}"
}), v = new f({
  icon: "trash",
  id: "remove-selected-feature",
  title: "{messages.remove}"
}), c = new f({
  icon: "magnifying-glass-plus",
  id: "zoom-to-clustered-features",
  title: "{messages.zoom}"
}), E = new b({
  icon: "table",
  id: "browse-clustered-features",
  title: "{messages.browseClusteredFeatures}",
  value: !1
});
function Z(e) {
  if (e.icon)
    return e.icon;
  if (!("image" in e && e.image) && !e.className)
    return "question";
}
function k(e) {
  return e ? {
    backgroundImage: `url(${e})`
  } : {};
}
function C(e) {
  const { selectedFeature: t, location: a, view: o } = e;
  return o ? t ?? a ?? null : null;
}
async function F(e, t) {
  const { location: a, selectedFeature: o, view: r, zoomFactor: i } = e;
  await t?.viewModel?.updateGeometry();
  const n = t?.graphic, m = n?.geometry ? n : C(e);
  if (!r || !m) {
    const l = "zoom-to:invalid-target-or-view: Cannot zoom to location without a target and view.", g = new Error(l);
    throw console.error(l, {
      target: m,
      view: r
    }), g;
  }
  const p = r.scale / i, u = n?.geometry ?? e.selectedFeature?.geometry ?? a, w = u != null && u.type === "point" && await z(n ?? o, r);
  s.active = !0, s.disabled = !0;
  try {
    await e.zoomTo({
      target: {
        target: m,
        scale: w ? p : void 0
      }
    });
  } catch (l) {
    if (y(l))
      return;
    const g = "zoom-to:invalid-graphic: Could not zoom to the location of the graphic.", h = new Error(g);
    throw console.error(g, {
      graphic: n ?? o
    }), h;
  } finally {
    s.active = !1, s.disabled = !1, w && e.location !== u && (e.location = u);
  }
}
function T(e) {
  return !!e && e.isAggregate && d(e)?.featureReduction?.type === "cluster";
}
async function A(e, t) {
  const a = await e.whenLayerView(d(t)), o = a.createQuery(), r = t.getObjectId();
  return o.aggregateIds = r != null ? [r] : [], [a, o];
}
async function S(e) {
  const { selectedFeature: t, view: a } = e;
  if (a?.type !== "2d")
    throw new Error("zoomToCluster:invalid-view: View must be 2d MapView.");
  if (!t || !T(t))
    throw new Error("zoomToCluster:invalid-selectedFeature: Selected feature must represent an aggregate/cluster graphic.");
  const [o, r] = await A(a, t);
  c.active = !0, c.disabled = !0;
  const { extent: i } = await o.queryExtent(r);
  i && await e.zoomTo({ target: i }), c.active = !1, c.disabled = !1;
}
const B = async (e, t) => {
  if (!e)
    throw new Error("trigger-action:missing-arguments: Event has no action");
  const { disabled: a, id: o } = e;
  if (!o)
    throw new Error("trigger-action:invalid-action: action.id is missing");
  if (a)
    throw new Error("trigger-action:invalid-action: Action is disabled");
  if (o === s.id)
    return await F(t).catch((r) => {
      if (!y(r))
        throw r;
    });
  if (o === c.id)
    return await S(t);
  if (o === E.id) {
    t.browseClusterEnabled = !t.browseClusterEnabled, t.featureMenuOpen = t.browseClusterEnabled;
    return;
  }
  if (o === v.id) {
    t.visible = !1;
    const { selectedFeature: r } = t;
    if (!r)
      throw new Error("trigger-action:missing-selected-feature: Cannot remove selected feature without a selected feature.");
    const i = d(r);
    i ? i.remove(r) : t.view?.graphics.remove(r);
  }
};
export {
  k as a,
  F as b,
  Z as g,
  B as t,
  s as z
};
