/* COPYRIGHT Esri - https://js.arcgis.com/1.2/LICENSE.txt */
import { c as E } from "../../chunks/runtime.js";
import { css as F, html as l } from "lit";
import { keyed as d } from "lit/directives/keyed.js";
import { u as N } from "../../chunks/useT9n.js";
import { LitElement as S, createEvent as _ } from "@arcgis/lumina";
import { c as I } from "../../chunks/deployAppContexts.js";
import { useContextConsumer as f } from "@arcgis/lumina/context";
import { d as p, T as A } from "../../chunks/interfaces.js";
const y = F`.width-full{width:100%}calcite-panel{--calcite-panel-background-color: var(--calcite-color-foreground-1);--calcite-panel-footer-space: var(--calcite-spacing-sm)}.border-color{--calcite-block-border-color: transparent}calcite-block{margin:0;--calcite-block-content-space: 0}.tree-padding{padding:0 .2rem}`, T = {
  PUBLICSAFETY: ["EMERGENCYMANAGEMENT", "FIRESERVICE", "LAWENFORCEMENT"],
  STATEANDLOCALGOVERNMENT: [
    "AGRICULTURE",
    "AIRPORT",
    "ASSESSMENTANDTAXATION",
    "ELECTIONS",
    "EMERGENCYMANAGEMENT",
    "FIRESERVICE",
    "FISHANDWILDLIFE",
    "HEALTHANDHUMANSERVICES",
    "LAWENFORCEMENT",
    "NATURALRESOURCES",
    "PARKS",
    "PLANNINGANDDEVELOPMENT",
    "PUBLICWORKS",
    "TRANSPORTATION"
  ],
  UTILITIES: ["DISTRICTENERGY", "ELECTRIC", "GAS", "SEWER", "STORMWATER", "WATER"]
}, b = Object.keys(p).reduce(
  (m, t) => {
    const e = t;
    return m[e] = T[e] ?? [], m;
  },
  {}
);
class O extends S {
  constructor() {
    super(), this.hideIndustryFilter = !1, this.isAIDialogOpen = !1, this.showReset = !1, this._activeFilterSettings = {
      categories: []
    }, this._translations = N({ name: "solutions-filter-panel", blocking: !0 }), this._userFirstName = "", this._configContextConsumer = f({
      context: I,
      subscribe: !0,
      callback: (t) => {
        t?.userSession && !this._userFirstName && this._loadUserFirstName();
      }
    }), this.applyFilter = _(), this._handleFilterRemoved = (t) => {
      const e = t.detail;
      Array.isArray(e) && (this._activeFilterSettings = {
        ...this._activeFilterSettings,
        categories: [...e]
      });
    }, this._industryMatchName = (t) => {
      const e = Object.keys(this._translations.industries).find((o) => o.toUpperCase() === t.replaceAll(" ", "").toUpperCase());
      return e && e in this._translations.industries ? this._translations.industries[e] : t;
    }, this.listenOn(window, "filterRemoved", this._handleFilterRemoved), this.listenOn(window, "closeAssistant", () => {
      this.isAIDialogOpen = !1;
    }), this.listenOn(window, "openAssistant", () => {
      this.isAIDialogOpen = !0;
    });
  }
  static {
    this.properties = { hideIndustryFilter: 5, isAIDialogOpen: 5, showReset: 5, _activeFilterSettings: 16, _translations: 16, _userFirstName: 16 };
  }
  static {
    this.styles = y;
  }
  get _telemetry() {
    return this._configContextConsumer.value?.telemetrySession;
  }
  changeIndustryFilter(t, e, o, h) {
    if (t !== "" || e !== "") {
      let n = [];
      if (e !== "") {
        const i = p[t];
        n = [{
          displayValue: this._industryMatchName(i),
          type: "industry",
          value: t,
          industry: t,
          domain: ""
        }, {
          displayValue: o,
          type: "domain",
          value: e,
          industry: t,
          domain: e
        }];
      } else
        n = [{
          displayValue: o,
          type: e !== "" ? "domain" : "industry",
          value: e !== "" ? e : t,
          industry: t,
          domain: e
        }];
      this._activeFilterSettings = {
        ...this._activeFilterSettings,
        categories: [...n]
      }, h && this.applyFilter.emit(n);
    } else {
      const n = {
        displayValue: o,
        type: "industry",
        value: t,
        industry: t,
        domain: e
      };
      this._activeFilterSettings = {
        ...this._activeFilterSettings,
        categories: [n]
      }, h && this.applyFilter.emit(n);
    }
  }
  clearFilters() {
    this.changeIndustryFilter("", "", ""), this.applyFilter.emit(null);
  }
  async load() {
    await this._loadUserFirstName();
  }
  render() {
    return l`<calcite-panel description heading>${this.showReset && l`<calcite-action appearance=solid icon=reset @click=${() => {
      this.clearFilters();
    }} slot=header-actions-end .text=${this._translations.resetFilters} text-enabled></calcite-action>` || ""}<calcite-block-group .label=${this._translations.filters} scale=l>${!this.hideIndustryFilter && this.renderIndustryBlock() || ""}</calcite-block-group><calcite-button id=solutions-assistant-button icon-start=speech-bubbles label="Solutions Assistant" @click=${() => {
      this._telemetry?.logEvent({
        category: A.ASSISTANT,
        action: "Open"
      }), this.isAIDialogOpen = !0;
    }} round scale=l slot=footer-start></calcite-button><calcite-tooltip reference-element=solutions-assistant-button placement=right slot=footer-start offset-distance=10 open><span>${this._translations.assistant.welcomeTip}</span></calcite-tooltip><arcgis-solutions-assistant .open=${this.isAIDialogOpen} .userFirstName=${this._userFirstName}></arcgis-solutions-assistant></calcite-panel>`;
  }
  renderIndustryBlock() {
    const t = (i, r) => !this._activeFilterSettings || !this._activeFilterSettings.categories ? !1 : this._activeFilterSettings.categories.some((s) => s.industry === i && (s.domain ?? "") === r), e = (i) => !this._activeFilterSettings || !this._activeFilterSettings.categories ? !1 : this._activeFilterSettings.categories.some((r) => r.industry === i && (r.domain ?? "") === ""), o = (i, r) => r.length > 0 && (e(i) || this._activeFilterSettings.categories?.some((s) => s.industry === i && !!s.domain && r.includes(s.domain)) === !0), h = (i, r) => r.map((s) => {
      const a = Object.keys(this._translations.industriesDomain).find((g) => g.toUpperCase() === s.toUpperCase()), c = a && a in this._translations.industriesDomain ? this._translations.industriesDomain[a] : s;
      return t(i, s) ? d(s, l`<calcite-tree-item .label=${c} @click=${() => this.changeIndustryFilter(i, s, c, !0)} selected>${c}</calcite-tree-item>`) : d(s, l`<calcite-tree-item .label=${c} @click=${() => this.changeIndustryFilter(i, s, c, !0)}>${c}</calcite-tree-item>`);
    }), n = Object.entries(b).map(([i, r]) => {
      const s = this._industryMatchName(i), a = r, c = e(i), u = o(i, a);
      return c ? d(i, l`<calcite-tree-item .label=${s} @click=${() => this.changeIndustryFilter(i, "", s, !0)} selected .expanded=${u ? !0 : void 0}>${s}${a.length > 0 && l`<calcite-tree slot=children selection-mode=multiple>${h(i, a)}</calcite-tree>` || ""}</calcite-tree-item>`) : d(i, l`<calcite-tree-item .label=${s} @click=${() => this.changeIndustryFilter(i, "", s, !0)} .expanded=${u ? !0 : void 0}>${s}${a.length > 0 && l`<calcite-tree slot=children selection-mode=multiple>${h(i, a)}</calcite-tree>` || ""}</calcite-tree-item>`);
    });
    return l`<calcite-block description expanded class="border-color" .heading=${this._translations.industriesHeader} .label=${this._translations.industriesHeader}><calcite-tree selection-mode=single-persist class="tree-padding">${n}</calcite-tree></calcite-block>`;
  }
  async _loadUserFirstName() {
    try {
      const t = await this._configContextConsumer.value?.userSession?.getUser();
      if (t?.fullName) {
        const e = t.fullName.split(" ")[0];
        this._userFirstName = e;
      }
    } catch {
    }
  }
}
E("solutions-filter-panel", O);
export {
  O as SolutionsFilterPanel
};
