var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
  if (kind === "a" && !f)
    throw new TypeError("Private accessor was defined without a getter");
  if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver))
    throw new TypeError("Cannot read private member from an object whose class did not declare it");
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
  if (kind === "m")
    throw new TypeError("Private method is not writable");
  if (kind === "a" && !f)
    throw new TypeError("Private accessor was defined without a setter");
  if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver))
    throw new TypeError("Cannot write private member to an object whose class did not declare it");
  return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
};
var _KlevuMerchandising_instances, _KlevuMerchandising_viewportUtil, _KlevuMerchandising_layoutElement, _KlevuMerchandising_resultObject, _KlevuMerchandising_clickEvent, _KlevuMerchandising_initialFetch, _KlevuMerchandising_loadMore, _KlevuMerchandising_paginationChange, _KlevuMerchandising_sortChanged, _KlevuMerchandising_sizeChange, _KlevuMerchandising_applyFilters, _KlevuMerchandising_internalRenderProductSlot;
import { applyFilterWithManager, categoryMerchandising, FilterManager, KlevuFetch, listFilters, sendMerchandisingViewEvent, } from "@klevu/core";
import { h, Host } from "@stencil/core";
import { parts } from "../../utils/parts";
import { globalExportedParts } from "../../utils/utils";
import { KlevuInit } from "../klevu-init/klevu-init";
/**
 * Full merchandising app to power up your product grid pages
 */
