/* COPYRIGHT Esri - https://js.arcgis.com/5.1/LICENSE.txt */
import { c as n } from "../../chunks/runtime.js";
import { css as c, html as t } from "lit";
import { LitElement as l, createEvent as o, safeClassMap as r } from "@arcgis/lumina";
import { generateGuid as d } from "@arcgis/toolkit/string";
import { a } 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 s } from "@arcgis/toolkit/log";
const f = c`:host{height:100%}calcite-tooltip:not([calcite-hydrated])>*{display:none}`;
class u extends l {
  constructor() {
    super(...arguments), this._codeEditorElt = p(), this._disposables = [], this.messages = h(), this._modelId = d(), 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(s(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 a.setProfileForModel(this._modelId, this.profile, { locale: this.messages._t9nLocale }), this._editorProfile = a.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(s(this));
  }
  _insertAsText(i) {
    this._codeEditorElt.value?.insertText(i.detail.replaceAll('"', "'")).catch(s(this));
  }
  async _updateApiLibrary() {
    this._apiLibrary = await a.getApiLibraryForModel(this._modelId);
  }
  _onCodeEditorValueChange(i) {
    i.stopPropagation(), this.script = i.detail, this.arcgisScriptChange.emit(i.detail);
  }
  _getFeatureSetVariable() {
    let i;
    const e = a.getEditorProfileForModel(this._modelId);
    return e?.variables?.length && e?.variables?.[0]?.type !== "featureSet" ? s(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} @arcgisInternalItemSelected=${this._insertAsText} @arcgisInternalClosePanel=${() => 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"} @arcgisInternalItemSelected=${this._insertAsSnippet} @arcgisInternalClosePanel=${() => this._setActiveAction(void 0)} .loading=${this._preparing} .apiLibrary=${this._apiLibrary} data-panel-name=none hide-documentation-actions .languageId=${a.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>`;
  }
}
n("arcgis-sql-expression-editor", u);
export {
  u as ArcgisSqlExpressionEditor
};
