/* COPYRIGHT Esri - https://js.arcgis.com/5.2/LICENSE.txt */
import { c as C } from "../../chunks/runtime.js";
import { watch as n } from "@arcgis/core/core/reactiveUtils.js";
import _ from "@arcgis/core/views/LinkChartView.js";
import b from "@arcgis/core/WebLinkChart.js";
import { LitElement as V, createEvent as c, safeClassMap as w } from "@arcgis/lumina";
import { reCreateAccessor as A, reEmitEvent as e, makeAccessorController as f } from "@arcgis/lumina/controllers/accessor";
import { createRef as v, ref as m } from "lit/directives/ref.js";
import { s as I } from "../../chunks/esri-main.js";
import { s as E } from "../../chunks/slots.js";
import { C as u } from "../../chunks/resources.js";
import { p as L, i as k } from "../../chunks/component-utils.js";
import { U as M, u as R, a as U } from "../../chunks/useArcgisViewReadyError.js";
import { fetchKnowledgeGraph as D } from "@arcgis/core/rest/knowledgeGraphService.js";
import x from "@arcgis/core/rest/knowledgeGraph/GraphQueryStreaming.js";
import { t as d } from "../../chunks/async-utils.js";
import { isAbortError as g } from "@arcgis/core/core/promiseUtils.js";
import { html as y } from "lit";
import Q from "@arcgis/core/layers/LinkChartLayer.js";
const P = f(
  b,
  /**
   * Since omitProps is only needed for typing, not runtime, `{} as { ... }` is
   * a hack to remove them from the runtime code. This argument is not used at
   * all at runtime. For makeViewModelController and makeWidgetController, the
   * minifier is able to remove them automatically. However, minifier does not
   * remove them for makeAccessorController as makeAccessorController is defined
   * in an externalized package.
   */
  {}
), z = f(_, {});
class Z extends V {
  constructor() {
    super(), this._containerRef = v(), this._mapRef = v(), this._uiController = new M(this), this._activeLoadAbortController = null, this.arcgisLoadError = R(this), this.arcgisViewReadyError = U(this), this._map = P(this), this._baseCypherQuery = "", this._serviceUrl = "", this._propItemId = "", this.view = z(this), this.animationsDisabled = this.view.animationsEnabled, this.allLayerViews = this.view.allLayerViews, this.analyses = this.view.analyses, this.attributionMode = this.view.attributionMode, this.autoDestroyDisabled = !1, this.background = this.view.background, this.basemap = this._map.basemap, this.basemapView = this.view.basemapView, this.canZoomIn = this.view.canZoomIn, this.canZoomOut = this.view.canZoomOut, this.constraints = this.view.constraints, this.displayFilterDisabled = this.view.displayFilterEnabled, this.fatalError = this.view.fatalError, this.graphics = this.view.graphics, this.highlights = this.view.highlights, this.layerViews = this.view.layerViews, this.magnifier = this.view.magnifier, this.map = this.view.map, this.navigation = this.view.navigation, this.padding = this.view.padding, this.popup = this.view.popup, this.popupDisabled = this.view.popupEnabled, this.popupComponentEnabled = !1, this.ready = this.view.ready, this.resizeAlign = this.view.resizeAlign, this.resolution = this.view.resolution, this.rotation = this.view.rotation, this.selectionManager = this.view.selectionManager, this.slotGroupRefs = this._uiController.slotGroupRefs, this.spatialReference = this.view.spatialReference, this.suspended = this.view.suspended, this.theme = this.view.theme, this.timeExtent = this.view.timeExtent, this.timeZone = this.view.timeZone, this.updating = this.view.updating, this.arcgisViewAnalysisViewCreate = e(() => this.view, "analysis-view-create"), this.arcgisViewAnalysisViewCreateError = e(() => this.view, "analysis-view-create-error"), this.arcgisViewAnalysisViewDestroy = e(() => this.view, "analysis-view-destroy"), this.arcgisViewChange = c(), this.arcgisViewClick = e(() => this.view, "click"), this.arcgisViewDoubleClick = e(() => this.view, "double-click"), this.arcgisViewDoubleTapDrag = e(() => this.view, "double-tap-drag"), this.arcgisViewDrag = e(() => this.view, "drag"), this.arcgisViewHold = e(() => this.view, "hold"), this.arcgisViewImmediateClick = e(() => this.view, "immediate-click"), this.arcgisViewImmediateDoubleClick = e(() => this.view, "immediate-double-click"), this.arcgisViewKeyDown = e(() => this.view, "key-down"), this.arcgisViewKeyUp = e(() => this.view, "key-up"), this.arcgisViewLayerviewCreate = e(() => this.view, "layerview-create"), this.arcgisViewLayerviewCreateError = e(() => this.view, "layerview-create-error"), this.arcgisViewLayerviewDestroy = e(() => this.view, "layerview-destroy"), this.arcgisViewMouseWheel = e(() => this.view, "mouse-wheel"), this.arcgisViewPointerDown = e(() => this.view, "pointer-down"), this.arcgisViewPointerEnter = e(() => this.view, "pointer-enter"), this.arcgisViewPointerLeave = e(() => this.view, "pointer-leave"), this.arcgisViewPointerMove = e(() => this.view, "pointer-move"), this.arcgisViewPointerUp = e(() => this.view, "pointer-up"), this.arcgisViewReadyChange = c(), this.arcgisViewVerticalTwoFingerDrag = e(() => this.view, "vertical-two-finger-drag"), this.view.ui = {
      components: []
    };
  }
  static {
    this.properties = { _map: 16, _baseCypherQuery: 16, _serviceUrl: 16, _propItemId: 16, view: 32, animationsDisabled: 5, aria: 0, allLayerViews: 32, analyses: 0, attributionItems: 32, attributionMode: 1, autoDestroyDisabled: 5, background: 0, basemap: 1, basemapView: 0, canZoomIn: 36, canZoomOut: 36, center: 1, constraints: 0, displayFilterDisabled: 5, extent: 0, fatalError: 0, focusAreas: 0, graphics: 0, hideAttribution: 5, highlights: 0, interacting: 32, itemId: 3, baseCypherQuery: 3, serviceUrl: 3, layerViews: 32, magnifier: 32, map: 0, navigating: 32, navigation: 0, padding: 0, popup: 0, popupElement: 32, popupDisabled: 5, popupComponentEnabled: 5, ready: 36, resizeAlign: 1, resolution: 40, rotation: 9, selectionManager: 32, scale: 9, slotGroupRefs: 32, spatialReference: 0, stationary: 32, suspended: 39, theme: 0, timeExtent: 0, timeZone: 1, updating: 36, viewpoint: 0, zoom: 9, loadErrorSources: 0 };
  }
  static {
    this.styles = [I, E];
  }
  get aria() {
    return this.view?.aria;
  }
  set aria(i) {
    this.view && (this.view.aria = i);
  }
  get attributionItems() {
    return this.view?.attributionItems ?? [];
  }
  get center() {
    return this.view.center;
  }
  set center(i) {
    const t = L(i, this.view);
    t && (this.view.center = t);
  }
  get extent() {
    return this.view.extent;
  }
  set extent(i) {
    this.extent?.equals(i) || (this.view.extent = i);
  }
  get focusAreas() {
    return this.map ? this.map?.focusAreas : {
      areas: [],
      style: "bright"
    };
  }
  set focusAreas(i) {
    this.map && (this.map.focusAreas = i);
  }
  get hideAttribution() {
    return !this.view.attributionVisible;
  }
  set hideAttribution(i) {
    this.view.attributionVisible = !i;
  }
  get interacting() {
    return this.view.interacting;
  }
  get itemId() {
    return this._map.portalItem?.id;
  }
  set itemId(i) {
    this._propItemId = i;
  }
  get baseCypherQuery() {
    return this._baseCypherQuery;
  }
  set baseCypherQuery(i) {
    this._baseCypherQuery = i;
  }
  get serviceUrl() {
    return this._serviceUrl;
  }
  set serviceUrl(i) {
    this._serviceUrl = i;
  }
  get navigating() {
    return this.view.navigating;
  }
  get popupElement() {
    return this._uiController.popupElement || null;
  }
  get scale() {
    return this.view.scale;
  }
  set scale(i) {
    this.view.scale = i;
  }
  get stationary() {
    return this.view.stationary;
  }
  get viewpoint() {
    return this.view.viewpoint;
  }
  set viewpoint(i) {
    k(this.viewpoint, i) && (this.view.viewpoint = i);
  }
  get zoom() {
    return this.view.zoom;
  }
  set zoom(i) {
    this.view.zoom = i;
  }
  async closePopup() {
    this._uiController.closePopup();
  }
  async fetchPopupFeatures(i, t) {
    return this.view.fetchPopupFeatures(i, t);
  }
  async destroy() {
    await this.manager.destroy();
  }
  async goTo(i, t) {
    return await this.view.goTo(i, t);
  }
  async openPopup(i) {
    this._uiController.openPopup(i);
  }
  toMap(i) {
    return this.view.toMap(i);
  }
  toScreen(i, t) {
    return this.view.toScreen(i, t);
  }
  async tryFatalErrorRecovery() {
    this.view.tryFatalErrorRecovery();
  }
  async viewOnReady(i, t) {
    return await this.componentOnReady(), await this.view.whenReady().then(() => i?.()).catch(t);
  }
  async whenAnalysisView(i) {
    return await this.view.whenAnalysisView(i);
  }
  async whenLayerView(i) {
    return await this.view.whenLayerView(i);
  }
  async zoomIn() {
    this.view.zoomIn();
  }
  async zoomOut() {
    this.view.zoomOut();
  }
  loaded() {
    const i = this;
    i.el.childElem = this._mapRef.value, this.view.container ??= i.el.childElem, s.setLocalPublicMap(this, this._map), this.manager.onLifecycle(() => [
      n(() => this.map, (t) => {
        t && s.setLocalPrivateMap(this, t);
      }, { initial: !0, sync: !0 }),
      n(() => this.view.stationary, () => this.arcgisViewChange.emit(), { initial: !0 }),
      n(() => this.view.ready, () => this.arcgisViewReadyChange.emit(), { initial: this.view.ready }),
      n(() => this.view.attributionHeight, (t) => {
        this._uiController.handleAttributionHeightChange(t);
      }, { initial: !0 })
    ]);
  }
  willUpdate(i) {
    const t = () => {
      this._activeLoadAbortController?.abort();
      const a = new AbortController();
      return this._activeLoadAbortController = a, a;
    };
    if (i.has("_propItemId") || i.has("_serviceUrl") || i.has("_baseCypherQuery")) {
      if (i.has("_propItemId") && this._propItemId || i.has("_propItemId") && !this._serviceUrl)
        t(), s.reCreateAccessor(this._map, this), s.setPortalItem(this._map, this._propItemId), s.setMap(this.view, this._map);
      else if (!this._propItemId)
        if (this._serviceUrl && !this._baseCypherQuery) {
          const a = t();
          s.fetchKnowledgeGraph(this._serviceUrl).then((l) => {
            const h = new s.WebLinkChart(), o = new s.LinkChartLayer({
              url: l.url
            });
            o.load({
              signal: a?.signal
            });
            try {
              d(a.signal), h.add(o), this._setLinkChartAndZoom(h);
            } catch (p) {
              if (!g(p))
                throw p;
            }
          });
        } else if (this._baseCypherQuery && this._serviceUrl) {
          const a = t();
          s.fetchKnowledgeGraph(this._serviceUrl).then((l) => {
            s.WebLinkChart.fromCypherQuery(l, new x({
              openCypherQuery: this._baseCypherQuery
            }), void 0, {
              signal: a?.signal
            }).then(async (h) => {
              try {
                d(a.signal), this._setLinkChartAndZoom(h);
              } catch (o) {
                if (!g(o))
                  throw o;
              }
            });
          });
        } else !this._serviceUrl && i.has("_serviceUrl") && (t(), s.setMap(this.view, null));
    }
  }
  _setLinkChartAndZoom(i) {
    s.reCreateAccessor(this._map, this), s.setLocalPrivateMap(this, i), s.setMap(this.view, i), this._map && this.view.when(() => {
      i.diagramNodesExtent && this.view.goTo(i.diagramNodesExtent);
    });
  }
  renderMain() {
    return y`<div class=${w(u.base)} ${m(this._mapRef)}></div>`;
  }
  render() {
    return y`${this.renderMain()}<section class=${w(u.containerUI)} ${m(this._containerRef)}>${this._uiController.render()}</section>`;
  }
}
const s = {
  fetchKnowledgeGraph: async (r) => await D(r),
  WebLinkChart: b,
  LinkChartLayer: Q,
  setMap: (r, i) => {
    r.map = i;
  },
  setLocalPrivateMap: (r, i) => {
    r._map = i;
  },
  setLocalPublicMap: (r, i) => {
    r.map ||= i;
  },
  setPortalItem(r, i) {
    r.portalItem = { id: i };
  },
  reCreateAccessor: A
};
C("arcgis-link-chart", Z);
export {
  Z as ArcgisLinkChart,
  s as exportsForTests
};
