/* COPYRIGHT Esri - https://js.arcgis.com/1.2/LICENSE.txt */
import { c as n } from "../../chunks/runtime.js";
import { a as d } from "../../chunks/mapViewUtils.js";
import { u as p } from "../../chunks/useT9n.js";
import { css as c, html as r } from "lit";
import { createRef as f, ref as g } from "lit-html/directives/ref.js";
import { LitElement as _, createEvent as s, noShadowRoot as m, safeClassMap as l } from "@arcgis/lumina";
import u from "@arcgis/core/widgets/Editor";
import * as o from "@arcgis/core/core/reactiveUtils";
const w = c`@layer{:host{display:block}#feature-form{padding-top:0}.esri-editor__prompt--danger{position:relative!important;width:100%!important;background-color:var(--calcite-color-foreground-1)!important}.esri-feature__content-node{background-color:var(--calcite-color-foreground-1)!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 y extends _ {
  constructor() {
    super(), this.enableEditGeometry = !1, this.enableSnapping = !1, this.graphicIndex = 0, this.graphics = [], this.open = !1, this.allowMultipleEditing = !1, this._editorLoading = !1, this._translations = p({ name: "edit-card", blocking: !0 }), this._editContainer = f(), this._shouldClose = !1, this._shouldEmitEditsComplete = !1, this.closeEdit = s(), this.editorInitialized = s(), this.editsComplete = s(), this.refreshGraphics = s(), this.creatingFeature = s(), 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, _translations: 16 };
  }
  static {
    this.styles = w;
  }
  static {
    this.shadowRootOptions = m;
  }
  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._initEditorWidget(), this.graphicIndex > -1 && this.graphics.length > 0 && this.open && !this._shouldClose && await this._startUpdate(), this._editorLoading = !1), 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", a = this._editorLoading ? "" : "display-none";
    return r`<div class="position-absolute">${t ? r`<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"><div class=${l(e)} id=feature-form ${g(this._editContainer)}></div><calcite-loader class=${l(a)} label scale=s></calcite-loader></div></div>`;
  }
  async _initEditorWidget() {
    if (this.mapView && this.graphics && this.graphics.length > 0 && this.graphics[0]) {
      this._editor && this._editor.destroy();
      const t = document.createElement("div"), e = await d(this.mapView), a = this.enableSnapping && this.enableEditGeometry, h = e.map((i) => ({
        layer: i,
        geometryUpdatesEnabled: this.enableEditGeometry
      }));
      this._editor = new u({
        view: this.mapView,
        layerInfos: h,
        visibleElements: {
          flow: !0,
          settingsMenu: a,
          sketch: !1,
          snappingControls: a
        },
        container: t
      }), this._attachmentHandle && this._activeWorkflowHandle && (this._attachmentHandle.remove(), this._activeWorkflowHandle.remove()), this._attachmentHandle = o.when(() => this._editor?.viewModel.state === "adding-attachment" || this._editor?.viewModel.state === "editing-attachment" || this._editor?.viewModel.state === "creating-features", () => {
        this._shouldClose = !1;
      }), this._activeWorkflowHandle = o.watch(() => this._editor?.viewModel.activeWorkflow, (i) => {
        (i?.type === "update" || i?.type === "create-features") && (this._shouldClose = !1), (!i?.type && !i?.hasPendingEdits && !this._editor?.activeWorkflow || !this._editor?.activeWorkflow?.started) && (this.allowMultipleEditing = !1, this.open = !1);
      }), o.watch(() => this._editor?.viewModel.state, (i) => {
        i === "creating-features" && this.graphics[0].layer.isTable && !(this._editor?.viewModel).featureTemplatesLayers.items[0].isTable && this.creatingFeature.emit(), this._shouldEmitEditsComplete && i === "ready" && !this._editor?.activeWorkflow && (this.editsComplete.emit(), this._shouldEmitEditsComplete = !1);
      }), this.enableSnapping || t.classList.add("hide-editor-toolbar"), this._editContainer.value && this._editContainer.value.appendChild(t), this._editor && this.editorInitialized.emit(this._editor);
    }
  }
  async _closeEdit(t) {
    this._shouldClose = !0, t && this._editor?.activeWorkflow ? (this._editor.activeWorkflow?.activeWorkflow?.hasPendingEdits && (await this._editor.activeWorkflow.reset(), await this._editor.cancelWorkflow()), this._editor.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?.startUpdateFeaturesWorkflow(this.graphics) : await this._editor?.startUpdateWorkflowAtFeatureEdit(this.graphics[this.graphicIndex]), this._shouldClose = !0;
  }
}
n("edit-card", y);
export {
  y as EditCard
};
