/* COPYRIGHT Esri - https://js.arcgis.com/5.1/LICENSE.txt */
import { c as N } from "../../chunks/runtime.js";
import { css as y, html as d } from "lit";
import { repeat as g } from "lit/directives/repeat.js";
import { u as b } from "../../chunks/useT9n.js";
import { LitElement as S, createEvent as p, safeClassMap as f } from "@arcgis/lumina";
import { c as C, i as I } from "../../chunks/deployAppContexts.js";
import { useContextConsumer as _ } from "@arcgis/lumina/context";
import { l as E } from "../../chunks/helpers.js";
import { e as F, d as A } from "../../chunks/interfaces.js";
const R = y`.width-full{width:100%}calcite-panel{--calcite-panel-background-color: var(--calcite-color-surface-2);--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}.display-none{display:none}`, v = {
  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"]
}, M = Object.keys(F).reduce(
  (u, t) => {
    const e = t;
    return u[e] = v[e] ?? [], u;
  },
  {}
);
class T extends S {
  constructor() {
    super(), this.hideIndustryFilter = !1, this.showReset = !1, this._activeFilterSettings = {
      categories: []
    }, this._isMobile = !1, this._translations = b({ name: "solutions-filter-panel", blocking: !0 }), this._userFirstName = "", this._configContextConsumer = _({
      context: C,
      subscribe: !0,
      callback: (t) => {
        t?.userSession && !this._userFirstName && this._loadUserFirstName();
      }
    }), this._isMobileContextConsumer = _({
      context: I,
      subscribe: !0,
      callback: (t) => this._isMobile = t
    }), this.applyFilter = p(), this.toggleFilter = p(), this.updateUrlParams = p(), 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((c) => c.toUpperCase() === t.replaceAll(" ", "").toUpperCase());
      return e && e in this._translations.industries ? this._translations.industries[e] : t;
    }, this._domainMatchName = (t) => {
      const e = Object.keys(this._translations.industriesDomain).find((c) => c.toUpperCase() === t.replaceAll(" ", "").toUpperCase());
      return e && e in this._translations.industriesDomain ? this._translations.industriesDomain[e] : t;
    }, this.listenOn(window, "filterRemoved", this._handleFilterRemoved), this.listenOn(window, "tabChanged", (t) => {
      t.detail === "all-solutions" ? this.updateUrlParams.emit(this._activeFilterSettings) : this.updateUrlParams.emit(null);
    });
  }
  static {
    this.properties = { hideIndustryFilter: 5, showReset: 5, _activeFilterSettings: 16, _isMobile: 16, _translations: 16, _userFirstName: 16 };
  }
  static {
    this.styles = R;
  }
  changeIndustryFilter(t, e, c, h) {
    if (window.dispatchEvent(new CustomEvent("clearAssistantResultFilters")), t !== "" || e !== "") {
      let r = [];
      if (e !== "") {
        const i = F[t], a = this._industryMatchName(i), s = A[e], n = this._domainMatchName(s);
        r = [{
          displayValue: a,
          type: "industry",
          value: t,
          industry: t,
          domain: ""
        }, {
          displayValue: n,
          type: "domain",
          value: e,
          industry: t,
          domain: e
        }];
      } else
        r = [{
          displayValue: c,
          type: e !== "" ? "domain" : "industry",
          value: e !== "" ? e : t,
          industry: t,
          domain: e
        }];
      this._activeFilterSettings = {
        ...this._activeFilterSettings,
        categories: [...r]
      }, h && this.applyFilter.emit(r);
    } else {
      const r = {
        displayValue: c,
        type: "industry",
        value: t,
        industry: t,
        domain: e
      };
      this._activeFilterSettings = {
        ...this._activeFilterSettings,
        categories: [r]
      }, h && this.applyFilter.emit(r);
    }
  }
  clearFilters() {
    this.changeIndustryFilter("", "", ""), this.applyFilter.emit(null);
  }
  async load() {
    await this._loadUserFirstName(), this._isMobileContextConsumer.value && (this._isMobile = this._isMobileContextConsumer.value);
  }
  render() {
    return d`<calcite-panel description heading>${this.showReset && d`<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-panel>`;
  }
  renderIndustryBlock() {
    const t = (i, a) => !this._activeFilterSettings || !this._activeFilterSettings.categories ? !1 : this._activeFilterSettings.categories.some((s) => s.industry === i && (s.domain ?? "") === a), e = (i) => !this._activeFilterSettings || !this._activeFilterSettings.categories ? !1 : this._activeFilterSettings.categories.some((a) => a.industry === i && (a.domain ?? "") === ""), c = (i, a) => a.length > 0 && (e(i) || this._activeFilterSettings.categories?.some((s) => s.industry === i && !!s.domain && a.includes(s.domain)) === !0), h = (i, a) => g(a, (s) => s, (s) => {
      const n = Object.keys(this._translations.industriesDomain).find((l) => l.toUpperCase() === s.toUpperCase()), o = n && n in this._translations.industriesDomain ? this._translations.industriesDomain[n] : s, m = t(i, s);
      return d`<calcite-tree-item .label=${o} @click=${(l) => {
        l.detail !== 0 && this.changeIndustryFilter(i, s, o, !0);
      }} @keydown=${(l) => {
        E(l, () => {
          this.changeIndustryFilter(i, s, o, !0);
        });
      }} .selected=${m || void 0}>${o}</calcite-tree-item>`;
    }), r = g(Object.entries(M), ([i, a]) => i, ([i, a]) => {
      const s = this._industryMatchName(i), n = a, o = e(i), m = c(i, n);
      return d`<calcite-tree-item .label=${s} @click=${(l) => {
        l.detail !== 0 && this.changeIndustryFilter(i, "", s, !0);
      }} @keydown=${(l) => {
        E(l, () => {
          this.changeIndustryFilter(i, "", s, !0);
        });
      }} .selected=${o || void 0} .expanded=${m ? !0 : void 0}>${s}${n.length > 0 && d`<calcite-tree slot=children selection-mode=multiple>${h(i, n)}</calcite-tree>` || ""}</calcite-tree-item>`;
    });
    return d`<calcite-block description expanded class="border-color" .heading=${this._translations.industriesHeader} .label=${this._translations.industriesHeader}><calcite-action class=${f(this._isMobile ? "" : "display-none")} icon=x @click=${this._toggleFilter} slot=content-end .text=${this._translations.close}></calcite-action><calcite-tree selection-mode=single-persist class="tree-padding">${r}</calcite-tree></calcite-block>`;
  }
  _toggleFilter() {
    this.toggleFilter.emit();
  }
  async _loadUserFirstName() {
    try {
      const t = await this._configContextConsumer.value?.userSession?.getUser();
      if (t?.fullName) {
        const e = t.fullName.split(" ")[0];
        this._userFirstName = e;
      }
    } catch {
    }
  }
}
N("solutions-filter-panel", T);
export {
  T as SolutionsFilterPanel
};
