/* COPYRIGHT Esri - https://js.arcgis.com/5.1/LICENSE.txt */
import { c as r } from "../../chunks/runtime.js";
import { a as h } from "../../chunks/mapViewUtils.js";
import { u as n } from "../../chunks/useT9n.js";
import { css as d, html as s } from "lit";
import { createRef as c, ref as p } from "lit-html/directives/ref.js";
import { LitElement as f, createEvent as i, noShadowRoot as g, safeClassMap as o } from "@arcgis/lumina";
import * as a from "@arcgis/core/core/reactiveUtils.js";
const m = d`@layer{:host{display:block}#feature-form{padding-top:0}.esri-editor__prompt--danger{position:relative!important;width:100%!important;background-color:var(--calcite-color-surface-2)!important}.esri-feature__content-node{background-color:var(--calcite-color-surface-2)!important}.hide-editor-toolbar .esri-editor__panel-toolbar{display:none!important}.position-absolute{position:absolute;inset:0;overflow:auto}.esri-editor__panel-content{padding-block:10px!important}}`;
class u extends f {
  constructor() {
    super(), this.enableEditGeometry = !1, this.enableSnapping = !1, this.graphicIndex = 0, this.graphics = [], this.open = !1, this.allowMultipleEditing = !1, this._editorLoading = !1, this._layerInfos = null, this._translations = n({ name: "edit-card", blocking: !0 }), this._editor = c(), this._shouldClose = !1, this._shouldEmitEditsComplete = !1, this.closeEdit = i(), this.editorInitialized = i(), this.editsComplete = i(), this.refreshGraphics = i(), this.creatingFeature = i(), this._onEditorReady = async () => {
      this._attachmentHandle && this._activeWorkflowHandle && (this._attachmentHandle.remove(), this._activeWorkflowHandle.remove());
      const t = this._editor.value;
      this._attachmentHandle = a.when(() => t?.state === "adding-attachment" || t?.state === "editing-attachment" || t?.state === "creating-features", () => {
        this._shouldClose = !1;
      }), this._activeWorkflowHandle = a.watch(() => t?.activeWorkflow, (e) => {
        (e?.type === "update" || e?.type === "create-features") && (this._shouldClose = !1), (!e?.type && !e?.hasPendingEdits && !t?.activeWorkflow || !t?.activeWorkflow?.started) && (this.allowMultipleEditing = !1, this.open = !1);
      }), a.watch(() => t?.state, (e) => {
        e === "creating-features" && this.graphics[0].layer.isTable && !t?.viewModel?.featureTemplatesLayers?.items.length && !t?.viewModel?.featureTemplatesLayers?.items[0]?.isTable && this.creatingFeature.emit(), this._shouldEmitEditsComplete && e === "ready" && !t?.activeWorkflow && (this.editsComplete.emit(), this._shouldEmitEditsComplete = !1);
      }), t && this.editorInitialized.emit(t), this.graphicIndex > -1 && this.graphics.length > 0 && this.open && !this._shouldClose && await this._startUpdate(), this._editorLoading = !1;
    }, this.listenOn(window, "featureSelectionChange", this.featureSelectionChange), this.listenOn(window, "mapChanged", this.mapChanged);
  }
  static {
    this.properties = { enableEditGeometry: 5, enableSnapping: 5, graphicIndex: 9, graphics: 0, mapView: 0, open: 5, allowMultipleEditing: 5, _editorLoading: 16, _layerInfos: 16, _translations: 16 };
  }
  static {
    this.styles = m;
  }
  static {
    this.shadowRootOptions = g;
  }
  async graphicsWatchHandler() {
    this.graphics.length === 0 && this.open && await this._closeEdit(!0);
  }
  async openWatchHandler(t) {
    t && this.graphics?.length > 0 && this.graphicIndex > -1 && (this._editorLoading = !0, await this._loadLayerInfos()), t || await this._closeEdit(!0);
  }
  async featureSelectionChange() {
    this.open && await this._closeEdit(!1);
  }
  async mapChanged() {
    this.open && await this._closeEdit(!1);
  }
  willUpdate(t) {
    if (t.has("graphics") && this.graphicsWatchHandler(), t.has("open") && (this.hasUpdated || this.open !== !1) && this.openWatchHandler(this.open), this.graphics?.length > 0 && this.graphics[0]?.layer) {
      this._layer = this.graphics[0].layer, this._layerEditHandle && this._layerEditHandle.remove();
      const e = this._layer.type === "subtype-sublayer" ? this._layer.parent : this._layer;
      e && (this._layerEditHandle = e.on("edits", () => {
        this._shouldEmitEditsComplete = !0;
      }));
    }
  }
  render() {
    const t = this.graphics?.length > 0 && this.graphics[0] ? !this.graphics[0].layer.editingEnabled : !0, e = this._editorLoading ? "display-none" : "position-absolute", l = this._editorLoading ? "" : "display-none";
    return s`<div class="position-absolute">${t ? s`<calcite-notice kind=warning open slot=content-top width=full><div slot=message>${this._translations.enableEditing}</div></calcite-notice>` : void 0}<div class="position-absolute">${this.open && this.mapView && this._layerInfos !== null && s`<arcgis-editor class=${o(e)} .view=${this.mapView} .layerInfos=${this._layerInfos} .hideSettingsMenu=${!(this.enableSnapping && this.enableEditGeometry)} hide-sketch .hideSnappingControlsElementsEnabledToggle=${!(this.enableSnapping && this.enableEditGeometry)} @arcgisReady=${this._onEditorReady} ${p(this._editor)}></arcgis-editor>` || ""}<calcite-loader class=${o(l)} label scale=s></calcite-loader></div></div>`;
  }
  disconnectedCallback() {
    super.disconnectedCallback(), this._editor.value && this._editor.value.destroy(), this._attachmentHandle?.remove(), this._activeWorkflowHandle?.remove(), this._layerEditHandle?.remove();
  }
  async _loadLayerInfos() {
    if (this.mapView && this.graphics && this.graphics.length > 0 && this.graphics[0]) {
      const t = await h(this.mapView);
      this._layerInfos = t.map((e) => ({
        layer: e,
        geometryUpdatesEnabled: this.enableEditGeometry
      }));
    }
  }
  async _closeEdit(t) {
    this._shouldClose = !0, t && this._editor.value?.activeWorkflow ? (this._editor.value.activeWorkflow?.activeWorkflow?.hasPendingEdits && (await this._editor.value.activeWorkflow.reset(), await this._editor.value.cancelWorkflow()), await this._editor.value.destroy()) : this.graphicIndex > -1 && this.graphics?.length > 0 && this.refreshGraphics.emit(this.graphics), this.enableSnapping && ((this.mapView?.ui.view).grid = null), this._shouldClose = !1, this.closeEdit.emit();
  }
  async _startUpdate() {
    this.allowMultipleEditing ? await this._editor.value?.startUpdateFeaturesWorkflow(this.graphics) : await this._editor.value?.startUpdateWorkflowAtFeatureEdit(this.graphics[this.graphicIndex]), this._shouldClose = !0;
  }
}
r("edit-card", u);
export {
  u as EditCard
};
