/* COPYRIGHT Esri - https://js.arcgis.com/5.1/LICENSE.txt */
import { c as d } from "../../chunks/runtime.js";
import { css as u, html as n } from "lit";
import { u as g } from "../../chunks/useT9n.js";
import { LitElement as S, createEvent as a, nothing as c } from "@arcgis/lumina";
import { useContextConsumer as h } from "@arcgis/lumina/context";
import { c as m, i as y } from "../../chunks/deployAppContexts.js";
import { l } from "../../chunks/helpers.js";
import { c as p } from "../../chunks/interfaces.js";
const _ = u`.toolbar-sizing{display:flex;align-items:center;padding-right:0;padding-left:0;margin-left:auto;margin-right:auto;box-sizing:border-box;height:65px}.flex-row{display:flex;flex-direction:row}.flex-grow-1{flex-grow:1}calcite-input-text{--calcite-color-focus: transparent;--calcite-input-text-border-color: transparent;--calcite-color-border-input: transparent;--calcite-color-brand: transparent;display:grid;block-size:65px;box-sizing:border-box;border-bottom:2px solid transparent}@media(prefers-color-scheme:dark){calcite-input-text:focus{border-bottom:2px solid #009AF2}}@media(prefers-color-scheme:light){calcite-input-text:focus{border-bottom:2px solid #007AC2}}.padding-end-1-2{padding-inline-end:.5rem}`;
class f extends S {
  constructor() {
    super(), this.defaultSortType = "relevance", this.isOrgSolutions = !1, this._activeSearchSortSettings = [
      {
        scope: "all-solutions",
        settings: {
          enabled: !0,
          isOrgSolutions: !1,
          searchTerm: "",
          sortType: "relevance"
        }
      },
      {
        scope: "deployments",
        settings: {
          enabled: !0,
          isOrgSolutions: !1,
          searchTerm: "",
          sortType: "created"
        }
      }
    ], this._isMobile = !1, this._canViewOrg = !1, this._translations = g({ name: "solutions-searchsort-toolbar", blocking: !0 }), this._configContextConsumer = h({
      context: m,
      subscribe: !0,
      callback: async (t) => {
        t && t.authentication && (this._canViewOrg = t.authentication.CanViewOrg);
      }
    }), this._isMobileContextConsumer = h({
      context: y,
      subscribe: !0,
      callback: (t) => this._isMobile = t
    }), this.searchChanged = a(), this.sortTypeChanged = a(), this.toggleFilter = a(), this.updateUrlParams = a(), this.listenOn(window, "detailsCardSelected", () => {
      this.scope = void 0;
    }), this.listenOn(window, "deployedCardSelected", () => {
      this.scope = void 0;
    }), this.listenOn(window, "searchTermSet", (t) => {
      const e = t.detail;
      this.scope = e.scope, this.changeSearchFilter(e.searchTerm, !1);
    }), this.listenOn(window, "sortTypeSet", (t) => {
      const e = t.detail;
      this.scope = e.scope, this.sortChanged(e.sortType, !1);
    }), this.listenOn(window, "tabChanged", (t) => {
      this.scope = t.detail;
      const e = this._getCurrentScopeSetting();
      (this.scope === "all-solutions" || this.scope === "deployments") && (this.changeSearchFilter(e?.searchTerm || "", !1), this.sortChanged(e?.sortType || this.defaultSortType, !1), this._updateUrlParams(t));
    });
  }
  static {
    this.properties = { defaultSortType: [0, { type: String, attribute: "default-sort-type" }], isOrgSolutions: 5, scope: 1, _activeSearchSortSettings: 16, _isMobile: 16, _canViewOrg: 16, _translations: 16 };
  }
  static {
    this.styles = _;
  }
  changeOwnerFilter(t) {
    this.isOrgSolutions = !t, this._activeSearchSortSettings = this._activeSearchSortSettings.map((s) => s.scope === this.scope ? {
      ...s,
      settings: {
        ...s.settings,
        isOrgSolutions: this.isOrgSolutions
      }
    } : s);
    const e = {
      displayValue: this.isOrgSolutions ? p.ORG : p.MINE,
      type: "owner",
      value: this.isOrgSolutions ? "My organization's solutions" : "My solutions"
    };
    this.searchChanged.emit({ option: e, scope: this.scope });
  }
  changeSearchFilter(t, e, s) {
    const i = this._activeSearchSortSettings.map((o) => o.scope === this.scope ? {
      ...o,
      settings: {
        ...o.settings,
        searchTerm: t
      }
    } : o);
    let r = JSON.stringify(this._activeSearchSortSettings) !== JSON.stringify(i);
    this._activeSearchSortSettings = i, e && r && this.searchChanged.emit({ option: { searchTerm: t }, scope: this.scope }), s && (this._searchTelemetryTimer && window.clearTimeout(this._searchTelemetryTimer), this._searchTelemetryTimer = window.setTimeout(() => {
      this.searchChanged.emit({ option: { searchTerm: t }, scope: this.scope, logSearchTelemetry: !0 });
    }, 1e3));
  }
  sortChanged(t, e) {
    this._activeSearchSortSettings = this._activeSearchSortSettings.map((s) => s.scope === this.scope ? {
      ...s,
      settings: {
        ...s.settings,
        sortType: t
      }
    } : s), e && this.sortTypeChanged.emit({ sortType: t, scope: this.scope });
  }
  async load() {
    this._isMobileContextConsumer.value && (this._isMobile = this._isMobileContextConsumer.value);
  }
  render() {
    const t = this.scope === "all-solutions" || this.scope === "deployments" && this._configContextConsumer.value?.authentication?.IsSignedIn, e = t ? this._getCurrentScopeSetting() : void 0, s = this.scope === "deployments" ? this._translations.search.deployments : this._translations.search.solutions, i = this._isMobile ? "padding-end-1-2" : "";
    return t ? n`<div class="flex-row toolbar-sizing"><calcite-input-text class="flex-grow-1" clearable icon=search .label=${s} @calciteInputTextChange=${(r) => {
      const o = r.target;
      this.changeSearchFilter(o.value, !0, !1);
    }} @keyup=${(r) => {
      const o = r.target;
      ["Tab", "Shift", "Alt", "Control", "Meta", "ArrowUp", "ArrowDown", "ArrowLeft", "ArrowRight", "Escape"].includes(r.key) || (r.key === "Enter" ? this.changeSearchFilter(o.value, !0, !1) : (o.value.length >= 3 || o.value.length === 0) && this.changeSearchFilter(o.value, o.value.length >= 3 || o.value.length === 0, !0));
    }} .placeholder=${s} scale=l slot=content-top .value=${e?.searchTerm}></calcite-input-text>${this._isMobile && this.scope === "all-solutions" ? n`<calcite-button appearance=transparent class=${`focus-color ${i}`} icon-start=filter kind=neutral .label=${this._translations.showFilters} @click=${this._toggleFilter}></calcite-button>` : ""}${this.scope === "deployments" && this._canViewOrg && this._renderOwnerBlock() || ""}${this._renderDropdown()}</div>` : "";
  }
  _renderOwnerBlock() {
    return n`<calcite-dropdown width=m overlay-positioning=fixed><calcite-button appearance=transparent class="header-button-padding" .iconStart=${this.isOrgSolutions ? "organization" : "user"} kind=neutral .label=${this.isOrgSolutions ? this._translations.myOrgSolutions : this._translations.mySolutions} slot=trigger>${this._isMobile ? "" : this.isOrgSolutions ? this._translations.myOrgSolutions : this._translations.mySolutions}</calcite-button><calcite-dropdown-group group-title=${this._translations.filterBy ?? c}><calcite-dropdown-item icon-start=user @click=${(t) => {
      t.detail !== 0 && this.changeOwnerFilter(!0);
    }} @keydown=${(t) => l(t, () => this.changeOwnerFilter(!0))} .selected=${!this.isOrgSolutions}>${this._translations.mySolutions}</calcite-dropdown-item><calcite-dropdown-item icon-start=organization @click=${(t) => {
      t.detail !== 0 && this.changeOwnerFilter(!1);
    }} @keydown=${(t) => l(t, () => this.changeOwnerFilter(!1))} .selected=${this.isOrgSolutions}>${this._translations.myOrgSolutions}</calcite-dropdown-item></calcite-dropdown-group></calcite-dropdown>`;
  }
  _renderDropdown() {
    let t = [];
    t = [{
      displayValue: this._translations.sortByRelevance,
      sortType: "relevance"
    }, {
      displayValue: this._translations.sortByTitle,
      sortType: "title"
    }], this.scope === "deployments" ? t.push({
      displayValue: this._translations.sortByDateDeployed,
      sortType: "created"
    }) : (t.push({
      displayValue: this._translations.sortByNew,
      sortType: "new"
    }), t.push({
      displayValue: this._translations.sortByUpdated,
      sortType: "updated"
    }), this._configContextConsumer.value?.authentication?.IsSignedIn && t.push({
      displayValue: this._translations.sortBySuggestedForYou,
      sortType: "suggested"
    }));
    const e = this._getCurrentScopeSetting();
    return n`<calcite-dropdown placement=bottom-end width=m><calcite-button appearance=transparent class="header-button-padding" icon-start=sort-descending kind=neutral .label=${e?.sortType ? this._getString(e.sortType) : this._translations.chooseSortType} slot=trigger>${this._isMobile ? "" : e?.sortType ? this._getString(e.sortType) : ""}</calcite-button><calcite-dropdown-group group-title=${this._translations.chooseSortType ?? c}>${t.map((s) => this._getDropdownItem(s.displayValue, s.sortType))}</calcite-dropdown-group></calcite-dropdown>`;
  }
  _getDropdownItem(t, e) {
    const s = this._getCurrentScopeSetting();
    return s?.sortType && s.sortType !== e ? n`<calcite-dropdown-item @click=${(i) => {
      i.detail !== 0 && this.sortChanged(e, !0);
    }} @keydown=${(i) => l(i, () => this.sortChanged(e, !0))}>${t}</calcite-dropdown-item>` : n`<calcite-dropdown-item @click=${(i) => {
      i.detail !== 0 && this.sortChanged(e, !0);
    }} @keydown=${(i) => l(i, () => this.sortChanged(e, !0))} selected>${t}</calcite-dropdown-item>`;
  }
  _getString(t) {
    let e = "";
    switch (t) {
      case "new":
        e = this._translations.sortByNew;
        break;
      case "relevance":
        e = this._translations.sortByRelevance;
        break;
      case "suggested":
        e = this._translations.sortBySuggestedForYou;
        break;
      case "title":
        e = this._translations.sortByTitle;
        break;
      case "updated":
        e = this._translations.sortByUpdated;
        break;
      case "created":
        e = this._translations.sortByDateDeployed;
        break;
    }
    return e;
  }
  _getCurrentScopeSetting() {
    return this._activeSearchSortSettings?.filter((e) => e.scope === this.scope)?.[0]?.settings;
  }
  _toggleFilter() {
    this.toggleFilter.emit();
  }
  _updateUrlParams(t) {
    const e = t.detail;
    let s;
    this._activeSearchSortSettings.some((i) => i.scope === e ? (s = i, !0) : !1), s && this.updateUrlParams.emit(s);
  }
}
d("solutions-searchsort-toolbar", f);
export {
  f as SolutionsSearchSortToolbar
};