export class KlevuMerchandising {
  constructor() {
    _KlevuMerchandising_instances.add(this);
    _KlevuMerchandising_viewportUtil.set(this, void 0);
    _KlevuMerchandising_layoutElement.set(this, void 0);
    _KlevuMerchandising_resultObject.set(this, void 0);
    _KlevuMerchandising_clickEvent.set(this, void 0);
    this.usePagination = undefined;
    this.limit = 24;
    this.category = undefined;
    this.categoryTitle = undefined;
    this.sort = undefined;
    this.filterCount = undefined;
    this.options = undefined;
    this.filterCustomOrder = undefined;
    this.sortOptions = undefined;
    this.currentViewPortSize = undefined;
    this.results = [
      undefined,
      undefined,
      undefined,
      undefined,
      undefined,
      undefined,
      undefined,
      undefined,
      undefined,
    ];
    this.manager = new FilterManager();
    this.renderProductSlot = undefined;
  }
  async connectedCallback() {
    await KlevuInit.ready();
    await __classPrivateFieldGet(this, _KlevuMerchandising_instances, "m", _KlevuMerchandising_initialFetch).call(this);
  }
  async watchPropHandler(newValue, oldValue) {
    if (newValue !== oldValue) {
      this.manager.clear();
      await __classPrivateFieldGet(this, _KlevuMerchandising_instances, "m", _KlevuMerchandising_initialFetch).call(this);
    }
  }
  productClickHandler(event) {
    var _a;
    if (!event.detail.product.id || !event.detail.product.itemGroupId) {
      return;
    }
    if (__classPrivateFieldGet(this, _KlevuMerchandising_clickEvent, "f")) {
      __classPrivateFieldGet(this, _KlevuMerchandising_clickEvent, "f").call(this, (_a = event.detail.product) === null || _a === void 0 ? void 0 : _a.id, this.categoryTitle, event.detail.product.itemGroupId);
    }
  }
  async componentDidLoad() {
    this.currentViewPortSize = await __classPrivateFieldGet(this, _KlevuMerchandising_viewportUtil, "f").getCurrentSize();
  }
  render() {
    var _a, _b, _c;
    const isMobile = ((_a = this.currentViewPortSize) === null || _a === void 0 ? void 0 : _a.name) === "xs" || ((_b = this.currentViewPortSize) === null || _b === void 0 ? void 0 : _b.name) === "sm";
    return (h(Host, null, h("klevu-util-viewport", { onSizeChanged: __classPrivateFieldGet(this, _KlevuMerchandising_instances, "m", _KlevuMerchandising_sizeChange).bind(this), ref: (el) => (__classPrivateFieldSet(this, _KlevuMerchandising_viewportUtil, el, "f")) }), h("klevu-layout-results", { exportparts: globalExportedParts, ref: (el) => (__classPrivateFieldSet(this, _KlevuMerchandising_layoutElement, el, "f")) }, h("klevu-facet-list", { slot: "sidebar", accordion: true, customOrder: this.filterCustomOrder, exportparts: globalExportedParts, manager: this.manager, useApplyButton: isMobile, onKlevuApplyFilters: __classPrivateFieldGet(this, _KlevuMerchandising_instances, "m", _KlevuMerchandising_applyFilters).bind(this) }), h("div", { slot: "header", class: "header" }, h("klevu-typography", { variant: "h1" }, this.categoryTitle), h("klevu-sort", { variant: "inline", exportparts: globalExportedParts, onKlevuSortChanged: __classPrivateFieldGet(this, _KlevuMerchandising_instances, "m", _KlevuMerchandising_sortChanged).bind(this), options: this.sortOptions })), h("klevu-product-grid", { slot: "content" }, this.results.map((p) => (h("klevu-product", { product: p, fixedWidth: true, exportparts: parts["klevu-product"] }, __classPrivateFieldGet(this, _KlevuMerchandising_instances, "m", _KlevuMerchandising_internalRenderProductSlot).call(this, p, "top"), __classPrivateFieldGet(this, _KlevuMerchandising_instances, "m", _KlevuMerchandising_internalRenderProductSlot).call(this, p, "image"), __classPrivateFieldGet(this, _KlevuMerchandising_instances, "m", _KlevuMerchandising_internalRenderProductSlot).call(this, p, "info"), __classPrivateFieldGet(this, _KlevuMerchandising_instances, "m", _KlevuMerchandising_internalRenderProductSlot).call(this, p, "bottom"))))), h("div", { slot: "footer", class: "footer" }, this.usePagination && __classPrivateFieldGet(this, _KlevuMerchandising_resultObject, "f") ? (h("klevu-pagination", { exportparts: globalExportedParts, queryResult: __classPrivateFieldGet(this, _KlevuMerchandising_resultObject, "f"), onKlevuPaginationChange: __classPrivateFieldGet(this, _KlevuMerchandising_instances, "m", _KlevuMerchandising_paginationChange).bind(this) })) : ((_c = __classPrivateFieldGet(this, _KlevuMerchandising_resultObject, "f")) === null || _c === void 0 ? void 0 : _c.next) ? (h("klevu-button", { onClick: __classPrivateFieldGet(this, _KlevuMerchandising_instances, "m", _KlevuMerchandising_loadMore).bind(this) }, "Load more")) : null))));
  }
  static get is() { return "klevu-merchandising"; }
  static get encapsulation() { return "shadow"; }
  static get originalStyleUrls() {
    return {
      "$": ["klevu-merchandising.css"]
    };
  }
  static get styleUrls() {
    return {
      "$": ["klevu-merchandising.css"]
    };
  }
  static get properties() {
    return {
      "usePagination": {
        "type": "boolean",
        "mutable": false,
        "complexType": {
          "original": "boolean",
          "resolved": "boolean | undefined",
          "references": {}
        },
        "required": false,
        "optional": true,
        "docs": {
          "tags": [],
          "text": "Should display pagination instead of load next"
        },
        "attribute": "use-pagination",
        "reflect": false
      },
      "limit": {
        "type": "number",
        "mutable": false,
        "complexType": {
          "original": "number",
          "resolved": "number",
          "references": {}
        },
        "required": false,
        "optional": false,
        "docs": {
          "tags": [],
          "text": "Count of products for page"
        },
        "attribute": "limit",
        "reflect": false,
        "defaultValue": "24"
      },
      "category": {
        "type": "string",
        "mutable": false,
        "complexType": {
          "original": "string",
          "resolved": "string",
          "references": {}
        },
        "required": true,
        "optional": false,
        "docs": {
          "tags": [],
          "text": "Which category products"
        },
        "attribute": "category",
        "reflect": false
      },
      "categoryTitle": {
        "type": "string",
        "mutable": false,
        "complexType": {
          "original": "string",
          "resolved": "string",
          "references": {}
        },
        "required": true,
        "optional": false,
        "docs": {
          "tags": [],
          "text": "Category title"
        },
        "attribute": "category-title",
        "reflect": false
      },
      "sort": {
        "type": "string",
        "mutable": false,
        "complexType": {
          "original": "KlevuSearchSorting",
          "resolved": "KlevuSearchSorting.AdvancedSorting | KlevuSearchSorting.NameAsc | KlevuSearchSorting.NameDesc | KlevuSearchSorting.NewArrivalAsc | KlevuSearchSorting.NewArrivalDesc | KlevuSearchSorting.PriceAsc | KlevuSearchSorting.PriceDesc | KlevuSearchSorting.RatingAsc | KlevuSearchSorting.RatingDesc | KlevuSearchSorting.Relevance | undefined",
          "references": {
            "KlevuSearchSorting": {
              "location": "import",
              "path": "@klevu/core"
            }
          }
        },
        "required": false,
        "optional": true,
        "docs": {
          "tags": [],
          "text": "Order of results"
        },
        "attribute": "sort",
        "reflect": false
      },
      "filterCount": {
        "type": "number",
        "mutable": false,
        "complexType": {
          "original": "number",
          "resolved": "number | undefined",
          "references": {}
        },
        "required": false,
        "optional": true,
        "docs": {
          "tags": [],
          "text": "How many filters per facet to show"
        },
        "attribute": "filter-count",
        "reflect": false
      },
      "options": {
        "type": "unknown",
        "mutable": false,
        "complexType": {
          "original": "KlevuMerchandisingOptions",
          "resolved": "undefined | { id: string; searchTerm: string; } & Omit<KlevuBaseQuerySettings, \"query\">",
          "references": {
            "KlevuMerchandisingOptions": {
              "location": "import",
              "path": "@klevu/core"
            }
          }
        },
        "required": false,
        "optional": true,
        "docs": {
          "tags": [],
          "text": "Object to override and settings on search options"
        }
      },
      "filterCustomOrder": {
        "type": "unknown",
        "mutable": false,
        "complexType": {
          "original": "{ [key: string]: string[] }",
          "resolved": "undefined | { [key: string]: string[]; }",
          "references": {}
        },
        "required": false,
        "optional": true,
        "docs": {
          "tags": [],
          "text": "Order filters in given order"
        }
      },
      "sortOptions": {
        "type": "unknown",
        "mutable": false,
        "complexType": {
          "original": "Array<{ value: KlevuSearchSorting; text: string }>",
          "resolved": "undefined | { value: KlevuSearchSorting; text: string; }[]",
          "references": {
            "Array": {
              "location": "global"
            },
            "KlevuSearchSorting": {
              "location": "import",
              "path": "@klevu/core"
            }
          }
        },
        "required": false,
        "optional": true,
        "docs": {
          "tags": [],
          "text": "Pass custom options for the sort dropdown"
        }
      },
      "renderProductSlot": {
        "type": "unknown",
        "mutable": false,
        "complexType": {
          "original": "(product: KlevuRecord, productSlot: KlevuProductSlots) => HTMLElement | string | null",
          "resolved": "((product: KlevuRecord, productSlot: KlevuProductSlots) => string | HTMLElement | null) | undefined",
          "references": {
            "KlevuRecord": {
              "location": "import",
              "path": "@klevu/core"
            },
            "KlevuProductSlots": {
              "location": "import",
              "path": "../klevu-product/klevu-product"
            },
            "HTMLElement": {
              "location": "global"
            }
          }
        },
        "required": false,
        "optional": true,
        "docs": {
          "tags": [],
          "text": "Rendering function created to put custom content to klevu-product slots. Provides a product being rendered.\nThis function is called for each slot (top, image, info and bottom) of the component. Second parameter provides\nslot requested. Return null for slots that you do not want to render."
        }
      }
    };
  }
  static get states() {
    return {
      "currentViewPortSize": {},
      "results": {},
      "manager": {}
    };
  }
  static get elementRef() { return "el"; }
  static get watchers() {
    return [{
        "propName": "category",
        "methodName": "watchPropHandler"
      }];
  }
  static get listeners() {
    return [{
        "name": "productClick",
        "method": "productClickHandler",
        "target": undefined,
        "capture": false,
        "passive": false
      }];
  }
}
_KlevuMerchandising_viewportUtil = new WeakMap(), _KlevuMerchandising_layoutElement = new WeakMap(), _KlevuMerchandising_resultObject = new WeakMap(), _KlevuMerchandising_clickEvent = new WeakMap(), _KlevuMerchandising_instances = new WeakSet(), _KlevuMerchandising_initialFetch = async function _KlevuMerchandising_initialFetch() {
  var _a, _b, _c, _d;
  const result = await KlevuFetch(categoryMerchandising(this.category, Object.assign({ limit: this.limit, sort: this.sort }, this.options), sendMerchandisingViewEvent(this.categoryTitle), listFilters({
    filterManager: this.manager,
    limit: this.filterCount,
    rangeFilterSettings: [
      {
        key: "klevu_price",
        minMax: true,
      },
    ],
  }), applyFilterWithManager(this.manager)));
  __classPrivateFieldSet(this, _KlevuMerchandising_resultObject, result.queriesById("categoryMerchandising"), "f");
  this.results = (_b = (_a = __classPrivateFieldGet(this, _KlevuMerchandising_resultObject, "f")) === null || _a === void 0 ? void 0 : _a.records) !== null && _b !== void 0 ? _b : [];
  __classPrivateFieldSet(this, _KlevuMerchandising_clickEvent, (_d = (_c = __classPrivateFieldGet(this, _KlevuMerchandising_resultObject, "f")) === null || _c === void 0 ? void 0 : _c.getCategoryMerchandisingClickSendEvent) === null || _d === void 0 ? void 0 : _d.call(_c), "f");
}, _KlevuMerchandising_loadMore = async function _KlevuMerchandising_loadMore() {
  var _a, _b, _c, _d, _e;
  if (!((_a = __classPrivateFieldGet(this, _KlevuMerchandising_resultObject, "f")) === null || _a === void 0 ? void 0 : _a.next)) {
    return;
  }
  const nextResultObject = await __classPrivateFieldGet(this, _KlevuMerchandising_resultObject, "f").next();
  __classPrivateFieldSet(this, _KlevuMerchandising_resultObject, nextResultObject.queriesById("categoryMerchandising"), "f");
  this.results = [...this.results, ...((_c = (_b = __classPrivateFieldGet(this, _KlevuMerchandising_resultObject, "f")) === null || _b === void 0 ? void 0 : _b.records) !== null && _c !== void 0 ? _c : [])];
  __classPrivateFieldSet(this, _KlevuMerchandising_clickEvent, (_e = (_d = __classPrivateFieldGet(this, _KlevuMerchandising_resultObject, "f")) === null || _d === void 0 ? void 0 : _d.getCategoryMerchandisingClickSendEvent) === null || _e === void 0 ? void 0 : _e.call(_d), "f");
}, _KlevuMerchandising_paginationChange = async function _KlevuMerchandising_paginationChange(event) {
  var _a, _b, _c, _d, _e;
  if (!((_a = __classPrivateFieldGet(this, _KlevuMerchandising_resultObject, "f")) === null || _a === void 0 ? void 0 : _a.getPage)) {
    return;
  }
  const nextResultObject = await __classPrivateFieldGet(this, _KlevuMerchandising_resultObject, "f").getPage({ pageIndex: event.detail - 1 });
  __classPrivateFieldSet(this, _KlevuMerchandising_resultObject, nextResultObject.queriesById("categoryMerchandising"), "f");
  this.results = (_c = (_b = __classPrivateFieldGet(this, _KlevuMerchandising_resultObject, "f")) === null || _b === void 0 ? void 0 : _b.records) !== null && _c !== void 0 ? _c : [];
  __classPrivateFieldSet(this, _KlevuMerchandising_clickEvent, (_e = (_d = __classPrivateFieldGet(this, _KlevuMerchandising_resultObject, "f")) === null || _d === void 0 ? void 0 : _d.getCategoryMerchandisingClickSendEvent) === null || _e === void 0 ? void 0 : _e.call(_d), "f");
}, _KlevuMerchandising_sortChanged = async function _KlevuMerchandising_sortChanged(event) {
  this.sort = event.detail;
  await __classPrivateFieldGet(this, _KlevuMerchandising_instances, "m", _KlevuMerchandising_initialFetch).call(this);
}, _KlevuMerchandising_sizeChange = function _KlevuMerchandising_sizeChange(event) {
  this.currentViewPortSize = event.detail;
}, _KlevuMerchandising_applyFilters = function _KlevuMerchandising_applyFilters() {
  __classPrivateFieldGet(this, _KlevuMerchandising_instances, "m", _KlevuMerchandising_initialFetch).call(this);
  __classPrivateFieldGet(this, _KlevuMerchandising_layoutElement, "f").closeDrawer();
}, _KlevuMerchandising_internalRenderProductSlot = function _KlevuMerchandising_internalRenderProductSlot(product, slot) {
  if (!this.renderProductSlot || !product) {
    return null;
  }
  const content = this.renderProductSlot(product, slot);
  if (content === null) {
    return null;
  }
  if (typeof content === "string") {
    return h("div", { slot: slot, innerHTML: content });
  }
  return (h("div", { slot: slot, ref: (el) => {
      if (!el) {
        return;
      }
      el.innerHTML = "";
      el.appendChild(content);
    } }));
};
//# sourceMappingURL=klevu-merchandising.js.map
