import { c } from "../../chunks/runtime.js";
import { css as n, html as t } from "lit";
import { LitElement as d, createEvent as o, safeClassMap as r } from "@arcgis/lumina";
import { generateGuid as l } from "@arcgis/toolkit/string";
import { a as s } from "../../chunks/sql-expr-defaults.js";
import { u as h } from "../../chunks/useT9n.js";
import { createRef as p, ref as g } from "lit/directives/ref.js";
import { getSqlExprDiagnosticService as _ } from "../../chunks/sql-expr-mode.js";
import { rethrowError as a } from "@arcgis/toolkit/log";
/*! All material copyright Esri, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/5.0/esri/copyright.txt for details.
v5.0.0-next.128 */
const f = n`:host{height:100%}calcite-tooltip:not([calcite-hydrated])>*{display:none}`;
class u extends d {
  constructor() {
    super(...arguments), this._codeEditorElt = p(), this._disposables = [], this.messages = h(), this._modelId = l(), this._preparing = !1, this.sideActionBarExpanded = !1, this.script = "", this.hideSideBar = !1, this.arcgisDiagnosticsChange = o({ bubbles: !1 }), this.arcgisScriptChange = o({ bubbles: !1 });
  }
  static {
    this.properties = { _activeAction: 16, _apiLibrary: 16, _editorProfile: 16, _preparing: 16, sideActionBarExpanded: 16, profile: 0, script: 1, hideSideBar: 5 };
  }
  static {
    this.styles = [f];
  }
  async setFocus() {
    await this._codeEditorElt.value?.setFocus();
  }
  async load() {
    await this._updateEditorProfile().catch(a(this));
  }
  willUpdate(i) {
    (i.has("messages") || i.has("profile")) && this._updateDataModelDeps();
  }
  async loaded() {
    const i = await _();
    this._disposables.push(i.onDiagnosticsChange((e) => !this._preparing && this.arcgisDiagnosticsChange.emit(e.diagnostics)));
  }
  disconnectedCallback() {
    for (super.disconnectedCallback(); this._disposables.length; )
      this._disposables.pop()?.dispose();
  }
  async _updateDataModelDeps() {
    await this._updateEditorProfile(), await this._updateApiLibrary();
  }
  async _updateEditorProfile() {
    this._preparing = !0;
    try {
      await s.setProfileForModel(this._modelId, this.profile, { locale: this.messages._t9nLocale }), this._editorProfile = s.getEditorProfileForModel(this._modelId);
    } catch {
      this._editorProfile = void 0;
    } finally {
      this._preparing = !1;
    }
  }
  _setActiveAction(i) {
    if (this._activeAction === i) {
      this._activeAction = void 0;
      return;
    }
    this._activeAction = i;
  }
  _handleActionClick(i) {
    const e = i.target;
    this._setActiveAction(e.dataset.panelName);
  }
  _insertAsSnippet(i) {
    this._codeEditorElt.value?.insertSnippet(i.detail).catch(a(this));
  }
  _insertAsText(i) {
    this._codeEditorElt.value?.insertText(i.detail.replaceAll('"', "'")).catch(a(this));
  }
  async _updateApiLibrary() {
    this._apiLibrary = await s.getApiLibraryForModel(this._modelId);
  }
  _onCodeEditorValueChange(i) {
    i.stopPropagation(), this.script = i.detail, this.arcgisScriptChange.emit(i.detail);
  }
  _getFeatureSetVariable() {
    let i;
    const e = s.getEditorProfileForModel(this._modelId);
    return e?.variables?.length && e?.variables?.[0]?.type !== "featureSet" ? a(this)("Encountered unexpected editor profile") : i = e?.variables?.[0], i;
  }
  _toggleSideActionBarExpanded() {
    this.sideActionBarExpanded = !this.sideActionBarExpanded;
  }
  render() {
    const i = this._getFeatureSetVariable();
    return t`<arcgis-code-editor-shell><arcgis-code-editor slot=editor language=arcgis-sql-expression .modelId=${this._modelId} .value=${this.script ?? ""} @arcgisValueChange=${this._onCodeEditorValueChange} ${g(this._codeEditorElt)}></arcgis-code-editor>${!this.hideSideBar && i && t`<arcgis-editor-variables slot=side-panel .variable=${i} .closed=${this._activeAction !== "fields"} class=${r(this._activeAction === "fields" ? "" : "hidden")} .loading=${this._preparing} .modelId=${this._modelId} @arcgisItemSelected=${this._insertAsText} @arcgisClose=${() => this._setActiveAction(void 0)} data-panel-name=none></arcgis-editor-variables>` || ""}${!this.hideSideBar && t`<arcgis-language-api-panel slot=side-panel class=${r(this._activeAction === "functions" ? "" : "hidden")} .closed=${this._activeAction !== "functions"} @arcgisItemSelected=${this._insertAsSnippet} @arcgisClose=${() => this._setActiveAction(void 0)} .loading=${this._preparing} .apiLibrary=${this._apiLibrary} data-panel-name=none hide-documentation-actions .languageId=${s.languageId}></arcgis-language-api-panel>` || ""}${!this.hideSideBar && t`<calcite-action-bar slot=side-action-bar .expanded=${this.sideActionBarExpanded} position=end @calciteActionBarToggle=${this._toggleSideActionBarExpanded}>${i && t`<calcite-action id=fields-action .text=${this.messages.fields ?? "Fields"} icon=profile-variables .active=${this._activeAction === "fields"} data-panel-name=fields @click=${this._handleActionClick}></calcite-action>` || ""}${i && !this.sideActionBarExpanded && t`<calcite-tooltip reference-element=fields-action><span>${this.messages.fields ?? "Fields"}</span></calcite-tooltip>` || ""}<calcite-action id=functions-action .text=${this.messages.functions ?? "Functions"} icon=function .active=${this._activeAction === "functions"} data-panel-name=functions @click=${this._handleActionClick}></calcite-action>${!this.sideActionBarExpanded && t`<calcite-tooltip reference-element=functions-action><span>${this.messages.functions ?? "Functions"}</span></calcite-tooltip>` || ""}</calcite-action-bar>` || ""}</arcgis-code-editor-shell>`;
  }
}
c("arcgis-sql-expression-editor", u);
export {
  u as ArcgisSqlExpressionEditor
};
