import { r as registerInstance, c as createEvent, h, H as Host } from './index-345463dd.js';
import { K as KlevuInit } from './klevu-init-0abe4d30.js';
import { p as parts } from './parts-d5ec35e6.js';
import { c as KlevuEvents, K as KlevuTypeOfRequest, b as KlevuFetch } from './klevuFetch-4a3f7817.js';
import { k as kmcRecommendation } from './kmcRecommendation-660e7ed1.js';
import './KlevuDomEvents-a1ecbf7c.js';
import './isBrowser-38d37bdf.js';
import './KlevuApiRawResponse-82e24d1e.js';
import './personalisation-4e689dad.js';

const recommendationTypeOfRequests = [
    KlevuTypeOfRequest.AlsoBought,
    KlevuTypeOfRequest.AlsoViewed,
    KlevuTypeOfRequest.NewArrivals,
    KlevuTypeOfRequest.SimilarProducts,
    KlevuTypeOfRequest.Trending,
];
/**
 * This modifier should be used with all recommendation requests. It sends
 * correct event data to klevu backend on recommendation view
 *
 * @category Modifier
 * @returns
 */
function sendRecommendationViewEvent(title, eventData, override) {
    return {
        klevuModifierId: "sendMerchandisingViewEvent",
        onResult: (res, f) => {
            var _a, _b, _c, _d, _e, _f, _g, _h, _j;
            // is used with kmcRecommendation query
            if (f.klevuFunctionId === "kmcRecommendation") {
                if (!title) {
                    title = (_c = (_b = (_a = f.params) === null || _a === void 0 ? void 0 : _a.kmcConfig) === null || _b === void 0 ? void 0 : _b.metadata.title) !== null && _c !== void 0 ? _c : "";
                }
                const products = (_f = res.queriesById((_e = (_d = f.queries) === null || _d === void 0 ? void 0 : _d[0].id) !== null && _e !== void 0 ? _e : "")) === null || _f === void 0 ? void 0 : _f.records;
                if (!products) {
                    console.warn("No result in recommendations. Can't send recommendationViewEvent");
                    return res;
                }
                const kmcData = (_g = f.params) === null || _g === void 0 ? void 0 : _g.kmcConfig;
                if (!kmcData) {
                    console.warn("Problem with kmcData fetching. Can't send recommendationViewEvent");
                    return res;
                }
                if (products.length === 0) {
                    return res;
                }
                KlevuEvents.recommendationView({
                    recommendationMetadata: Object.assign(Object.assign({}, kmcData.metadata), { title }),
                    products,
                    override,
                });
                return res;
            }
            if (!eventData) {
                throw new Error("Need to provider eventData parameter for custom view events");
            }
            const queries = (_h = f.queries) === null || _h === void 0 ? void 0 : _h.filter((q) => recommendationTypeOfRequests.includes(q.typeOfRequest));
            if (!queries || (queries === null || queries === void 0 ? void 0 : queries.length) == 0) {
                return res;
            }
            const products = (_j = res.queriesById(queries[0].id)) === null || _j === void 0 ? void 0 : _j.records;
            if (!products) {
                console.warn("No products in result. Can't send recommendationViewEvent");
                return res;
            }
            eventData.title = title !== null && title !== void 0 ? title : eventData.title;
            KlevuEvents.recommendationView({
                recommendationMetadata: eventData,
                products,
            });
            return res;
        },
    };
}

const klevuRecommendationsCss = ":host{display:block}";

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 __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 _KlevuRecommendations_responseObject;
const KlevuRecommendations = class {
  constructor(hostRef) {
    registerInstance(this, hostRef);
    this.klevuData = createEvent(this, "klevuData", 7);
    _KlevuRecommendations_responseObject.set(this, void 0);
    this.recommendationTitle = undefined;
    this.recommendationId = undefined;
    this.cartProductIds = undefined;
    this.categoryPath = undefined;
    this.currentProductId = undefined;
    this.itemGroupId = undefined;
    this.products = [undefined, undefined, undefined, undefined, undefined, undefined];
  }
  async connectedCallback() {
    var _a, _b;
    await KlevuInit.ready();
    if (!this.recommendationId) {
      throw new Error("recommendationId is required");
    }
    const res = await KlevuFetch(kmcRecommendation(this.recommendationId, {
      id: "recommendation",
      cartProductIds: this.cartProductIds,
      categoryPath: this.categoryPath,
      currentProductId: this.currentProductId,
      itemGroupId: this.itemGroupId,
    }, sendRecommendationViewEvent(this.recommendationTitle)));
    __classPrivateFieldSet(this, _KlevuRecommendations_responseObject, res.queriesById("recommendation"), "f");
    if (__classPrivateFieldGet(this, _KlevuRecommendations_responseObject, "f")) {
      if (this.recommendationTitle === undefined) {
        this.recommendationTitle = (_b = (_a = __classPrivateFieldGet(this, _KlevuRecommendations_responseObject, "f").getQueryParameters()) === null || _a === void 0 ? void 0 : _a.kmcConfig) === null || _b === void 0 ? void 0 : _b.metadata.title;
      }
      this.products = __classPrivateFieldGet(this, _KlevuRecommendations_responseObject, "f").records;
      this.klevuData.emit(__classPrivateFieldGet(this, _KlevuRecommendations_responseObject, "f"));
    }
  }
  productClick(event) {
    var _a, _b;
    if (((_a = __classPrivateFieldGet(this, _KlevuRecommendations_responseObject, "f")) === null || _a === void 0 ? void 0 : _a.recommendationClickEvent) && event.detail.product.id) {
      (_b = __classPrivateFieldGet(this, _KlevuRecommendations_responseObject, "f")) === null || _b === void 0 ? void 0 : _b.recommendationClickEvent({
        productId: event.detail.product.id,
        variantId: event.detail.product.itemGroupId || event.detail.product.id,
      });
    }
  }
  render() {
    if (this.products.length === 0) {
      return null;
    }
    return (h(Host, null, h("slot", null, h("klevu-slides", { heading: this.recommendationTitle }, this.products.map((product) => (h("klevu-product", { exportparts: parts["klevu-product"], fixedWidth: true, product: product, style: {
        "--klevu-product-width": "300px",
      } })))))));
  }
};
_KlevuRecommendations_responseObject = new WeakMap();
KlevuRecommendations.style = klevuRecommendationsCss;

export { KlevuRecommendations as klevu_recommendations };

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