import { proxyCustomElement, HTMLElement, h, Host } from '@stencil/core/internal/client';
import { p as parts } from './parts.js';
import { g as globalExportedParts } from './utils.js';
import { K as KlevuInit } from './klevu-init2.js';
import { d as defineCustomElement$g } from './klevu-accordion2.js';
import { d as defineCustomElement$f } from './klevu-button2.js';
import { d as defineCustomElement$e } from './klevu-checkbox2.js';
import { d as defineCustomElement$d } from './klevu-drawer2.js';
import { d as defineCustomElement$c } from './klevu-dropdown2.js';
import { d as defineCustomElement$b } from './klevu-facet2.js';
import { d as defineCustomElement$a } from './klevu-facet-list2.js';
import { d as defineCustomElement$9 } from './klevu-layout-results2.js';
import { d as defineCustomElement$8 } from './klevu-pagination2.js';
import { d as defineCustomElement$7 } from './klevu-product2.js';
import { d as defineCustomElement$6 } from './klevu-product-grid2.js';
import { d as defineCustomElement$5 } from './klevu-slider2.js';
import { d as defineCustomElement$4 } from './klevu-sort2.js';
import { d as defineCustomElement$3 } from './klevu-typography2.js';
import { d as defineCustomElement$2 } from './klevu-util-viewport2.js';
import { F as FilterManager } from './filterManager.js';
import { K as KlevuFetch, a as applyFilterWithManager } from './applyFilterWithManager.js';
import { s as search, a as sendSearchEvent } from './search.js';
import { l as listFilters } from './listFilters.js';

const klevuSearchLandingPageCss = ":host{display:block}.header{display:flex;justify-content:space-between;width:100%}.footer{display:flex;justify-content:center}@media (max-width: 511px){.header{display:block}}";

