/* COPYRIGHT Esri - https://js.arcgis.com/5.1/LICENSE.txt */
import { c as v } from "../../chunks/runtime.js";
import { keyed as x } from "lit/directives/keyed.js";
import { repeat as S } from "lit/directives/repeat.js";
import { u as w } from "../../chunks/useT9n.js";
import { LitElement as C, createEvent as F, safeClassMap as c, safeStyleMap as h } from "@arcgis/lumina";
import { useContextConsumer as f } from "@arcgis/lumina/context";
import { css as $, html as a } from "lit";
import { c as R, i as T } from "../../chunks/deployAppContexts.js";
import { e as _, d as g, T as m } from "../../chunks/interfaces.js";
import { createRef as d, ref as n } from "lit/directives/ref.js";
import "../solutions-filter-panel/customElement.js";
const M = $`.display-flex{display:flex}.border-override{border-top-width:var(--calcite-border-width-none);border-bottom-width:var(--calcite-border-width-none)}.header-override{--calcite-panel-header-content-space: 0px;--calcite-panel-header-background-color: var(--calcite-color-surface-3)}.panel-override{--calcite-panel-background-color: var(--calcite-color-surface-3);--calcite-panel-border-color: none}.label-override{--calcite-label-margin-bottom: 0px;font-weight:var(--calcite-font-weight-medium)}.header-button-padding{padding-top:var(--calcite-spacing-sm);padding-bottom:var(--calcite-spacing-sm);padding-inline-start:var(--calcite-spacing-lg);padding-inline-end:var(--calcite-spacing-lg)}.padding-start{padding-inline-start:var(--calcite-spacing-sm)}.align-items-center{align-items:center}.justify-content-end{justify-content:end}.filter-width-350{--calcite-shell-panel-width: 350px}.z-index-1{z-index:1}.z-index-2{z-index:2}h1{font-weight:unset;margin:0;padding:0}@container (max-width: 800px){.filter{display:none}}.mock-fab{--calcite-fab-shadow-internal: 0 6px 20px -4px rgba(0, 0, 0, .1), 0 4px 12px -2px rgba(0, 0, 0, .08);box-shadow:var(--calcite-fab-shadow, var(--calcite-fab-shadow-internal));--calcite-button-background-color: var(--calcite-fab-background-color);--calcite-button-border-color: var(--calcite-fab-border-color);--calcite-button-text-color: var(--calcite-fab-text-color);--calcite-button-loader-color: var(--calcite-fab-loader-color)}.background-override{--calcite-shell-panel-background-color: var(--calcite-color-surface-4);--calcite-panel-background-color: var(--calcite-color-surface-4)}.chips-container{display:flex;flex-wrap:wrap;align-items:center;gap:var(--calcite-spacing-xs);flex:1;padding-block:var(--calcite-spacing-xs)}.header-content-wrapper{min-height:50px;flex-wrap:wrap;gap:var(--calcite-spacing-xs);padding-inline-start:var(--calcite-spacing-lg);padding-top:8px}.position-relative{position:relative;height:100%;overflow:hidden}.height-100{height:100%}.border-none{border:none}.height-50{height:50px}.chip-group{max-width:100%}.chip-group{display:flex;flex-wrap:wrap}.responsive-chip{max-width:100%}.responsive-chip .chip-text{display:inline-flex;align-items:center;line-height:1;max-width:100%}@container (max-width: 800px){.responsive-chip{max-width:100%;min-width:0}.responsive-chip .chip-text{max-width:100%;overflow:hidden;white-space:nowrap;text-overflow:ellipsis}}.mobile-stack{flex-direction:column;align-items:flex-start;padding-top:var(--calcite-spacing-md)}`;
class P extends C {
  constructor() {
    super(), this.solutionsCount = 0, this._isLoading = !0, this._filterOpen = !0, this._filters = {
      categories: [],
      searchTerm: ""
    }, this._isMobile = !1, this._translations = w({ name: "solutions-all-shell", blocking: !0 }), this._allSolutionsPanelRef = d(), this._solutionsFilterPanelRef = d(), this._mainPanelRef = d(), this._panelFixed = !1, this._configContextConsumer = f({
      context: R,
      subscribe: !0
    }), this._isMobileContextConsumer = f({
      context: T,
      subscribe: !0,
      callback: (e) => this._isMobile = e
    }), this.filterRemoved = F(), this.listenOn(window, "searchChanged", (e) => {
      e.detail && e.detail.scope === "all-solutions" && this._filterApplied(e.detail.option, e.detail.logSearchTelemetry);
    }), this.listenOn(window, "toggleFilter", this._toggleFilter), this.listenOn(window, "cardsFetched", () => {
      this._isLoading = !1, this._syncSolutionChipOrderWithDisplayedCards();
    }), this.listenOn(window, "solutionMatchesFromAssistant", (e) => {
      this._applyAssistantSolutionMatches(e.detail);
    });
  }
  static {
    this.properties = { solutionsCount: 9, _isLoading: 16, _filterOpen: 16, _filters: 16, _isMobile: 16, _translations: 16 };
  }
  static {
    this.styles = M;
  }
  willUpdate(e) {
    e.has("_isMobile") && (this._filterOpen = !this._isMobile);
  }
  get _telemetry() {
    return this._configContextConsumer.value?.telemetrySession;
  }
  async searchSolutions(e) {
    if (this._allSolutionsPanelRef.value.setSearchOptions(e), "industry" in e || "domain" in e) {
      e.displayValue = e.domain || e.industry;
      const t = e.industry ? Object.entries(_).find(([, s]) => s === e.industry)?.[0] : void 0, i = e.domain ? Object.entries(g).find(([, s]) => s === e.domain)?.[0] : void 0;
      e.industry = t || "", e.domain = i || "", e.value = i || t || "", this._solutionsFilterPanelRef.value.changeIndustryFilter(e.industry, e.domain, e.displayValue, !0);
    } else ("searchTerm" in e || "sortType" in e) && ("searchTerm" in e && (this._filters.searchTerm = e.searchTerm), this._allSolutionsPanelRef.value?.searchSolutions(e));
  }
  async load() {
    this._isMobileContextConsumer.value && (this._isMobile = this._isMobileContextConsumer.value);
  }
  updated() {
    this._fixPanelContentWrapperFocus();
  }
  _fixPanelContentWrapperFocus() {
    if (!this._panelFixed && this._mainPanelRef.value) {
      const e = this._mainPanelRef.value;
      e.componentOnReady?.().then(() => {
        if (e.shadowRoot) {
          const t = e.shadowRoot.querySelector(".content-wrapper");
          t && (t.tabIndex = -1, this._panelFixed = !0, this._setupPanelObserver());
        }
      }).catch(() => {
        this._panelFixed = !0;
      });
    }
  }
  _setupPanelObserver() {
    if (this._panelObserver)
      return;
    this._panelObserver = new MutationObserver((t) => {
      t.forEach((i) => {
        if (i.type === "attributes" && i.attributeName === "tabindex") {
          const s = i.target;
          s.classList.contains("content-wrapper") && s.tabIndex === 0 && (s.tabIndex = -1);
        }
      });
    });
    const e = this._mainPanelRef.value;
    if (e?.shadowRoot) {
      const t = e.shadowRoot.querySelector(".content-wrapper");
      t && this._panelObserver.observe(t, { attributes: !0, attributeFilter: ["tabindex"] });
    }
  }
  disconnectedCallback() {
    super.disconnectedCallback(), this._panelObserver?.disconnect();
  }
  render() {
    const e = this._isMobile ? "justify-content-end" : "", t = this._isLoading ? "display-none" : "";
    return a`<calcite-shell class=${`position-relative ${t}`}>${this._isMobile ? "" : this._getFilterPanel()}<h1 class="height-100"><calcite-panel class="border-override header-override panel-override z-index-1" ${n(this._mainPanelRef)}><div class=${c(this._isMobile ? `mobile-stack ${e}` : `display-flex align-items-center header-content-wrapper ${e}`)} slot=header-content><calcite-label class="label-override">${this._translations.numberOfSolutions.replace("{{count}}", this.solutionsCount.toString())}</calcite-label><div class="chips-container">${this._renderChips()}${this._filters.categories && this._filters.categories?.length > 0 && a`<calcite-button appearance=transparent icon-start=reset kind=neutral @click=${() => {
      this._filters = {
        ...this._filters,
        categories: [],
        searchTerm: ""
      }, this._emitSolutionMatchesFromAssistant([], []), this._allSolutionsPanelRef.value?.searchSolutions(this._filters.categories), this.filterRemoved.emit([]);
    }} .label=${this._translations.clearIndustryFilter}>${this._translations.clearIndustryFilter}</calcite-button>` || ""}</div></div>${this._isMobile && this._getFilterPanel() || ""}${this._getSolutionsAllPanel()}</calcite-panel></h1></calcite-shell>`;
  }
  _emitSolutionMatchesFromAssistant(e, t) {
    window.dispatchEvent(new CustomEvent("solutionMatchesFromAssistant", {
      detail: {
        solutionIds: e,
        solutionTitles: t
      },
      bubbles: !0,
      composed: !0
    }));
  }
  _getFilterPanel() {
    const e = this._filterOpen ? { display: "flex" } : "";
    return this._isMobile ? a`<calcite-panel class=${`border-none ${this._filterOpen ? "background-override" : "filter background-override"}`} id=solutions-all-shell-panel-start style=${h(e)}><solutions-filter-panel class="height-100" @applyFilter=${(t) => {
      this._filterApplied(t.detail);
    }} ${n(this._solutionsFilterPanelRef)}></solutions-filter-panel></calcite-panel>` : a`<calcite-shell-panel class="filter background-override filter-width-350" id=solutions-all-shell-panel-start slot=panel-start style=${h(e)}><calcite-panel class="border-none"><solutions-filter-panel class="height-100" @applyFilter=${(t) => {
      this._filterApplied(t.detail);
    }} ${n(this._solutionsFilterPanelRef)}></solutions-filter-panel></calcite-panel></calcite-shell-panel>`;
  }
  _getSolutionsAllPanel() {
    const e = this._isMobile && this._filterOpen ? { display: "none" } : "";
    return a`<solutions-all-panel @cardsFetched=${this._updateSolutionCount} style=${h(e)} ${n(this._allSolutionsPanelRef)}></solutions-all-panel>`;
  }
  _renderChips() {
    if (!this._filters.categories)
      return;
    const e = this._filters.categories.filter((t) => t.type === "industry" || t.type === "domain" || t.type === "solution");
    if (e.length !== 0)
      return a`<calcite-chip-group class="chip-group" .label=${this._translations.appliedFilters}>${this._filters.displayValue && this._filters.displayValue !== "" ? x(`${this._filters.displayValue}`, a`<calcite-chip appearance=outline-fill class=${c(this._isMobile ? "responsive-chip" : "padding-start responsive-chip")} closable .label=${this._filters.displayValue ?? ""} @calciteChipClose=${() => this._chipClosed(this._filters.displayValue, "solution")} .value=${this._filters.displayValue}>${this._filters.displayValue}</calcite-chip>`) : S(e, (t) => `${t.type}:${t.value}`, (t) => a`<calcite-chip appearance=outline-fill class=${c(this._isMobile ? "responsive-chip" : "padding-start responsive-chip")} closable .label=${t.displayValue} @calciteChipClose=${() => this._chipClosed(t.displayValue, t.type)} .value=${t.displayValue}>${t.displayValue}</calcite-chip>`)}</calcite-chip-group>`;
  }
  _updateSolutionCount(e) {
    this.solutionsCount = e.detail;
  }
  _syncSolutionChipOrderWithDisplayedCards() {
    if (!this._filters.categories?.length)
      return;
    const e = this._filters.categories.filter((l) => l.type === "solution");
    if (e.length <= 1)
      return;
    const t = this._allSolutionsPanelRef.value?.getDisplayedSolutionIds?.() ?? [];
    if (!t.length)
      return;
    const i = /* @__PURE__ */ new Map();
    t.forEach((l, o) => {
      i.set(l, o);
    });
    const s = [...e].sort((l, o) => {
      const u = i.get(String(l.value)), p = i.get(String(o.value)), b = u === void 0 ? Number.MAX_SAFE_INTEGER : u, y = p === void 0 ? Number.MAX_SAFE_INTEGER : p;
      return b - y;
    }), r = this._filters.categories.filter((l) => l.type !== "solution");
    this._filters = {
      ...this._filters,
      categories: [...r, ...s]
    };
  }
  _chipClosed(e, t) {
    if (this._filters.categories) {
      if (t === "solution") {
        this._filters.categories = [], this._emitSolutionMatchesFromAssistant([], []), this._filters = {
          ...this._filters,
          searchTerm: ""
        }, this._allSolutionsPanelRef.value?.searchSolutions();
        return;
      }
      if (t === "industry")
        this._filters.categories = [];
      else {
        const i = this._filters.categories.filter((s) => s.displayValue !== e && s.type !== t);
        this._filters.categories = i;
      }
      this._filters.categories.length > 0 ? this._allSolutionsPanelRef.value?.searchSolutions(this._filters.categories) : this._filters.searchTerm !== "" ? (this._allSolutionsPanelRef.value?.searchSolutions(this._filters.categories), this._allSolutionsPanelRef.value?.searchSolutions({ searchTerm: this._filters.searchTerm })) : this._allSolutionsPanelRef.value?.searchSolutions(), this.filterRemoved.emit(this._filters.categories);
    }
  }
  _applyAssistantSolutionMatches(e) {
    const t = Array.isArray(e?.solutionIds) ? e.solutionIds : [], i = Array.isArray(e?.solutionTitles) ? e.solutionTitles : [], s = t.map((r, l) => ({
      type: "solution",
      value: r,
      displayValue: i[l] ?? r
    }));
    this._filters = {
      categories: s,
      searchTerm: "",
      displayValue: this._translations.aiAssistantResults
    }, this.filterRemoved.emit([]);
  }
  _toggleFilter() {
    this._filterOpen = !this._filterOpen;
  }
  _logFilterTelemetry(e) {
    if ((e.type === "industry" || e.type === "domain") && e.industry) {
      const i = [_[e.industry]];
      if (e.domain) {
        const s = g[e.domain];
        i.push(s);
      }
      this._telemetry?.logEvent({
        category: m.ALL_SOLUTIONS,
        action: "Filter",
        dimensions: i
      });
    }
  }
  _filterApplied(e, t) {
    if (this._filters.categories)
      if (e) {
        let i = e;
        if ("displayValue" in e)
          this._filters.categories.push(e), this._logFilterTelemetry(e);
        else if (Array.isArray(e) && e.length > 0) {
          e.length >= 2 && (i = [e[e.length - 1]]), this._filters = {
            categories: e,
            searchTerm: this._filters.searchTerm
          };
          const s = e[e.length - 1];
          s && this._logFilterTelemetry(s);
        }
        if ("searchTerm" in i && (this._filters.searchTerm = i.searchTerm, t && i.searchTerm)) {
          this._telemetry?.logEvent({
            category: m.ALL_SOLUTIONS,
            action: "Search",
            dimensions: [i.searchTerm]
          });
          return;
        }
        this._allSolutionsPanelRef.value?.searchSolutions(i);
      } else
        this._filters.categories = [], this._allSolutionsPanelRef.value?.searchSolutions();
  }
}
v("solutions-all-shell", P);
export {
  P as SolutionsAllShell
};
