import { c as m } from "../../chunks/runtime.js";
import { ref as o } from "lit/directives/ref.js";
import { css as h, html as s } from "lit";
import { setFocusOnElement as u, focusElement as f } from "@arcgis/toolkit/dom";
import { addLtrMark as c } from "@arcgis/toolkit/string";
import { LitElement as p, createEvent as a } from "@arcgis/lumina";
import { g as r, o as g, c as n, a as _ } from "../../chunks/markdown.js";
import { f as b } from "../../chunks/monaco-importer.js";
import { u as v } from "../../chunks/useT9n.js";
import { s as d } from "../../chunks/sql-expr-contribution.js";
/*! 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.121 */
const y = h`:host{background-color:var(--calcite-color-foreground-1);.notice-container{margin:var(--calcite-spacing-xxs);padding:var(--calcite-spacing-sm);background-color:var(--calcite-color-foreground-1)}.sticky{position:sticky;top:0;z-index:10}calcite-flow{height:100%}calcite-block{p:first-of-type{margin-top:0}code{font-family:var(--calcite-code-family)}pre{direction:ltr;overflow-x:auto;background-color:var(--calcite-color-foreground-2);padding:var(--calcite-spacing-sm);code{font-size:var(--calcite-font-size--2)}}}calcite-list-item-group{background-color:var(--calcite-color-foreground-2)}}`;
class I extends p {
  constructor() {
    super(...arguments), this.messages = v(), this._unselectItem = async () => {
      this._selectedApiItem = void 0;
    }, this._colorizeStyle = "", this._filterValue = "", this.languageId = "arcade", this.hideDocumentationActions = !1, this.loading = !1, this.closed = !1, this.arcgisClose = a({ bubbles: !1 }), this.arcgisItemSelected = a({ bubbles: !1 });
  }
  static {
    this.properties = { _colorizeStyle: 16, _filterValue: 16, _selectedApiItem: 16, languageId: 1, apiLibrary: 0, hideDocumentationActions: 5, loading: 5, closed: 5 };
  }
  static {
    this.styles = y;
  }
  async load() {
    await d(this.languageId), this._colorizeStyle = r(), this._themeChangedListener = g(() => this._colorizeStyle = r());
  }
  willUpdate(e) {
    if (e.has("languageId") && d(this.languageId), e.has("apiLibrary")) {
      if (!this._selectedApiItem)
        return;
      for (const i of this.apiLibrary ?? [])
        for (const t of i.items)
          if (t.name === this._selectedApiItem.name) {
            this._selectedApiItem = t;
            return;
          }
      this._selectedApiItem = void 0;
    }
  }
  disconnectedCallback() {
    super.disconnectedCallback(), this._flowObserver?.disconnect(), this._themeChangedListener?.dispose();
  }
  flowFocusHandler(e) {
    if (!e) {
      this._flowObserver?.disconnect(), this._flowObserver = void 0;
      return;
    }
    this._flowObserver || (this._flowObserver = new MutationObserver(() => {
      u(e.querySelector("calcite-flow-item:last-child"), "calcite-input");
    }), this._flowObserver.observe(e, { attributes: !0, attributeFilter: ["id"], childList: !0 }));
  }
  _emitItemSelected(e, i) {
    if (!i)
      return;
    const t = e;
    (!t.key || t.key === "Enter") && (e.preventDefault(), this.arcgisItemSelected.emit(i.completion.insertText ?? ""));
  }
  _emitClose() {
    this.arcgisClose.emit();
  }
  _emitItemSelectedAndClose(e, i) {
    const t = e;
    (!t.key || t.key === "Enter") && (e.preventDefault(), this.arcgisItemSelected.emit(i.completion.insertText ?? ""));
  }
  _selectItem(e, i) {
    e.stopPropagation(), this._selectedApiItem = i;
  }
  _updateFilterValue(e) {
    this._filterValue = e.currentTarget.value ?? "";
  }
  _elementToMarkdown(e) {
    e !== void 0 && (e.innerHTML = n(this._selectedApiItem.examples), _(e, this.languageId).catch(console.error));
  }
  renderApiCategory(e) {
    const i = b(e.items, "name", this._filterValue);
    return i.length ? s`<calcite-list-item-group .heading=${e.title}>${i.map((t) => s`<calcite-list-item .label=${c(t.completion.detail)} @calciteListItemSelect=${(l) => this._emitItemSelected(l, t)} @keydown=${(l) => this._emitItemSelected(l, t)}><calcite-action slot=actions-end .text=${this.messages.expand ?? ""} icon=chevron-right icon-flip-rtl scale=s @click=${(l) => this._selectItem(l, t)}></calcite-action></calcite-list-item>`)}</calcite-list-item-group>` : null;
  }
  renderApiItemFlowItem() {
    const e = this._selectedApiItem;
    return e ? s`<calcite-flow-item .heading=${this.messages.constantsandfunctions} .beforeBack=${this._unselectItem} closable @calciteFlowItemClose=${this._emitClose} selected>${this.hideDocumentationActions ? null : s`<calcite-button width=half slot=footer appearance=outline kind=brand scale=s .href=${e.link} target=Help icon-start=information>${this.messages.moreinformation}</calcite-button>`}<calcite-button .width=${this.hideDocumentationActions ? "full" : "half"} slot=footer appearance=outline kind=brand icon-start=code scale=s @click=${(i) => this._emitItemSelectedAndClose(i, e)} ${o(f)}>${this.messages.insert}</calcite-button><calcite-block open .heading=${c(e.completion.detail)}><div .innerHTML=${n(e.completion.documentation) ?? ""}></div>${e.examples ? s`<div ${o(this._elementToMarkdown)}></div>` : null}</calcite-block></calcite-flow-item>` : null;
  }
  render() {
    if (this.closed)
      return null;
    const e = (this.apiLibrary ?? []).map((t) => this.renderApiCategory(t)), i = e.every((t) => !t);
    return s`<style>${this._colorizeStyle}</style><calcite-flow ${o(this.flowFocusHandler)}><calcite-flow-item .heading=${this.messages.constantsandfunctions} closable @calciteFlowItemClose=${this._emitClose} .selected=${!this._selectedApiItem}><calcite-input .value=${this._filterValue} icon=magnifying-glass clearable @calciteInputInput=${this._updateFilterValue} class="sticky" scale=m></calcite-input>${this.loading ? s`<calcite-loader scale=s type=indeterminate .label=${this.messages.loading ?? "Loading"}></calcite-loader>` : s`<calcite-list label="arcade api">${i ? s`<div class="notice-container">${this.messages.noitems}</div>` : e}</calcite-list>`}</calcite-flow-item>${this.renderApiItemFlowItem()}</calcite-flow>`;
  }
}
m("arcgis-language-api-panel", I);
export {
  I as ArcgisLanguageApiPanel
};
