/* COPYRIGHT Esri - https://js.arcgis.com/5.1/LICENSE.txt */
import { c as r } from "../../chunks/runtime.js";
import { css as l, html as a } from "lit";
import { LitElement as d, createEvent as s, safeClassMap as n } from "@arcgis/lumina";
import { generateGuid as c } from "@arcgis/toolkit/string";
import { u as h } from "../../chunks/useT9n.js";
import { createRef as p, ref as g } from "lit/directives/ref.js";
import { k as m } from "../../chunks/utils.js";
import "@arcgis/arcade-languageservice";
import "@arcgis/core/portal/PortalItem.js";
import "@arcgis/core/WebMap.js";
import "@arcgis/core/WebScene.js";
import "@arcgis/core/layers/FeatureLayer.js";
import "@arcgis/core/applications/Components/arcadeEditorUtils.js";
import "@arcgis/toolkit/predicate";
import "vscode-languageserver-types";
import { rethrowError as t } from "@arcgis/toolkit/log";
import "@arcgis/core/core/Error.js";
import { d as o } from "../../chunks/language-defaults-base.js";
import "@arcgis/core/portal/Portal.js";
import { g as _, a as f, b as u, c as b } from "../../chunks/sql-layer-defaults.js";
import { getSqlLayerDiagnosticService as v } from "../../chunks/sql-layer-mode.js";
const S = l`:host{height:100%}calcite-tooltip:not([calcite-hydrated])>*{display:none}`;
class E extends d {
  constructor() {
    super(...arguments), this._codeEditorElt = p(), this._disposables = [], this._modelId = c(), this.messages = h(), this._preparing = !1, this._resolvedLanguage = o, this.sideActionBarExpanded = !1, this.script = "", this.hideSideBar = !1, this.openedSidePanel = "none", this.arcgisDiagnosticsChange = s({ bubbles: !1 }), this.arcgisScriptChange = s({ bubbles: !1 });
  }
  static {
    this.properties = { _editorProfile: 16, _preparing: 16, _resolvedLanguage: 16, sideActionBarExpanded: 16, profile: 0, script: 1, hideSideBar: 5, messageOverrides: 0, openedSidePanel: 3 };
  }
  static {
    this.styles = [S];
  }
  async setFocus() {
    await this._codeEditorElt.value?.setFocus();
  }
  async load() {
    await this._updateDataModelDeps().catch(t(this));
  }
  willUpdate(e) {
    e.has("profile") && this._updateDataModelDeps().catch(t(this));
  }
  async loaded() {
    const e = v();
    this._disposables.push(e.onDiagnosticsChange((i) => {
      i.uri.toString() === this._modelId && !this._preparing && this.arcgisDiagnosticsChange.emit(i.diagnostics);
    }));
  }
  disconnectedCallback() {
    for (super.disconnectedCallback(); this._disposables.length; )
      this._disposables.pop()?.dispose();
    _().forEach((e) => e.disposeForModel(this._modelId));
  }
  async _updateDataModelDeps() {
    this._preparing = !0;
    try {
      const e = await f(this.profile);
      e !== this._resolvedLanguage && (u(this._resolvedLanguage)?.disposeForModel(this._modelId), this._resolvedLanguage = e);
      const i = b(this._resolvedLanguage);
      await i.setProfileForModel(this._modelId, this.profile, { locale: this.messages._t9nLocale }), this._editorProfile = i.getEditorProfileForModel(this._modelId);
    } catch {
      this._editorProfile = void 0, this._resolvedLanguage = o;
    } finally {
      this._preparing = !1;
    }
  }
  _toggleSideActionBarExpanded() {
    this.sideActionBarExpanded = !this.sideActionBarExpanded;
  }
  _toggleSidePanel(e) {
    this.openedSidePanel = this.openedSidePanel === e ? "none" : e, this.openedSidePanel === "none" && this._codeEditorElt.value?.setFocus().catch(t(this));
  }
  _onCodeEditorValueChange(e) {
    e.stopPropagation(), this.script = e.detail, this.arcgisScriptChange.emit(e.detail);
  }
  _insertAsText(e) {
    this._codeEditorElt.value?.insertText(e.detail).catch(t(this));
  }
  _getTablesVariable() {
    return this._editorProfile?.variables.find(m);
  }
  render() {
    const e = this._getTablesVariable();
    return a`<arcgis-code-editor-shell><arcgis-code-editor slot=editor .language=${this._resolvedLanguage} .modelId=${this._modelId} .value=${this.script ?? ""} @arcgisValueChange=${this._onCodeEditorValueChange} ${g(this._codeEditorElt)}></arcgis-code-editor>${!this.hideSideBar && a`<arcgis-editor-variables slot=side-panel .variable=${e} .heading=${this.messages.tables ?? "Tables"} .messageOverrides=${{ profileempty: this.messages.profileempty }} .closed=${this.openedSidePanel !== "variables"} class=${n(this.openedSidePanel === "variables" ? "" : "hidden")} .loading=${this._preparing} .modelId=${this._modelId} @arcgisInternalItemSelected=${this._insertAsText} @arcgisInternalClosePanel=${() => {
      this.openedSidePanel = "none";
    }} data-panel-name=none></arcgis-editor-variables><calcite-action-bar slot=side-action-bar .expanded=${this.sideActionBarExpanded} position=end @calciteActionBarToggle=${this._toggleSideActionBarExpanded}><calcite-action id=tables-action .text=${this.messages.tables ?? "Tables"} icon=table .active=${this.openedSidePanel === "variables"} data-panel-name=variables @click=${() => this._toggleSidePanel("variables")}></calcite-action>${!this.sideActionBarExpanded && a`<calcite-tooltip reference-element=tables-action><span>${this.messages.tables ?? "Tables"}</span></calcite-tooltip>` || ""}</calcite-action-bar>` || ""}</arcgis-code-editor-shell>`;
  }
}
r("arcgis-sql-layer-editor", E);
export {
  E as ArcgisSqlLayerEditor
};
