/* COPYRIGHT Esri - https://js.arcgis.com/1.2/LICENSE.txt */
import { c as m } from "../../chunks/runtime.js";
import { css as u, nothing as p, html as r } from "lit";
import { u as f } from "../../chunks/useT9n.js";
import { LitElement as g, createEvent as d, safeClassMap as c } from "@arcgis/lumina";
import { useContextConsumer as _ } from "@arcgis/lumina/context";
import { A as D, T as I, D as b } from "../../chunks/interfaces.js";
import { deleteSolution as y } from "@esri/solution-common";
import { createRef as w, ref as h } from "lit-html/directives/ref.js";
import "../solution-item-accordion/customElement.js";
import { p as $, g as C } from "../../chunks/getRelatedInfos.js";
const v = u`*[tabindex]:focus-visible{outline-style:solid!important;outline-width:2px!important;outline-color:var(--calcite-color-brand)!important;outline-offset:2px!important}.deletion-note-text{display:inline-block}.deletion-note-container{display:flex;align-items:center;gap:var(--calcite-spacing-sm);font-size:var(--calcite-font-size-0);margin-bottom:var(--calcite-spacing-md)}.panel-bg-color{--calcite-panel-header-background-color: var(--calcite-color-foreground-1);--calcite-panel-background-color: var(--calcite-color-foreground-1)}.display-none{display:none}calcite-loader{--calcite-loader-spacing: var(--calcite-spacing-md)}.loader-text{margin-top:var(--calcite-spacing-md);margin-bottom:var(--calcite-spacing-xl);text-align:center;font-size:var(--calcite-font-size-relative-base);color:var(--calcite-color-text-1)}.padding-around{padding:12px 2rem 2rem}`;
class x extends g {
  constructor() {
    super(), this.open = !1, this.source = "Card Dropdown", this._deletePercentage = 0, this._fetchingTemplates = !1, this._isDeleteInProgress = !1, this._nonSolutionDependencyItems = [], this._showDeleteError = !1, this._showUndeletableItems = !1, this._templateInfos = [], this._translations = f({ name: "solution-delete-dialog", blocking: !0 }), this._accordion = w(), this._configContextConsumer = _({
      context: D,
      subscribe: !0
    }), this.solutionDeleteDialogOpen = d(), this.solutionDeleted = d(), this.solutionDeleteDialogBeforeClose = d(), this.listenOn(window, "beforeunload", (e) => {
      this._isDeleteInProgress && (e.preventDefault(), e.returnValue = !0);
    });
  }
  static {
    this.properties = { card: 0, open: 5, source: 1, _deletePercentage: 16, _fetchingTemplates: 16, _isDeleteInProgress: 16, _nonSolutionDependencyItems: 16, _showDeleteError: 16, _showUndeletableItems: 16, _templateInfos: 16, _translations: 16 };
  }
  static {
    this.styles = v;
  }
  get _telemetry() {
    return this._configContextConsumer.value?.telemetrySession;
  }
  close() {
    this._showUndeletableItems && this.solutionDeleted.emit({ showAlert: !1, refreshSolutionsTimeout: !0, solutionTitle: this.card?.title }), this._showDeleteError = !1, this._showUndeletableItems = !1, this._isDeleteInProgress = !1, this.open = !1, this.solutionDeleteDialogOpen.emit(!1);
  }
  delete() {
    if (this._telemetry?.logEvent({
      category: I.DEPLOYMENTS,
      action: "Delete Solution",
      dimensions: [
        this.card?.title || "",
        this.card?.deployCommonId || "",
        this.card?.deployVersion || "",
        this.source
      ]
    }), this._isDeleteInProgress = !0, this.card) {
      const e = [{
        itemName: this.card.title,
        itemType: "Solution",
        owner: this.card.owner,
        modified: this.card.modified
      }], s = {
        progressCallback: (t) => {
          this._deletePercentage = Math.round(t);
        },
        solutionOwner: this.card.owner
      };
      y(this.card.id, this._configContextConsumer.value.userSession, s).then((t) => {
        const a = t[1];
        let i;
        if (a ? i = a.items.map((o) => ({
          itemName: o.title,
          itemType: o.type,
          owner: o.owner,
          modified: o.modified
        })) : i = e, i.length === 0)
          this.solutionDeleted.emit({ showAlert: !0, refreshSolutionsTimeout: !0, solutionTitle: this.card?.title }), this._isDeleteInProgress = !1, this.open = !1;
        else if (i.some((l) => l.itemType === "Solution"))
          this.card ? $(this.card, this._configContextConsumer.value.userSession).then((l) => {
            this._templateInfos = l, this._showDeleteError = !0, this._showUndeletableItems = !0, this._isDeleteInProgress = !1;
          }) : (this._showDeleteError = !0, this._isDeleteInProgress = !1);
        else if (this.card) {
          let l = [];
          l = a.items.map((n) => ({
            id: n.id,
            title: n.title,
            type: n.type,
            owner: n.owner,
            modified: n.modified,
            typeKeywords: [],
            snippet: "",
            dependencies: [],
            url: b(n.id, this._configContextConsumer.value.authentication)
          })), this._templateInfos = l, this._showDeleteError = !1, this._showUndeletableItems = !0, this._isDeleteInProgress = !1;
        } else
          this.solutionDeleted.emit({ showAlert: !0, refreshSolutionsTimeout: !0 }), this._isDeleteInProgress = !1, this.open = !1;
      });
    }
  }
  willUpdate(e) {
    if (e.has("open")) {
      const s = e.get("open");
      this.open && !s && (this._fetchingTemplates = !0, C(this._configContextConsumer.value.userSession, this.card).then((t) => {
        this._templateInfos = t.templateInfos, this._nonSolutionDependencyItems = t._nonSolutionDependencyItems, this._fetchingTemplates = !1;
      }));
    }
  }
  render() {
    const e = this._fetchingTemplates ? "" : "display-none", s = this._fetchingTemplates ? "display-none" : "", t = [...new Set(this._nonSolutionDependencyItems.map((o) => o.controlItemId))], a = this._templateInfos.length - this._getDependencyCount(), i = this._translations.deletionCount.replace("{{deleteableCount}}", a.toString()).replace("{{totalCount}}", this._templateInfos.length.toString());
    return r`<calcite-dialog class="panel-bg-color" .closeDisabled=${this._isDeleteInProgress} .escapeDisabled=${this._isDeleteInProgress} .heading=${this._translations.title.replace("{{solution}}", this.card?.title ?? "")} id=${this._translations.title.replace("{{solution}}", this.card?.title ?? "") ?? p} modal kind=danger .open=${this.open} width=l outside-close-disabled @calciteDialogBeforeClose=${() => {
      this.solutionDeleteDialogBeforeClose.emit();
    }} @calciteDialogClose=${() => {
      this.open = !1, this.solutionDeleteDialogOpen.emit(!1);
    }}>${this._isDeleteInProgress ? r`<calcite-loader .label=${this._translations.deleteInProgress} scale=l type=determinate-value .value=${this._deletePercentage}></calcite-loader><div class="loader-text" tabindex=0>${this._translations.deleteInProgress}</div>` : this._showUndeletableItems ? r`<calcite-label class=${c(s)} layout=inline><span tabindex=0>${this._translations.failedToDeleteItems}</span></calcite-label><solution-item-accordion class=${c(s)} .itemRelationshipDependencies=${[]} link-type=item list-selection-mode=multiple .reuseWarning=${this._translations.itemDependencyReusewarning} .templateInfos=${this._templateInfos} .typeKeywords=${this.card?.typeKeywords} ${h(this._accordion)}></solution-item-accordion><calcite-button appearance=outline @click=${this.close} slot=footer-end>${this._translations.buttonClose}</calcite-button>` : r`<div class="padding-around"><div class=${`deletion-note-container ${s}`}><span class="deletion-note-text" tabindex=0>${this._translations.deletionNote}</span><calcite-chip .label=${i} scale=m icon=information>${i}</calcite-chip></div><calcite-loader class=${c(e)} label></calcite-loader><solution-item-accordion class=${c(s)} .itemRelationshipDependencies=${t} link-type=item list-selection-mode=multiple .reuseWarning=${this._translations.itemDependencyReusewarning} .templateInfos=${this._templateInfos} .typeKeywords=${this.card?.typeKeywords} ${h(this._accordion)}></solution-item-accordion></div><calcite-button appearance=outline @click=${this.close} slot=footer-end>${this._translations.buttonCancel}</calcite-button><calcite-button @click=${() => {
      this.delete();
    }} slot=footer-end kind=danger>${this._translations.buttonDelete}</calcite-button>${this._showDeleteError && r`<calcite-alert icon=exclamation-mark-triangle kind=danger .label=${this._translations.failedToDelete} @calciteAlertClose=${() => {
      this._showDeleteError = !1;
    }} open><div slot=message>${this._translations.failedToDelete}</div></calcite-alert>` || ""}`}</calcite-dialog>`;
  }
  _getDependencyCount() {
    const e = this._nonSolutionDependencyItems.map((t) => t.controlItemId);
    return [...new Set(e)].length;
  }
}
m("solution-delete-dialog", x);
export {
  x as SolutionDeleteDialog
};