var __classPrivateFieldGet = (undefined && undefined.__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 = (undefined && undefined.__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 _KlevuSearchLandingPage_instances, _KlevuSearchLandingPage_resultObject, _KlevuSearchLandingPage_clickEvent, _KlevuSearchLandingPage_viewportUtil, _KlevuSearchLandingPage_layoutElement, _KlevuSearchLandingPage_initialFetch, _KlevuSearchLandingPage_internalRenderProductSlot, _KlevuSearchLandingPage_sizeChange, _KlevuSearchLandingPage_sortChanged, _KlevuSearchLandingPage_applyFilters;
const KlevuSearchLandingPage$1 = /*@__PURE__*/ proxyCustomElement(class KlevuSearchLandingPage extends HTMLElement {
  constructor() {
    super();
    this.__registerHost();
    this.__attachShadow();
    _KlevuSearchLandingPage_instances.add(this);
    _KlevuSearchLandingPage_resultObject.set(this, void 0);
    _KlevuSearchLandingPage_clickEvent.set(this, void 0);
    _KlevuSearchLandingPage_viewportUtil.set(this, void 0);
    _KlevuSearchLandingPage_layoutElement.set(this, void 0);
    this.limit = 24;
    this.term = undefined;
    this.sort = undefined;
    this.filterCount = undefined;
    this.filterCustomOrder = undefined;
    this.usePagination = undefined;
    this.results = [
      undefined,
      undefined,
      undefined,
      undefined,
      undefined,
      undefined,
      undefined,
      undefined,
      undefined,
    ];
    this.manager = new FilterManager();
    this.currentViewPortSize = undefined;
    this.renderProductSlot = undefined;
  }
  async connectedCallback() {
    await KlevuInit.ready();
    await __classPrivateFieldGet(this, _KlevuSearchLandingPage_instances, "m", _KlevuSearchLandingPage_initialFetch).call(this);
  }
  termChanged(oldValue, newValue) {
    __classPrivateFieldGet(this, _KlevuSearchLandingPage_instances, "m", _KlevuSearchLandingPage_initialFetch).call(this);
  }
  async loadMore() {
    var _a, _b, _c, _d, _e;
    if (!((_a = __classPrivateFieldGet(this, _KlevuSearchLandingPage_resultObject, "f")) === null || _a === void 0 ? void 0 : _a.next)) {
      return;
    }
    const nextResultObject = await __classPrivateFieldGet(this, _KlevuSearchLandingPage_resultObject, "f").next();
    __classPrivateFieldSet(this, _KlevuSearchLandingPage_resultObject, nextResultObject.queriesById("search"), "f");
    this.results = [...this.results, ...((_c = (_b = __classPrivateFieldGet(this, _KlevuSearchLandingPage_resultObject, "f")) === null || _b === void 0 ? void 0 : _b.records) !== null && _c !== void 0 ? _c : [])];
    __classPrivateFieldSet(this, _KlevuSearchLandingPage_clickEvent, (_e = (_d = __classPrivateFieldGet(this, _KlevuSearchLandingPage_resultObject, "f")).getSearchClickSendEvent) === null || _e === void 0 ? void 0 : _e.call(_d), "f");
  }
  async paginationChange(event) {
    var _a, _b, _c, _d, _e;
    if (!((_a = __classPrivateFieldGet(this, _KlevuSearchLandingPage_resultObject, "f")) === null || _a === void 0 ? void 0 : _a.getPage)) {
      return;
    }
    const nextResultObject = await __classPrivateFieldGet(this, _KlevuSearchLandingPage_resultObject, "f").getPage({ pageIndex: event.detail - 1 });
    __classPrivateFieldSet(this, _KlevuSearchLandingPage_resultObject, nextResultObject.queriesById("search"), "f");
    this.results = (_c = (_b = __classPrivateFieldGet(this, _KlevuSearchLandingPage_resultObject, "f")) === null || _b === void 0 ? void 0 : _b.records) !== null && _c !== void 0 ? _c : [];
    __classPrivateFieldSet(this, _KlevuSearchLandingPage_clickEvent, (_e = (_d = __classPrivateFieldGet(this, _KlevuSearchLandingPage_resultObject, "f")) === null || _d === void 0 ? void 0 : _d.getSearchClickSendEvent) === null || _e === void 0 ? void 0 : _e.call(_d), "f");
  }
  productClickHandler(event) {
    if (__classPrivateFieldGet(this, _KlevuSearchLandingPage_clickEvent, "f") && event.detail.product.id) {
      __classPrivateFieldGet(this, _KlevuSearchLandingPage_clickEvent, "f").call(this, event.detail.product.id, event.detail.product.itemGroupId || event.detail.product.id);
    }
  }
  filterSelectionUpdate() {
    __classPrivateFieldGet(this, _KlevuSearchLandingPage_instances, "m", _KlevuSearchLandingPage_initialFetch).call(this);
  }
  async componentDidLoad() {
    this.currentViewPortSize = await __classPrivateFieldGet(this, _KlevuSearchLandingPage_viewportUtil, "f").getCurrentSize();
  }
  render() {
    var _a, _b, _c, _d;
    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, _KlevuSearchLandingPage_instances, "m", _KlevuSearchLandingPage_sizeChange).bind(this), ref: (el) => (__classPrivateFieldSet(this, _KlevuSearchLandingPage_viewportUtil, el, "f")) }), h("klevu-layout-results", { exportparts: globalExportedParts, ref: (el) => (__classPrivateFieldSet(this, _KlevuSearchLandingPage_layoutElement, el, "f")) }, h("klevu-facet-list", { slot: "sidebar", customOrder: this.filterCustomOrder, exportparts: globalExportedParts, manager: this.manager, useApplyButton: isMobile, onKlevuApplyFilters: __classPrivateFieldGet(this, _KlevuSearchLandingPage_instances, "m", _KlevuSearchLandingPage_applyFilters).bind(this) }), h("div", { slot: "header", class: "header" }, h("klevu-typography", { slot: "header", variant: "h1" }, "Searching term \"", this.term, "\""), h("klevu-sort", { variant: "inline", exportparts: globalExportedParts, onKlevuSortChanged: __classPrivateFieldGet(this, _KlevuSearchLandingPage_instances, "m", _KlevuSearchLandingPage_sortChanged).bind(this) })), h("klevu-product-grid", { slot: "content" }, (_c = this.results) === null || _c === void 0 ? void 0 : _c.map((p) => (h("klevu-product", { product: p, fixedWidth: true, exportparts: parts["klevu-product"] }, __classPrivateFieldGet(this, _KlevuSearchLandingPage_instances, "m", _KlevuSearchLandingPage_internalRenderProductSlot).call(this, p, "top"), __classPrivateFieldGet(this, _KlevuSearchLandingPage_instances, "m", _KlevuSearchLandingPage_internalRenderProductSlot).call(this, p, "image"), __classPrivateFieldGet(this, _KlevuSearchLandingPage_instances, "m", _KlevuSearchLandingPage_internalRenderProductSlot).call(this, p, "info"), __classPrivateFieldGet(this, _KlevuSearchLandingPage_instances, "m", _KlevuSearchLandingPage_internalRenderProductSlot).call(this, p, "bottom"))))), h("div", { slot: "footer", class: "footer" }, this.usePagination && __classPrivateFieldGet(this, _KlevuSearchLandingPage_resultObject, "f") ? (h("klevu-pagination", { queryResult: __classPrivateFieldGet(this, _KlevuSearchLandingPage_resultObject, "f"), onKlevuPaginationChange: this.paginationChange.bind(this) })) : ((_d = __classPrivateFieldGet(this, _KlevuSearchLandingPage_resultObject, "f")) === null || _d === void 0 ? void 0 : _d.next) ? (h("klevu-button", { onClick: this.loadMore.bind(this) }, "Load more")) : null))));
  }
  static get watchers() { return {
    "term": ["termChanged"]
  }; }
  static get style() { return klevuSearchLandingPageCss; }
}, [1, "klevu-search-landing-page", {
    "limit": [2],
    "term": [1],
    "sort": [1],
    "filterCount": [2, "filter-count"],
    "filterCustomOrder": [16],
    "usePagination": [4, "use-pagination"],
    "renderProductSlot": [16],
    "results": [32],
    "manager": [32],
    "currentViewPortSize": [32]
  }, [[0, "productClick", "productClickHandler"], [4, "klevu-filter-selection-updates", "filterSelectionUpdate"]]]);
