import { c as y } from "../../chunks/runtime.js";
import { css as B, html as i, nothing as M } from "lit";
import { createRef as E, ref as w } from "lit/directives/ref.js";
import { LitElement as A, createEvent as g } from "@arcgis/lumina";
/*! All material copyright Esri, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.34/esri/copyright.txt for details.
v4.34.7 */
const T = B`:host{display:none;background:#bdf2c4}:host([selected]){display:flex}calcite-block{margin-block:0}calcite-pagination{justify-content:center}`;
class U extends A {
  constructor() {
    super(...arguments), this._filterInputElement = E(), this._flowItemElement = E(), this.currentPage = 1, this.selected = !1, this.showBackButton = !1, this.arcgisFlowItemBack = g(), this.arcgisFlowItemClose = g(), this.arcgisGetVersions = g(), this.arcgisManageVersion = g(), this.arcgisNewVersion = g();
  }
  static {
    this.properties = { _filteredVersionInfos: 16, currentPage: 11, selected: 7, showBackButton: 7, versionListElementProps: 0 };
  }
  static {
    this.styles = T;
  }
  load() {
    this._filteredVersionInfos = this.versionListElementProps.versionInfos;
  }
  willUpdate(e) {
    if (e.has("versionListElementProps")) {
      const t = e.get("versionListElementProps"), n = this.versionListElementProps;
      t?.versionInfos !== n.versionInfos && (this._filteredVersionInfos = n.versionInfos, this._filterInputElement.value && this._handleFilterChange());
    }
  }
  updated() {
    this._filterInputElement.value?.setFocus();
  }
  async setFocus() {
    this._flowItemElement.value?.setFocus();
  }
  goBack() {
  }
  _handleFilterChange() {
    const { versionListElementProps: { versionInfos: e } } = this, t = this._filterInputElement.value.value.toUpperCase();
    this._filteredVersionInfos = t === "" ? e : e.filter((n) => n.versionIdentifier.name.toUpperCase().includes(t) || n.versionIdentifier.guid.toUpperCase().includes(t)), this._filteredVersionInfos.length && (this.currentPage = 1);
  }
  _handleNoticeClosed() {
    this.versionListElementProps = { ...this.versionListElementProps, executionError: void 0 };
  }
  _handlePaginationChange(e) {
    this.currentPage = e.target.startItem;
  }
  _getExecutionError(e) {
    const { versionListElementProps: { strings: { executionErrors: t } } } = this;
    switch (e) {
      case "no-feature-service-found":
        return t.noFeatureServiceFound;
      case "no-valid-enterprise-version":
        return t.notValidEnterpriseVersion;
      case "no-valid-version-name":
        return t.invalidVersionName;
      default:
        return e;
    }
  }
  _paginate(e, t, n) {
    return e.slice((n - 1) * t, n * t);
  }
  render() {
    const { currentPage: e, selected: t, showBackButton: n, versionListElementProps: { closable: u, currentUser: V, currentVersionIdentifier: c, executionError: l, heading: s, isVersionAdministrator: d, isVersioningApiAvailable: p, pageSize: o, serviceName: m, serviceUrl: h, state: f, strings: { headers: a }, strings: r, versionInfos: v } } = this, $ = i`<calcite-block .heading=${a.currentVersion} .description=${c.name} overlay-positioning=fixed></calcite-block>`, I = i`<calcite-action id=actionRefreshVersions icon=refresh text @click=${() => this.arcgisGetVersions.emit({ serviceUrl: h })} slot=header-actions-end><calcite-tooltip overlay-positioning=fixed reference-element=actionRefreshVersions><span>${r.actions.refreshVersions}</span></calcite-tooltip></calcite-action>`;
    this._paginatedVersionInfos = this._filteredVersionInfos.length > o ? this._paginate(this._filteredVersionInfos, o, e) : this._filteredVersionInfos;
    const _ = this._filteredVersionInfos.length > o ? i`<calcite-pagination @calcitePaginationChange=${this._handlePaginationChange} page-size=1 .startItem=${e} .totalItems=${Math.ceil(this._filteredVersionInfos.length / o)}></calcite-pagination>` : void 0, b = this._paginate(this._paginatedVersionInfos, o, 1).map((L) => {
      const F = {
        currentUser: V,
        currentVersionIdentifier: c,
        isCurrentVersionCard: !1,
        isVersionAdministrator: d,
        isVersioningApiAvailable: p,
        serviceUrl: h,
        strings: r,
        versionInfo: L
      };
      return this._renderListItem(F);
    }), x = v.length > o ? i`<calcite-block heading .label=${a.filterVersions} open><calcite-input-text clearable icon=search @calciteInputTextInput=${this._handleFilterChange} .placeholder=${r.actions.searchVersions} ${w(this._filterInputElement)}></calcite-input-text></calcite-block>` : void 0, k = i`<calcite-list .label=${r.labels.versionList}>${b}</calcite-list>`, C = p ? i`<calcite-fab appearance=outline-fill slot=fab text-enabled .text=${r.actions.newVersion} @click=${() => this.arcgisNewVersion.emit({ serviceUrl: h })}></calcite-fab>` : void 0, P = l ? i`<calcite-notice @calciteNoticeClose=${this._handleNoticeClosed} class="notice" closable kind=warning open scale=s slot=footer width=full><div slot=message>${this._getExecutionError(l)}</div></calcite-notice>` : void 0;
    return i`<calcite-flow-item .closable=${u} .description=${m} .heading=${s} .selected=${t} .showBackButton=${n} @calciteFlowItemBack=${() => this.arcgisFlowItemBack.emit()} @calciteFlowItemClose=${() => this.arcgisFlowItemClose.emit()} ${w(this._flowItemElement)}><calcite-panel .loading=${f === "executing"}>${$}<calcite-panel .heading=${this._filteredVersionInfos.length ? `${r.headers.manageVersions} (${this._filteredVersionInfos.length})` : r.headers.manageVersions}>${I}${x}${k}${C}</calcite-panel>${_}${P}</calcite-panel></calcite-flow-item>`;
  }
  _renderListItem(e) {
    const { currentUser: t, currentVersionIdentifier: n, isVersionAdministrator: u, isVersioningApiAvailable: V, serviceUrl: c, strings: { actions: l }, versionInfo: s } = e, d = s.versionIdentifier.name, p = d.substring(d.lastIndexOf(".") + 1), o = d.substring(0, d.lastIndexOf(".")), m = s.versionIdentifier.guid, h = `actionSwitchToVersion_${m}`, f = o.toUpperCase() === t.toUpperCase() && V || u, a = n.guid === s.versionIdentifier.guid, r = i`<calcite-action .disabled=${a} icon=code-branch @click=${() => {
      this.arcgisManageVersion.emit({ actionType: "changeVersion", serviceUrl: c, versionInfo: s });
    }} scale=s .text=${l.switchToVersion} text-enabled></calcite-action>`, v = i`<calcite-action .disabled=${!f || f && a} icon=trash @click=${() => {
      this.arcgisManageVersion.emit({ actionType: "deleteVersion", serviceUrl: c, versionInfo: s });
    }} scale=s .text=${l.deleteVersion} text-enabled></calcite-action>`, $ = i`<calcite-action .disabled=${!f || a} icon=code-branch-edit @click=${() => {
      this.arcgisManageVersion.emit({ actionType: "editVersion", serviceUrl: c, versionInfo: s });
    }} scale=s .text=${l.editVersion} text-enabled></calcite-action>`, I = i`<calcite-action-menu overlay-positioning=fixed slot=actions-end label>${r}${$}${v}</calcite-action-menu>`, _ = a ? void 0 : i`<calcite-tooltip overlay-positioning=fixed .referenceElement=${h}><span>${l.switchToVersion}</span></calcite-tooltip>`, b = i`<div slot=actions-start><calcite-action appearance=transparent .disabled=${a} id=${h ?? M} icon=code-branch text @click=${() => {
      this.arcgisManageVersion.emit({ actionType: "changeVersion", serviceUrl: c, versionInfo: s });
    }}></calcite-action>${_}</div>`;
    return i`<calcite-list-item .description=${o} .label=${p} .value=${m}>${b}${I}</calcite-list-item>`;
  }
}
y("arcgis-version-management-version-list", U);
export {
  U as ArcgisVersionManagementVersionList
};
