import { r as registerInstance, c as createEvent, h, H as Host } from './index-439360eb.js';
import { K as KlevuInit } from './klevu-init-c4ef62f2.js';
import { F as FilterManager } from './filterManager-aafc4b62.js';
import { b as KlevuListenDomEvent, a as KlevuDomEvents } from './KlevuSearchSorting-7c3a8978.js';
import { K as KlevuFetch, a as applyFilterWithManager } from './klevuFetch-88f95ce0.js';
import { s as search } from './search-88a2f77e.js';
import { k as kmcRecommendation } from './kmcRecommendation-c213fb4c.js';
import { c as categoryMerchandising, s as sendMerchandisingViewEvent } from './categoryMerchandising-17e14c65.js';
import { l as listFilters } from './listFilters-aa5a52bb.js';
import { s as sendSearchEvent } from './sendSearchEvent-b68900f8.js';
import './isBrowser-38d37bdf.js';

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 _KlevuQuery_instances, _KlevuQuery_stopFilterListening, _KlevuQuery_lastResult, _KlevuQuery_fetch;
const KlevuQuery = class {
  constructor(hostRef) {
    registerInstance(this, hostRef);
    this.klevuQueryResult = createEvent(this, "klevuQueryResult", 7);
    _KlevuQuery_instances.add(this);
    _KlevuQuery_stopFilterListening.set(this, void 0);
    _KlevuQuery_lastResult.set(this, void 0);
    this.type = undefined;
    this.sendSearchViewEvent = undefined;
    this.options = undefined;
    this.sort = undefined;
    this.limit = undefined;
    this.offset = undefined;
    this.filterCount = undefined;
    this.category = undefined;
    this.categoryTitle = undefined;
    this.searchTerm = undefined;
    this.recommendationId = undefined;
    this.manager = new FilterManager();
    this.updateOnFilterChange = undefined;
  }
  onPropertyChange() {
    this.fetchAgain();
  }
  /**
   * Force component to fetch results again
   */
  async fetchAgain() {
    await __classPrivateFieldGet(this, _KlevuQuery_instances, "m", _KlevuQuery_fetch).call(this);
  }
  async connectedCallback() {
    await KlevuInit.ready();
    await __classPrivateFieldGet(this, _KlevuQuery_instances, "m", _KlevuQuery_fetch).call(this);
    if (this.updateOnFilterChange) {
      __classPrivateFieldSet(this, _KlevuQuery_stopFilterListening, KlevuListenDomEvent(KlevuDomEvents.FilterSelectionUpdate, async () => {
        await __classPrivateFieldGet(this, _KlevuQuery_instances, "m", _KlevuQuery_fetch).call(this);
      }), "f");
    }
  }
  disconnectedCallback() {
    if (__classPrivateFieldGet(this, _KlevuQuery_stopFilterListening, "f")) {
      __classPrivateFieldGet(this, _KlevuQuery_stopFilterListening, "f").call(this);
    }
  }
  onProductClick(event) {
    var _a, _b, _c, _d, _e, _f;
    const product = event.detail.product;
    if (!__classPrivateFieldGet(this, _KlevuQuery_lastResult, "f") || !product.id) {
      return;
    }
    switch (this.type) {
      case "merchandising":
        if (this.categoryTitle) {
          (_b = (_a = __classPrivateFieldGet(this, _KlevuQuery_lastResult, "f")).categoryMerchandisingClickEvent) === null || _b === void 0 ? void 0 : _b.call(_a, {
            productId: product.id,
            categoryTitle: this.categoryTitle,
            variantId: product.itemGroupId,
          });
        }
        break;
      case "recommendation":
        (_d = (_c = __classPrivateFieldGet(this, _KlevuQuery_lastResult, "f")).recommendationClickEvent) === null || _d === void 0 ? void 0 : _d.call(_c, { productId: product.id, variantId: product.itemGroupId });
        break;
      case "search":
        (_f = (_e = __classPrivateFieldGet(this, _KlevuQuery_lastResult, "f")).searchClickEvent) === null || _f === void 0 ? void 0 : _f.call(_e, { productId: product.id, variantId: product.itemGroupId });
        break;
      default:
        const e = this.type;
        throw new Error(e);
    }
  }
  render() {
    return (h(Host, null, h("slot", null)));
  }
  static get watchers() { return {
    "manager": ["onPropertyChange"],
    "recommendationId": ["onPropertyChange"],
    "searchTerm": ["onPropertyChange"],
    "categoryTitle": ["onPropertyChange"],
    "category": ["onPropertyChange"],
    "filterCount": ["onPropertyChange"],
    "offset": ["onPropertyChange"],
    "limit": ["onPropertyChange"],
    "sort": ["onPropertyChange"],
    "options": ["onPropertyChange"],
    "type": ["onPropertyChange"]
  }; }
};
_KlevuQuery_stopFilterListening = new WeakMap(), _KlevuQuery_lastResult = new WeakMap(), _KlevuQuery_instances = new WeakSet(), _KlevuQuery_fetch = async function _KlevuQuery_fetch() {
  var _a, _b, _c;
  const options = Object.assign({ limit: this.limit, sort: this.sort, id: "klevuquery", offset: this.offset }, this.options);
  switch (this.type) {
    case "merchandising": {
      if (!this.category || !this.categoryTitle) {
        throw new Error("Missing category or category title");
      }
      const result = await KlevuFetch(categoryMerchandising(this.category, options, sendMerchandisingViewEvent(this.categoryTitle), listFilters({ filterManager: this.manager, limit: this.filterCount }), applyFilterWithManager(this.manager)));
      const resultObject = result.queriesById("klevuquery");
      if (!resultObject) {
        console.error("KlevuQuery: Response meta", (_a = result.apiResponse) === null || _a === void 0 ? void 0 : _a.meta);
        throw new Error("KlevuQuery: Couldn't do fetch.");
      }
      __classPrivateFieldSet(this, _KlevuQuery_lastResult, resultObject, "f");
      this.klevuQueryResult.emit({
        result: resultObject,
        manager: this.manager,
      });
      break;
    }
    case "recommendation": {
      if (!this.recommendationId) {
        throw new Error("Recommendation requires KMC id");
      }
      const result = await KlevuFetch(kmcRecommendation(this.recommendationId));
      const resultObject = result.queriesById("klevuquery");
      if (!resultObject) {
        console.error("KlevuQuery: Response meta", (_b = result.apiResponse) === null || _b === void 0 ? void 0 : _b.meta);
        throw new Error("KlevuQuery: Couldn't do fetch.");
      }
      __classPrivateFieldSet(this, _KlevuQuery_lastResult, resultObject, "f");
      this.klevuQueryResult.emit({
        result: resultObject,
        manager: this.manager,
      });
      break;
    }
    case "search": {
      if (!this.searchTerm) {
        throw new Error("Search query requires search term");
      }
      const modifiers = [];
      if (this.sendSearchViewEvent) {
        modifiers.push(sendSearchEvent());
      }
      const result = await KlevuFetch(search(this.searchTerm, options, ...modifiers));
      const resultObject = result.queriesById("klevuquery");
      if (!resultObject) {
        console.error("KlevuQuery: Response meta", (_c = result.apiResponse) === null || _c === void 0 ? void 0 : _c.meta);
        throw new Error("KlevuQuery: Couldn't do fetch.");
      }
      __classPrivateFieldSet(this, _KlevuQuery_lastResult, resultObject, "f");
      this.klevuQueryResult.emit({
        result: resultObject,
        manager: this.manager,
      });
      break;
    }
    default:
      const e = this.type;
      throw new Error(e);
  }
};

export { KlevuQuery as klevu_query };

//# sourceMappingURL=klevu-query.entry.js.map