/* COPYRIGHT Esri - https://js.arcgis.com/5.1/LICENSE.txt */
import { c as h } from "../../chunks/runtime.js";
import { u as p } from "../../chunks/useT9n.js";
import { css as u, html as o, nothing as m } from "lit";
import { LitElement as g, createEvent as l, safeStyleMap as y } from "@arcgis/lumina";
import { c as f } from "../../chunks/deployAppContexts.js";
import { useContextConsumer as _ } from "@arcgis/lumina/context";
import * as T from "@esri/solution-common";
const x = u`:host{display:block}.padding-start-1{padding-inline-start:1rem}.border-override{--calcite-accordion-border-color: var(--calcite-color-border-1) !important}.display-flex{display:flex}.align-center{align-items:center}.solution-list-item-label{color:var(--calcite-color-text-1);font-size:var(--calcite-font-size--1)}.solution-list-item-label,.solution-list-item-description{font-family:var(--calcite-font-family);font-weight:var(--calcite-font-weight-normal);overflow-wrap:break-word;word-break:break-word}.solution-list-item-description{font-size:var(--calcite-font-size--2);margin-block-start:.125rem;color:var(--calcite-color-text-3)}.solution-list-item-content{display:flex;flex:1 1 auto;flex-direction:column;justify-content:center;line-height:1.375}.underline{text-decoration:underline}calcite-chip{--calcite-chip-icon-color: var(--calcite-color-brand)}`;
class v extends g {
  constructor() {
    super(...arguments), this.itemRelationshipDependencies = [], this.linkType = "item", this.listSelectionMode = "none", this.reuseWarning = "", this.templateInfos = [], this.typeKeywords = [], this.usageContext = "content", this._canDeletePermission = !1, this._dependencyTooltipContent = "", this._sortedTemplateInfos = [], this._translations = p({ name: "solution-item-accordion", blocking: !0 }), this._configContextConsumer = _({
      context: f,
      subscribe: !0
    }), this._sortOrder = [
      "Hub Site Application",
      "Hub Page",
      "Web Experience",
      "Instant App",
      "Web Mapping Application",
      "Dashboard",
      "StoryMap",
      "Form",
      "QuickCapture Project",
      "Workflow",
      "Big Data Analytic (hosted)",
      "Real Time Analytic (hosted)",
      "Feed (hosted)",
      "Tool",
      "Notebook",
      "Data Pipeline",
      "Project Package",
      "Desktop Application Template",
      "TypeNotFound",
      "Web Map",
      "Web Scene",
      "Feature Layer (hosted, view)",
      "Feature Layer (hosted)",
      "Table (hosted, view)",
      "Table (hosted)",
      "Tile Layer",
      "CSV",
      "Microsoft Excel",
      "Microsoft Word",
      "Report Template",
      "Rule Package",
      "Group"
    ], this.solutionItemHover = l(), this.solutionItemSelect = l(), this.solutionTypeHover = l();
  }
  static {
    this.properties = { itemRelationshipDependencies: 0, linkType: 1, listSelectionMode: 1, reuseWarning: 1, templateInfos: 0, typeKeywords: 0, usageContext: 1, _canDeletePermission: 16, _dependencyTooltipContent: 16, _sortedTemplateInfos: 16, _translations: 16 };
  }
  static {
    this.styles = x;
  }
  willUpdate(e) {
    e.has("templateInfos") && (this.hasUpdated || this.templateInfos?.length > 0) && this.templateInfosWatchHandler();
  }
  async templateInfosWatchHandler() {
    this._sortedTemplateInfos = this._sortTemplates();
  }
  async collapseAll() {
    this.el?.shadowRoot?.querySelectorAll("calcite-accordion-item")?.forEach((i) => {
      i.expanded = !1;
    });
  }
  async deselectAll(e = !1) {
    (this.listSelectionMode === "single" || this.listSelectionMode === "single-persist") && this.el?.shadowRoot?.querySelectorAll("calcite-list-item")?.forEach((t) => {
      t.selected && (t.selected = !1), e && (t.parentElement?.parentElement).expanded && ((t.parentElement?.parentElement).expanded = !1);
    });
  }
  async expandItemById(e) {
    this.el?.shadowRoot?.querySelectorAll("calcite-list-item")?.forEach((t) => {
      t.id === e && ((t.parentElement?.parentElement).expanded = !0);
    });
  }
  async expandFirstItem() {
    const e = this.el?.shadowRoot?.querySelectorAll("calcite-list-item");
    e?.length && ((e[0].parentElement?.parentElement).expanded = !0);
  }
  async expandItems(e) {
    this.el?.shadowRoot?.querySelectorAll("calcite-list-item")?.forEach((t) => {
      e.indexOf(t.id) > -1 && ((t.parentElement?.parentElement).expanded = !0);
    });
  }
  async selectItem(e) {
    (this.listSelectionMode === "single" || this.listSelectionMode === "single-persist") && this.el?.shadowRoot?.querySelectorAll("calcite-list-item")?.forEach((t) => {
      t.id === e && (t.selected = !0, (t.parentElement?.parentElement).expanded = !0);
    });
  }
  async selectFirstItem() {
    const e = this.el?.shadowRoot?.querySelectorAll("calcite-list-item");
    e?.length && (e[0].selected = !0, (e[0].parentElement?.parentElement).expanded = !0);
  }
  async selectItems(e) {
    this.listSelectionMode === "multiple" && this.el?.shadowRoot?.querySelectorAll("calcite-list-item")?.forEach((t) => {
      e.indexOf(t.id) > -1 && (t.selected = !0, (t.parentElement?.parentElement).expanded = !0);
    });
  }
  async load() {
    this._sortedTemplateInfos = this._sortTemplates(), this._canDeletePermission = (this._configContextConsumer.value?.authentication?.CanDeleteSolution && this._configContextConsumer.value?.authentication?.CanUpdateSolution) ?? !1;
  }
  render() {
    return this._getAccordion();
  }
  _deselectOthers(e) {
    (this.listSelectionMode === "single" || this.listSelectionMode === "single-persist") && this.el?.shadowRoot?.querySelectorAll("calcite-list-item")?.forEach((t) => {
      t.id !== e && (t.selected = !1);
    });
  }
  _getAccordion() {
    const e = [], i = this._sortedTemplateInfos.reduce((t, s) => {
      const n = this._getTypeForDisplay(s.type, s.typeKeywords);
      return e.indexOf(n) < 0 && (e.push(n), t.push(this._getAccordionItem(s))), t;
    }, []);
    return o`<calcite-accordion class="border-override" selection-mode=single>${i}</calcite-accordion>`;
  }
  _getAccordionItem(e) {
    const i = this._getTypeForDisplay(e.type, e.typeKeywords), t = this._sortedTemplateInfos.filter((s) => this._getTypeForDisplay(s.type, s.typeKeywords) === i);
    return o`<calcite-accordion-item .heading=${`${i} (${t.length})`} @mouseenter=${() => this.solutionTypeHover.emit({ itemType: i, entering: !0 })} @mouseleave=${() => this.solutionTypeHover.emit({ itemType: i, entering: !1 })}><solution-item-icon class="padding-start-1 display-flex align-center" slot=actions-start .type=${e.type} .typeKeywords=${e.typeKeywords}></solution-item-icon>${this._getList(t)}</calcite-accordion-item>`;
  }
  _getList(e) {
    const i = this._sortTemplatesByTitle(e);
    return o`<calcite-list .label=${this._translations.solutionsTemplates} selection-appearance=border .selectionMode=${this.listSelectionMode}>${i.map((t) => this._getListItem(t))}</calcite-list>`;
  }
  _getListItem(e) {
    const i = this.typeKeywords.includes("Deployed") && e.url, t = this.linkType === "action";
    return o`<calcite-list-item id=${e.id ?? m} @mouseenter=${() => this.solutionItemHover.emit({ itemId: e.id, entering: !0 })} @mouseleave=${() => this.solutionItemHover.emit({ itemId: e.id, entering: !1 })} @calciteListItemSelect=${() => {
      this._deselectOthers(e.id), this.solutionItemSelect.emit({ itemId: e.id, selected: !0 }), i && !t && window.open(e.url, "_blank");
    }} .value=${e.id}><div class="solution-list-item-content" slot=content><div class=${`solution-list-item-label ${i && !t ? "underline" : ""}`}>${e.title}</div><div class="solution-list-item-description">${e.snippet}</div></div>${this._checkUsageForWarnings(e)}${i && t ? o`<div slot=actions-end><calcite-action icon=launch .id=${`${e.id}-launch`} .label=${this._translations?.openDetails} @click=${() => window.open(e.url, "_blank")} text></calcite-action><calcite-tooltip overlay-positioning=fixed .referenceElement=${`${e.id}-launch`}><span>${this._translations?.openDetails}</span></calcite-tooltip></div>` : null}</calcite-list-item>`;
  }
  _sortTemplatesByTitle(e) {
    return e.sort((i, t) => i.title < t.title ? -1 : i.title > t.title ? 1 : 0);
  }
  _sortTemplates() {
    return this.templateInfos.sort((e, i) => {
      const t = this._getTypeForSort(e), s = this._getTypeForSort(i), n = this._sortOrder.indexOf(t), r = this._sortOrder.indexOf(s);
      return n - r;
    });
  }
  _getTypeForSort(e) {
    let i = this._getTypeForDisplay(e.type, e.typeKeywords);
    return this._sortOrder.indexOf(i) < 0 && (i = "TypeNotFound"), i;
  }
  _getTypeForDisplay(e, i) {
    let t = e;
    if (e === "Feature Service") {
      const s = i.indexOf("View Service") > -1, n = i.indexOf("Table") > -1;
      t = s && n ? "Table (hosted, view)" : n ? "Table (hosted)" : s ? "Feature Layer (hosted, view)" : "Feature Layer (hosted)";
    }
    return e === "Web Mapping Application" && (t = i.indexOf("configurableApp") > -1 ? "Instant App" : t), t = e === "Geoprocessing Service" ? "Tool" : t, t = e === "Feed" ? "Feed (hosted)" : t, t = e === "Real Time Analytic" ? "Real Time Analytic (hosted)" : t, t = e === "Big Data Analytic" ? "Big Data Analytic (hosted)" : t, t = e === "Vector Tile Service" ? "Tile Layer" : t, t;
  }
  _getWarningChip(e, i, t, s) {
    const n = i === "info" ? "var(--calcite-color-status-info)" : "var(--calcite-color-status-danger)";
    return o`<div slot=actions-end><calcite-chip .icon=${i === "info" ? "information" : "exclamation-mark-triangle"} .id=${t + "-warning"} kind=neutral .label=${e} scale=s style=${y({ "--calcite-chip-icon-color": n, cursor: s && s.length > 0 ? "pointer" : "default" })}>${e}</calcite-chip>${s && s.length > 0 ? o`<calcite-tooltip class=solution-list-item-content placement=bottom .referenceElement=${`${t}-warning`} scale=m @calciteTooltipBeforeOpen=${async () => {
      const a = {
        q: s.map((d) => `id:${d.dependencyItemId}`).join(" OR "),
        authentication: this._configContextConsumer.value?.userSession,
        pagingParam: { start: 1, num: 100 }
      }, c = await T.searchItems(a);
      this._dependencyTooltipContent = o`<solutions-deployed-list .results=${c} .listTitle=${this._translations?.itemUsedInOtherSolutions || ""}></solutions-deployed-list>`;
    }}>${this._dependencyTooltipContent}</calcite-tooltip>` : null}</div>`;
  }
  _checkUsageForWarnings(e) {
    const i = this._configContextConsumer.value?.userSession?.username ?? null, t = this.itemRelationshipDependencies.filter((n) => n.controlItemId === e.id), s = t.length > 0;
    if (this.usageContext === "content" && s && this.reuseWarning)
      return this._getWarningChip(this.reuseWarning, "info", e.id, t);
    if (this.usageContext === "rename") {
      const n = this._configContextConsumer.value?.authentication?.CanUpdateSolution ?? !1;
      if (e.owner !== i)
        if (n) {
          if (s && this.reuseWarning)
            return this._getWarningChip(this.reuseWarning, "info", e.id, t);
        } else
          return this._getWarningChip(this._translations?.noPrivilegeToManage || "", "warn", e.id);
      else if (s && this.reuseWarning)
        return this._getWarningChip(this.reuseWarning, "info", e.id, t);
    }
    if (this.usageContext === "delete") {
      if (e.owner !== i)
        if (this._canDeletePermission) {
          if (s)
            return this._getWarningChip(this._translations?.usedInOtherSolutionsDeleteWarning || "", "info", e.id);
        } else
          return this._getWarningChip(this._translations?.noPrivilegeToManage || "", "warn", e.id);
      else if (s)
        return this._getWarningChip(this.reuseWarning, "info", e.id, t);
    }
    return "";
  }
}
h("solution-item-accordion", v);
export {
  v as SolutionItemAccordion
};