_KlevuSearchLandingPage_resultObject = new WeakMap(), _KlevuSearchLandingPage_clickEvent = new WeakMap(), _KlevuSearchLandingPage_viewportUtil = new WeakMap(), _KlevuSearchLandingPage_layoutElement = new WeakMap(), _KlevuSearchLandingPage_instances = new WeakSet(), _KlevuSearchLandingPage_initialFetch = async function _KlevuSearchLandingPage_initialFetch() {
  var _a, _b, _c, _d;
  const result = await KlevuFetch(search(this.term, { limit: this.limit, sort: this.sort }, sendSearchEvent(), listFilters({
    filterManager: this.manager,
    limit: this.filterCount,
    rangeFilterSettings: [
      {
        key: "klevu_price",
        minMax: true,
      },
    ],
  }), applyFilterWithManager(this.manager)));
  __classPrivateFieldSet(this, _KlevuSearchLandingPage_resultObject, result.queriesById("search"), "f");
  this.results = (_b = (_a = __classPrivateFieldGet(this, _KlevuSearchLandingPage_resultObject, "f")) === null || _a === void 0 ? void 0 : _a.records) !== null && _b !== void 0 ? _b : [];
  __classPrivateFieldSet(this, _KlevuSearchLandingPage_clickEvent, (_d = (_c = __classPrivateFieldGet(this, _KlevuSearchLandingPage_resultObject, "f")) === null || _c === void 0 ? void 0 : _c.getSearchClickSendEvent) === null || _d === void 0 ? void 0 : _d.call(_c), "f");
}, _KlevuSearchLandingPage_internalRenderProductSlot = function _KlevuSearchLandingPage_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);
    } }));
}, _KlevuSearchLandingPage_sizeChange = function _KlevuSearchLandingPage_sizeChange(event) {
  this.currentViewPortSize = event.detail;
}, _KlevuSearchLandingPage_sortChanged = async function _KlevuSearchLandingPage_sortChanged(event) {
  this.sort = event.detail;
  await __classPrivateFieldGet(this, _KlevuSearchLandingPage_instances, "m", _KlevuSearchLandingPage_initialFetch).call(this);
}, _KlevuSearchLandingPage_applyFilters = function _KlevuSearchLandingPage_applyFilters() {
  __classPrivateFieldGet(this, _KlevuSearchLandingPage_instances, "m", _KlevuSearchLandingPage_initialFetch).call(this);
  __classPrivateFieldGet(this, _KlevuSearchLandingPage_layoutElement, "f").closeDrawer();
};
function defineCustomElement$1() {
  if (typeof customElements === "undefined") {
    return;
  }
  const components = ["klevu-search-landing-page", "klevu-accordion", "klevu-button", "klevu-checkbox", "klevu-drawer", "klevu-dropdown", "klevu-facet", "klevu-facet-list", "klevu-layout-results", "klevu-pagination", "klevu-product", "klevu-product-grid", "klevu-slider", "klevu-sort", "klevu-typography", "klevu-util-viewport"];
  components.forEach(tagName => { switch (tagName) {
    case "klevu-search-landing-page":
      if (!customElements.get(tagName)) {
        customElements.define(tagName, KlevuSearchLandingPage$1);
      }
      break;
    case "klevu-accordion":
      if (!customElements.get(tagName)) {
        defineCustomElement$g();
      }
      break;
    case "klevu-button":
      if (!customElements.get(tagName)) {
        defineCustomElement$f();
      }
      break;
    case "klevu-checkbox":
      if (!customElements.get(tagName)) {
        defineCustomElement$e();
      }
      break;
    case "klevu-drawer":
      if (!customElements.get(tagName)) {
        defineCustomElement$d();
      }
      break;
    case "klevu-dropdown":
      if (!customElements.get(tagName)) {
        defineCustomElement$c();
      }
      break;
    case "klevu-facet":
      if (!customElements.get(tagName)) {
        defineCustomElement$b();
      }
      break;
    case "klevu-facet-list":
      if (!customElements.get(tagName)) {
        defineCustomElement$a();
      }
      break;
    case "klevu-layout-results":
      if (!customElements.get(tagName)) {
        defineCustomElement$9();
      }
      break;
    case "klevu-pagination":
      if (!customElements.get(tagName)) {
        defineCustomElement$8();
      }
      break;
    case "klevu-product":
      if (!customElements.get(tagName)) {
        defineCustomElement$7();
      }
      break;
    case "klevu-product-grid":
      if (!customElements.get(tagName)) {
        defineCustomElement$6();
      }
      break;
    case "klevu-slider":
      if (!customElements.get(tagName)) {
        defineCustomElement$5();
      }
      break;
    case "klevu-sort":
      if (!customElements.get(tagName)) {
        defineCustomElement$4();
      }
      break;
    case "klevu-typography":
      if (!customElements.get(tagName)) {
        defineCustomElement$3();
      }
      break;
    case "klevu-util-viewport":
      if (!customElements.get(tagName)) {
        defineCustomElement$2();
      }
      break;
  } });
}
defineCustomElement$1();

const KlevuSearchLandingPage = KlevuSearchLandingPage$1;
const defineCustomElement = defineCustomElement$1;

export { KlevuSearchLandingPage, defineCustomElement };

//# sourceMappingURL=klevu-search-landing-page.js.map