import { c as r } from "../../chunks/runtime.js";
import { css as n, html as t } from "lit";
import { LitElement as c, createEvent as a, noShadowRoot as l, safeClassMap as o } from "@arcgis/lumina";
import { generateGuid as d } from "@arcgis/toolkit/string";
import { a as s } from "../../chunks/sql-expr-defaults.js";
import { u as p } from "../../chunks/useT9n.js";
import { createRef as h, ref as g } from "lit/directives/ref.js";
import { s as f } from "../../chunks/editor.js";
import { getSqlExprDiagnosticService as _ } from "../../chunks/sql-expr-mode.js";
/*! 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.20 */
const u = n`@layer{arcgis-sql-expression-editor{display:flex;flex-direction:column;position:relative;overflow:hidden;border:var(--arcgis-coding-components-border)!important;box-sizing:border-box;background-color:var(--arcgis-coding-components-background-color);height:100%}}`;
class m extends c {
  constructor() {
    super(...arguments), this._codeEditorElt = h(), this._disposables = [], this.messages = p(), this._modelId = d(), this._preparing = !1, this.sideActionBarExpanded = !1, this.script = "", this.hideSideBar = !1, this.arcgisDiagnosticsChange = a({ bubbles: !1 }), this.arcgisScriptChange = a({ bubbles: !1 });
  }
  static {
    this.properties = { _activeAction: 16, _apiLibrary: 16, _editorProfile: 16, _preparing: 16, sideActionBarExpanded: 16, profile: 0, script: 1, hideSideBar: 5 };
  }
  static {
    this.shadowRootOptions = l;
  }
  static {
    this.styles = [u, f];
  }
  async load() {
    await this._updateEditorProfile().catch(console.error);
  }
  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(console.error);
  }
  _insertAsText(i) {
    this._codeEditorElt.value?.insertText(i.detail.replaceAll('"', "'")).catch(console.error);
  }
  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" ? console.error("encountered unexpected editor profile") : i = e?.variables?.[0], i;
  }
  _toggleSideActionBarExpanded() {
    this.sideActionBarExpanded = !this.sideActionBarExpanded;
  }
  render() {
    const i = this._getFeatureSetVariable();
    return t`<div class="flex-row flex-adjustable"><div class="flex-column flex-adjustable"><arcgis-code-editor language=arcgis-sql-expression .modelId=${this._modelId} .value=${this.script ?? ""} @arcgisValueChange=${this._onCodeEditorValueChange} ${g(this._codeEditorElt)}></arcgis-code-editor></div>${!this.hideSideBar && t`<arcgis-editor-variables .variable=${i} .closed=${this._activeAction !== "fields"} class=${o(this._activeAction === "fields" && "code-editor-side-panel flex-panel border-inline-start")} .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 class=${o(this._activeAction === "functions" && "code-editor-side-panel flex-panel border-inline-start")} .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 class="side-action-bar border-inline-start" .expanded=${this.sideActionBarExpanded} position=end @calciteActionBarToggle=${this._toggleSideActionBarExpanded}><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>${!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>` || ""}</div>`;
  }
}
r("arcgis-sql-expression-editor", m);
export {
  m as ArcgisSqlExpressionEditor
};
