import { g as a, c as d } from "../../chunks/runtime.js";
import { i as c } from "../../chunks/monaco-importer.js";
import { observeAncestorsMutation as l, unsafeGetCalciteModeName as h } from "@arcgis/toolkit/dom";
import { generateGuid as u } from "@arcgis/toolkit/string";
import { Deferred as m } from "@arcgis/toolkit/promise";
import { LitElement as p, createEvent as o, noShadowRoot as g } from "@arcgis/lumina";
import { s as _ } from "../../chunks/setup-monaco.js";
import "../../chunks/sql-expr-contribution.js";
import { css as f } from "lit";
import { load as I } from "@arcgis/lumina/controllers";
import { Range as v, Uri as s, KeyMod as n, KeyCode as r } from "monaco-editor";
/*! All material copyright Esri, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.34/esri/copyright.txt for details.
v4.34.0-next.146 */
const b = f`@layer{arcgis-code-editor{display:flex;width:100%;height:100%;flex-direction:column;direction:ltr;.monaco-editor{flex:1 1 auto;.monaco-hover,.editor-widget,.suggest-details-container{z-index:9999}div[widgetid="editor.contrib.resizableContentHoverWidget"],div[widgetid="editor.widget.suggestWidget"],div[widgetid="suggest.details"]{z-index:calc(var(--calcite-z-index-header) + 1)!important}}}}`, M = {
  autoIndent: "advanced",
  automaticLayout: !0,
  bracketPairColorization: { enabled: !0 },
  minimap: { enabled: !1 },
  renderLineHighlight: "none",
  scrollBeyondLastLine: !1,
  suggest: {
    snippetsPreventQuickSuggestions: !1,
    showWords: !1
  },
  tabSize: 2,
  useShadowDOM: !0
};
class y extends p {
  constructor() {
    super(...arguments), this._componentReadyDefer = new m(), this._disposables = [], this._monacoEditor = I(c), this.modelId = u(), this.value = "", this.arcgisSelectionChange = o({ bubbles: !1 }), this.arcgisValueChange = o({ bubbles: !1 });
  }
  static {
    this.properties = { editorInstance: 32, editorOptions: 0, language: 1, modelId: 0, value: 1 };
  }
  static {
    this.shadowRootOptions = g;
  }
  static {
    this.styles = [b];
  }
  get editorInstance() {
    return this._editorInstance;
  }
  async getEditorInstance() {
    return await this._componentReadyDefer.promise, this._editorInstance;
  }
  async getValue() {
    return await this._componentReadyDefer.promise, this.value;
  }
  async insertSnippet(e) {
    if (await this._componentReadyDefer.promise, !this._editorInstance)
      return;
    this._editorInstance.getContribution("snippetController2")?.insert(e ?? ""), this._editorInstance.focus();
  }
  async insertText(e) {
    if (await this._componentReadyDefer.promise, !this._editorInstance || !this._editorInstance.getModel())
      return;
    const i = this._editorInstance.getSelection() ?? new v(1, 1, 1, 1);
    this._editorInstance.executeEdits("", [{ range: i, text: e ?? "", forceMoveMarkers: !0 }]);
  }
  async setFocus() {
    await this._componentReadyDefer.promise, this._editorInstance?.focus();
  }
  async setValue(e) {
    await this._componentReadyDefer.promise, this.value = e ?? "";
  }
  load() {
    _(a("./assets"));
  }
  willUpdate(e) {
    e.has("value") && this._updateModelValue(), (e.has("language") || e.has("modelId")) && this._updateModel(), e.has("editorOptions") && this._editorInstance?.updateOptions(this.editorOptions ?? {});
  }
  loaded() {
    this._monacoEditor.getModel(this.getUri())?.dispose();
    const e = this._monacoEditor.createModel(this.value, this.language, this.getUri());
    this._editorInstance = this._monacoEditor.create(this.el, {
      ...M,
      ...this.editorOptions ?? {},
      model: e,
      fixedOverflowWidgets: !0
    }), this._disposables.push(this._editorInstance), this._componentReadyDefer.resolve(), this._editorInstance.onDidChangeModelContent(() => {
      this.value = this._editorInstance?.getValue() ?? "", this.arcgisValueChange.emit(this.value);
    }), this._editorInstance.onDidChangeCursorSelection((i) => {
      this.arcgisSelectionChange.emit({ selection: i.selection, model: this._editorInstance?.getModel() });
    }), this._themeObserver = l(this.el, ["class"], () => this.updateTheme()), this.updateTheme(), new ResizeObserver(() => this._editorInstance?.layout()).observe(this.el), this.addCommonEditorActions();
  }
  disconnectedCallback() {
    for (super.disconnectedCallback(), this._editorInstance?.getModel()?.dispose(); this._disposables.length; )
      this._disposables.pop()?.dispose();
    this._themeObserver?.();
  }
  _updateModelValue() {
    if (!this._editorInstance)
      return;
    const e = this._editorInstance.getModel();
    if (!e || e.getValue() === this.value)
      return;
    const t = e.getFullModelRange();
    this._editorInstance.executeEdits("", [{ range: t, text: this.value, forceMoveMarkers: !0 }]), this._editorInstance.pushUndoStop();
  }
  _updateModel() {
    if (!this._editorInstance)
      return;
    const e = this._editorInstance;
    e.getModel()?.dispose(), e.setModel(this._monacoEditor.createModel(this.value, this.language, this.getUri()));
  }
  getUri() {
    return this.modelId ? s.parse(this.modelId) : s.parse("");
  }
  updateTheme() {
    const e = h(this.el) === "light" ? "vs" : "vs-dark";
    e !== this._currentTheme && (this._currentTheme = e, this._monacoEditor.setTheme(e));
  }
  addCommonEditorActions() {
    const e = this._editorInstance?.getAction("editor.action.fontZoomIn"), t = this._editorInstance?.getAction("editor.action.fontZoomOut");
    this._editorInstance?.addAction({
      // Use an existing action's ID to override it
      id: "editor.action.fontZoomIn",
      // A label of the action that will be presented to the user.
      label: "Editor Font Zoom In",
      // An optional array of keybindings for the action.
      keybindings: [n.CtrlCmd | r.Equal],
      contextMenuGroupId: "zooming",
      contextMenuOrder: 1,
      // Method that will be executed when the action is triggered.
      // @param editor The editor instance is passed in as a convenience
      run: async () => await e?.run()
    }), this._editorInstance?.addAction({
      id: "editor.action.fontZoomOut",
      label: "Editor Font Zoom Out",
      keybindings: [n.CtrlCmd | r.Minus],
      contextMenuGroupId: "zooming",
      contextMenuOrder: 2,
      run: async () => await t?.run()
    });
  }
}
d("arcgis-code-editor", y);
export {
  y as ArcgisCodeEditor
};
