import { c as n } from "../../chunks/runtime.js";
import { ref as c } from "lit/directives/ref.js";
import { css as m, html as s } from "lit";
import { setFocusOnElement as d, focusElement as u } from "@arcgis/toolkit/dom";
import { addLtrMark as a } from "@arcgis/toolkit/string";
import { LitElement as h, createEvent as o } from "@arcgis/lumina";
import { f } from "../../chunks/utilities.js";
import { c as r } from "../../chunks/markdown.js";
import { u as p } from "../../chunks/useT9n.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.125 */
const g = m`: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}}calcite-list-item-group{background-color:var(--calcite-color-foreground-2)}calcite-flow{[slot=footer]{word-break:break-word}}}`;
class b extends h {
  constructor() {
    super(...arguments), this.messages = p(), this._unselectItem = async () => {
      this._selectedApiItem = void 0;
    }, this._filterValue = "", this.languageId = "arcade", this.hideDocumentationActions = !1, this.loading = !1, this.closed = !1, this.arcgisClose = o({ bubbles: !1 }), this.arcgisItemSelected = o({ bubbles: !1 });
  }
  static {
    this.properties = { _filterValue: 16, _selectedApiItem: 16, languageId: 1, apiLibrary: 0, hideDocumentationActions: 5, loading: 5, closed: 5 };
  }
  static {
    this.styles = g;
  }
  willUpdate(e) {
    if (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();
  }
  flowFocusHandler(e) {
    if (!e) {
      this._flowObserver?.disconnect(), this._flowObserver = void 0;
      return;
    }
    this._flowObserver || (this._flowObserver = new MutationObserver(() => {
      d(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 = r(this._selectedApiItem.examples));
  }
  renderApiCategory(e) {
    const i = f(e.items, "name", this._filterValue);
    return i.length ? s`<calcite-list-item-group .heading=${e.title}>${i.map((t) => s`<calcite-list-item .label=${a(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-level=2 .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)} ${c(u)}>${this.messages.insert}</calcite-button><calcite-block expanded .heading=${a(e.completion.detail)}><div .innerHTML=${r(e.completion.documentation) ?? ""}></div>${e.examples ? s`<div ${c(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`<calcite-flow ${c(this.flowFocusHandler)}><calcite-flow-item heading-level=2 .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>`;
  }
}
n("arcgis-language-api-panel", b);
export {
  b as ArcgisLanguageApiPanel
};
