/* COPYRIGHT Esri - https://js.arcgis.com/5.1/LICENSE.txt */
import { c as u } from "../../chunks/runtime.js";
import { css as m, 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 { y as D, T as I, z as y } from "../../chunks/interfaces.js";
import { deleteSolution as b } from "@esri/solution-common";
import { c as C } from "../../chunks/deployAppContexts.js";
import { createRef as w, ref as h } from "lit-html/directives/ref.js";
import "../solution-item-accordion/customElement.js";
import { p as $, g as v } from "../../chunks/getRelatedInfos.js";
import { i as x, j as S, h as T } from "../../chunks/helpers.js";
const P = m`*[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-surface-2);--calcite-panel-background-color: var(--calcite-color-surface-2)}.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 E 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: C,
      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 = P;
  }
  get _hasAdminDeletePermission() {
    return this._configContextConsumer.value?.authentication ? D(this._configContextConsumer.value?.authentication) : !1;
  }
  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 (x(this._configContextConsumer.value)) {
      this.close();
      return;
    }
    if (this._telemetry?.logEvent({
      category: I.DEPLOYMENTS,
      action: "Delete Solution",
      dimensions: [
        this.card?.title || "",
        this.card?.deployCommonId || "",
        this.card?.deployVersion || "",
        this.source,
        S(this.card?.typeKeywords)
      ]
    }), this._isDeleteInProgress = !0, this.card) {
      const e = [{
        itemName: this.card.title,
        itemType: "Solution",
        owner: this.card.owner,
        modified: this.card.modified
      }], t = {
        progressCallback: (i) => {
          this._deletePercentage = Math.round(i);
        },
        solutionOwner: this.card.owner
      };
      b(this.card.id, this._configContextConsumer.value.userSession, t).then((i) => {
        const s = i[1];
        let o;
        if (s ? o = s.items.map((a) => ({
          itemName: a.title,
          itemType: a.type,
          owner: a.owner,
          modified: a.modified
        })) : o = e, o.length === 0)
          this.solutionDeleted.emit({ showAlert: !0, refreshSolutionsTimeout: !0, solutionTitle: this.card?.title }), this._isDeleteInProgress = !1, this.open = !1;
        else if (o.some((n) => n.itemType === "Solution"))
          this.card ? $(this.card, this._configContextConsumer.value.userSession).then((n) => {
            this._templateInfos = n, this._showDeleteError = !0, this._showUndeletableItems = !0, this._isDeleteInProgress = !1;
          }) : (this._showDeleteError = !0, this._isDeleteInProgress = !1);
        else if (this.card) {
          let n = [];
          n = s.items.map((l) => ({
            id: l.id,
            title: l.title,
            type: l.type,
            owner: l.owner,
            modified: l.modified,
            typeKeywords: [],
            snippet: "",
            dependencies: [],
            url: y(l.id, this._configContextConsumer.value.authentication)
          })), this._templateInfos = n, 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 t = e.get("open");
      this.open && !t && (this._fetchingTemplates = !0, v(this._configContextConsumer.value.userSession, this.card).then((i) => {
        this._templateInfos = i.templateInfos, this._nonSolutionDependencyItems = i._nonSolutionDependencyItems, this._fetchingTemplates = !1;
      }));
    }
  }
  render() {
    const e = this._fetchingTemplates ? "" : "display-none", t = this._fetchingTemplates ? "display-none" : "", i = T(this._nonSolutionDependencyItems, this.card.id), s = this._templateInfos.length - this._getDependencyCount() - this._getNonOwnerCount(), o = this._translations.deletionCount.replace("{{deleteableCount}}", s.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" role=status tabindex=0>${this._translations.deleteInProgress}</div>` : this._showUndeletableItems ? r`<calcite-label class=${c(t)} layout=inline><span tabindex=0>${this._translations.failedToDeleteItems}</span></calcite-label><solution-item-accordion class=${c(t)} .itemRelationshipDependencies=${[]} link-type=item list-selection-mode=multiple .reuseWarning=${this._translations.itemDependencyReusewarning} .templateInfos=${this._templateInfos} .typeKeywords=${this.card?.typeKeywords} usage-context=delete ${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 ${t}`}><span class="deletion-note-text" tabindex=0>${this._translations.deletionNote}</span><calcite-chip .label=${o} scale=m icon=information>${o}</calcite-chip></div><calcite-loader class=${c(e)} label></calcite-loader><solution-item-accordion class=${c(t)} .itemRelationshipDependencies=${i} link-type=item list-selection-mode=multiple .reuseWarning=${this._translations.itemDependencyReusewarning} .templateInfos=${this._templateInfos} .typeKeywords=${this.card?.typeKeywords} usage-context=delete ${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 auto-close 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).size;
  }
  _getNonOwnerCount() {
    if (this._templateInfos.length === 0 || this._hasAdminDeletePermission)
      return 0;
    const e = this._nonSolutionDependencyItems.map((s) => s.controlItemId), t = new Set(e), i = this._configContextConsumer.value.userSession.username;
    return this._templateInfos.filter((s) => !t.has(s.id)).filter((s) => s.owner !== i).length;
  }
}
u("solution-delete-dialog", E);
export {
  E as SolutionDeleteDialog
};
