/* COPYRIGHT Esri - https://js.arcgis.com/5.2/LICENSE.txt */
import { i as f } from "./async-utils.js";
import { g as l } from "./feature-utils.js";
import { isZoomScreenSize as w } from "@arcgis/core/applications/Components/actionUtils.js";
function C(e) {
  if (e.icon)
    return e.icon;
  if (!("image" in e && e.image) && !e.className)
    return "question";
}
function T(e) {
  return e ? {
    backgroundImage: `url(${e})`
  } : {};
}
function y(e) {
  const { selectedFeature: t, location: o, view: r } = e;
  return r ? t ?? o ?? null : null;
}
function b(e) {
  return "browseClusterEnabled" in e && "featureMenuOpen" in e;
}
function h(e) {
  return "aggregatedFeaturesEnabled" in e;
}
async function E(e, t, o) {
  const { location: r, selectedFeature: a, view: n, zoomFactor: i } = e;
  await o?.viewModel?.updateGeometry();
  const s = o?.graphic, c = s?.geometry ? s : y(e);
  if (!n || !c)
    throw new Error("Cannot zoom to location without a target and view.");
  const d = n.scale / i, u = s?.geometry ?? e.selectedFeature?.geometry ?? r, g = u?.type === "point" && await w(s ?? a, n);
  t.active = !0, t.disabled = !0;
  try {
    await e.zoomTo({
      target: {
        target: c,
        scale: g ? d : void 0
      }
    });
  } catch (m) {
    if (f(m))
      return;
    throw new Error("Could not zoom to the location of the graphic.");
  } finally {
    t.active = !1, t.disabled = !1, g && e.location !== u && (e.location = u);
  }
}
async function p(e, t) {
  const o = await e.whenLayerView(l(t)), r = o.createQuery(), a = t.getObjectId();
  return r.aggregateIds = a != null ? [a] : [], [o, r];
}
async function z(e, t) {
  const { selectedFeature: o, view: r } = e;
  if (r?.type !== "2d")
    throw new Error("zoomToAggregate:invalid-view: View must be 2d MapView.");
  if (!o?.isAggregate)
    throw new Error("zoomToAggregate:invalid-selectedFeature: Selected feature must represent an aggregate graphic.");
  const [a, n] = await p(r, o);
  t.active = !0, t.disabled = !0;
  const { extent: i } = await a.queryExtent(n);
  i && await e.zoomTo({ target: i }), t.active = !1, t.disabled = !1;
}
const q = async (e, t) => {
  if (!e)
    throw new Error("trigger-action:missing-arguments: Event has no action");
  const { disabled: o, id: r } = e;
  if (!r)
    throw new Error("trigger-action:invalid-action: action.id is missing");
  if (o)
    throw new Error("trigger-action:invalid-action: Action is disabled");
  if (r === "zoom-to-feature")
    return await E(t, e).catch((a) => {
      if (!f(a))
        throw a;
    });
  if (r === "zoom-to-aggregated-features")
    return await z(t, e);
  if (r === "browse-clustered-features") {
    if (!b(t))
      throw new Error("trigger-action:invalid-view-model: browse-clustered-features requires FeaturesViewModel.");
    t.browseClusterEnabled = !t.browseClusterEnabled, t.featureMenuOpen = t.browseClusterEnabled;
    return;
  }
  if (r === "browse-aggregated-features") {
    if (!h(t))
      throw new Error("trigger-action:invalid-view-model: browse-aggregated-features requires Features.");
    t.aggregatedFeaturesEnabled = !t.aggregatedFeaturesEnabled;
    return;
  }
  if (r === "remove-selected-feature") {
    t.open = !1;
    const { selectedFeature: a } = t;
    if (!a)
      throw new Error("trigger-action:missing-selected-feature: Cannot remove selected feature without a selected feature.");
    const n = l(a);
    n ? n.remove(a) : t.view?.graphics.remove(a);
  }
};
export {
  C as a,
  T as g,
  q as t,
  E as z
};
