'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

const index = require('./index-97fdbb7d.js');
const klevuInit = require('./klevu-init-57de47f9.js');
const parts = require('./parts-295eb56e.js');
const getTranslation = require('./getTranslation-a3cc5add.js');
const moi = require('./moi-5f434778.js');
require('./isBrowser-e8e10249.js');

const klevuProductQueryPopupCss = ":host{display:block;text-align:left}.content{padding:var(--klevu-spacing-04);box-sizing:border-box;display:flex;flex-direction:column;height:100%}klevu-chat-layout{height:500px}@media (max-width: 520px){klevu-chat-layout{height:100%}}klevu-chat-messages{padding-right:var(--klevu-spacing-04);height:100%}.header{display:flex;align-items:center;justify-content:space-between}.header klevu-icon{cursor:pointer}div[part=\"product-query-header\"]{width:100%}div[part=\"product-query-footer\"] .inputs{display:flex;gap:var(--klevu-spacing-04)}div[part=\"product-query-footer\"] .sendmessage{display:flex;gap:var(--klevu-spacing-04)}div[part=\"product-query-footer\"] .sendmessage klevu-textfield{width:100%}.pqa_feedback{text-align:center}.pqa_feedback>*{padding-bottom:var(--klevu-spacing-04)}.pqa_feedback klevu-icon{font-size:24px;padding:var(--klevu-spacing-04);cursor:pointer;border-radius:50%}.pqa_feedback klevu-icon:hover{background-color:var(--klevu-color-neutral-2)}.pqa_feedback klevu-icon:nth-child(1){color:var(--klevu-color-accent-1)}.pqa_feedback klevu-icon:nth-child(2){color:var(--klevu-color-accent-3)}klevu-typography.fineprint{display:block;--klevu-body-xs-size:10px;padding-bottom:var(--klevu-spacing-04);margin-bottom:var(--klevu-spacing-04);border-bottom:1px solid var(--klevu-color-neutral-4)}.loading-sorry{display:block;--klevu-body-xs-size:10px}";

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 _KlevuProductQueryPopup_instances, _KlevuProductQueryPopup_popup, _KlevuProductQueryPopup_layoutElement, _KlevuProductQueryPopup_contentDiv, _KlevuProductQueryPopup_sendMessage, _KlevuProductQueryPopup_register, _KlevuProductQueryPopup_pqafeedback, _KlevuProductQueryPopup_closeModal, _KlevuProductQueryPopup_start, _KlevuProductQueryPopup_onAction, _KlevuProductQueryPopup_onFeedback, _KlevuProductQueryPopup_renderChat, _KlevuProductQueryPopup_renderFeedback;
const KlevuProductQueryPopup = class {
  constructor(hostRef) {
    index.registerInstance(this, hostRef);
    _KlevuProductQueryPopup_instances.add(this);
    _KlevuProductQueryPopup_popup.set(this, void 0);
    _KlevuProductQueryPopup_layoutElement.set(this, void 0);
    _KlevuProductQueryPopup_contentDiv.set(this, void 0);
    this.url = "";
    this.productId = undefined;
    this.pqaWidgetId = undefined;
    this.textFieldVariant = "pill";
    this.tTextFieldPlaceholder = getTranslation.getTranslation("productQueryPopup.tTextFieldPlaceholder");
    this.tPopupTitle = getTranslation.getTranslation("productQueryPopup.tPopuptitle");
    this.tButtonText = getTranslation.getTranslation("productQueryPopup.tButtonText");
    this.tFinePrint = getTranslation.getTranslation("productQueryPopup.tFinePrint");
    this.tLoadingSorry = getTranslation.getTranslation("productQueryPopup.tLoadingSorry");
    this.tRateExperienceTitle = getTranslation.getTranslation("productQueryPopup.tRateExperienceTitle");
    this.tRateExperienceText = getTranslation.getTranslation("productQueryPopup.tRateExperienceText");
    this.tDataProtectionNotice = getTranslation.getTranslation("productQueryPopup.tDataProtectionNotice");
    this.askButtonText = undefined;
    this.useBackground = undefined;
    this.popupAnchor = "bottom-start";
    this.popupOffset = undefined;
    this.settings = undefined;
    this.originElement = undefined;
    this.disableCloseOutsideClick = undefined;
    this.config = undefined;
    this.useNativeScrollbars = undefined;
    this.text = "";
    this.name = "";
    this.email = "";
    this.registered = true // change to false when registering works
    ;
    this.showFeedback = false;
    this.showLoading = false;
    this.showLoadingSorry = false;
    this.feedbacks = [];
    this.showMessageFeedbackFor = undefined;
    this.messages = [];
  }
  async connectedCallback() {
    var _a, _b;
    await klevuInit.KlevuInit.ready();
    if ((_a = window.klevu_page_meta) === null || _a === void 0 ? void 0 : _a.itemId) {
      this.productId = window.klevu_page_meta.itemId;
    }
    if (!this.productId && this.url == "") {
      const canonical = document.querySelector("link[rel='canonical']");
      this.url = (_b = canonical === null || canonical === void 0 ? void 0 : canonical.href) !== null && _b !== void 0 ? _b : window.location.href;
    }
  }
  onMessage() {
    var _a;
    if (!this.session) {
      return;
    }
    this.messages = this.session.messages;
    this.feedbacks = this.session.feedbacks;
    (_a = __classPrivateFieldGet(this, _KlevuProductQueryPopup_layoutElement, "f")) === null || _a === void 0 ? void 0 : _a.scrollMainToBottom();
  }
  async onMessageFeedback(e) {
    var _a, _b, _c;
    await ((_a = this.session) === null || _a === void 0 ? void 0 : _a.addFeedback(e.detail.feedback.id, e.detail.feedback.thumbs, e.detail.reason));
    this.feedbacks = ((_b = this.session) === null || _b === void 0 ? void 0 : _b.feedbacks) || [];
    this.messages = ((_c = this.session) === null || _c === void 0 ? void 0 : _c.messages) || [];
  }
  render() {
    return (index.h(index.Host, null, index.h("klevu-popup", { ref: (el) => (__classPrivateFieldSet(this, _KlevuProductQueryPopup_popup, el, "f")), exportparts: parts.parts["klevu-popup"], onKlevuPopupClose: __classPrivateFieldGet(this, _KlevuProductQueryPopup_instances, "m", _KlevuProductQueryPopup_closeModal).bind(this), onKlevuPopupOpen: () => {
        __classPrivateFieldGet(this, _KlevuProductQueryPopup_instances, "m", _KlevuProductQueryPopup_start).call(this);
      }, anchor: this.popupAnchor, offset: this.popupOffset, elevation: 3, useBackground: this.useBackground, popupWidth: 520, originElement: this.originElement, closeAtOutsideClick: !this.disableCloseOutsideClick, fullscreenOnMobileSize: true }, index.h("div", { class: "content", slot: "content", ref: (el) => (__classPrivateFieldSet(this, _KlevuProductQueryPopup_contentDiv, el, "f")) }, this.showFeedback ? __classPrivateFieldGet(this, _KlevuProductQueryPopup_instances, "m", _KlevuProductQueryPopup_renderFeedback).call(this) : __classPrivateFieldGet(this, _KlevuProductQueryPopup_instances, "m", _KlevuProductQueryPopup_renderChat).call(this)))));
  }
  get el() { return index.getElement(this); }
};
_KlevuProductQueryPopup_popup = new WeakMap(), _KlevuProductQueryPopup_layoutElement = new WeakMap(), _KlevuProductQueryPopup_contentDiv = new WeakMap(), _KlevuProductQueryPopup_instances = new WeakSet(), _KlevuProductQueryPopup_sendMessage = async function _KlevuProductQueryPopup_sendMessage() {
  var _a, _b;
  const message = this.text.trim();
  if (message == "") {
    return;
  }
  this.text = "";
  (_a = __classPrivateFieldGet(this, _KlevuProductQueryPopup_layoutElement, "f")) === null || _a === void 0 ? void 0 : _a.scrollMainToBottom();
  this.showLoading = true;
  const timeout = setTimeout(() => {
    this.showLoadingSorry = true;
  }, 4000);
  await ((_b = this.session) === null || _b === void 0 ? void 0 : _b.query({
    message: message,
    klevuSettings: this.settings,
  }));
  clearTimeout(timeout);
  this.showLoadingSorry = false;
  this.showLoading = false;
}, _KlevuProductQueryPopup_register = function _KlevuProductQueryPopup_register() {
  var _a, _b;
  (_a = this.session) === null || _a === void 0 ? void 0 : _a.messages.push({
    message: {
      id: "register",
      type: "text",
      value: `${this.name}, (${this.email})`,
      note: null,
    },
  });
  this.registered = true;
  (_b = __classPrivateFieldGet(this, _KlevuProductQueryPopup_layoutElement, "f")) === null || _b === void 0 ? void 0 : _b.scrollMainToBottom();
}, _KlevuProductQueryPopup_pqafeedback = function _KlevuProductQueryPopup_pqafeedback(dir) {
  var _a;
  (_a = __classPrivateFieldGet(this, _KlevuProductQueryPopup_popup, "f")) === null || _a === void 0 ? void 0 : _a.closeModal();
}, _KlevuProductQueryPopup_closeModal = function _KlevuProductQueryPopup_closeModal(e) {
  /*
  if (!this.showFeedback) {
    this.showFeedback = true
    e.preventDefault()
    return
  }
  */
}, _KlevuProductQueryPopup_start = async function _KlevuProductQueryPopup_start() {
  var _a, _b, _c;
  this.showFeedback = false;
  const useConfig = ((_a = this.config) === null || _a === void 0 ? void 0 : _a.apiKey) && ((_b = this.config) === null || _b === void 0 ? void 0 : _b.apiKey) !== "";
  this.session = await moi.startMoi({
    onMessage: this.onMessage.bind(this),
    // Do nothing on redirect as we have our own system
    onRedirect: () => { },
    url: this.url === "" ? undefined : this.url,
    productId: this.productId,
    mode: "PQA",
    onAction: __classPrivateFieldGet(this, _KlevuProductQueryPopup_instances, "m", _KlevuProductQueryPopup_onAction).bind(this),
    pqaWidgetId: this.pqaWidgetId,
    settings: {
      configOverride: useConfig ? this.config : undefined,
    },
  });
  this.messages = this.session.messages;
  this.feedbacks = this.session.feedbacks;
  await ((_c = __classPrivateFieldGet(this, _KlevuProductQueryPopup_layoutElement, "f")) === null || _c === void 0 ? void 0 : _c.scrollMainToBottom("instant"));
  // add this when registering works
  //this.registered = this.messages.length > 1
}, _KlevuProductQueryPopup_onAction = function _KlevuProductQueryPopup_onAction(action) {
  if (action.type !== "askFeedbackReason") {
    return;
  }
  this.showMessageFeedbackFor = action.context.messageId;
}, _KlevuProductQueryPopup_onFeedback = async function _KlevuProductQueryPopup_onFeedback(e) {
  var _a, _b;
  await ((_a = this.session) === null || _a === void 0 ? void 0 : _a.addFeedback(e.detail.message.id, e.detail.feedback));
  this.feedbacks = ((_b = this.session) === null || _b === void 0 ? void 0 : _b.feedbacks) || [];
}, _KlevuProductQueryPopup_renderChat = function _KlevuProductQueryPopup_renderChat() {
  return (index.h(index.Fragment, null, index.h("klevu-chat-layout", { useNativeScrollbars: this.useNativeScrollbars, ref: (el) => (__classPrivateFieldSet(this, _KlevuProductQueryPopup_layoutElement, el, "f")) }, index.h("div", { part: "product-query-header", slot: "header" }, index.h("div", { class: "header" }, index.h("klevu-typography", { variant: "body-m-bold" }, this.tPopupTitle), index.h("klevu-icon", { name: "close", onClick: () => { var _a; return (_a = __classPrivateFieldGet(this, _KlevuProductQueryPopup_popup, "f")) === null || _a === void 0 ? void 0 : _a.closeModal(); } })), index.h("klevu-typography", { variant: "body-xs", class: "fineprint" }, this.tFinePrint, index.h("slot", { name: "after-fineprint" }))), index.h("klevu-chat-messages", { messages: this.messages, feedbacks: this.feedbacks, enableMessageFeedback: true, onKlevuMessageFeedback: __classPrivateFieldGet(this, _KlevuProductQueryPopup_instances, "m", _KlevuProductQueryPopup_onFeedback).bind(this), showFeedbackFor: this.showMessageFeedbackFor }, index.h("div", { slot: "chat-messages-after" }, this.showLoading ? index.h("klevu-loading-indicator", null) : null, this.showLoadingSorry ? (index.h("klevu-typography", { class: "loading-sorry", variant: "body-xs" }, this.tLoadingSorry)) : null)), index.h("div", { part: "product-query-footer", slot: "footer" }, !this.registered ? (index.h(index.Fragment, null, index.h("div", { class: "inputs" }, index.h("klevu-textfield", { value: this.name, variant: "pill", placeholder: "Name", onKlevuTextEnterPressed: () => __classPrivateFieldGet(this, _KlevuProductQueryPopup_instances, "m", _KlevuProductQueryPopup_register).call(this), onKlevuTextChanged: (e) => (this.name = e.detail) }), index.h("klevu-textfield", { value: this.email, variant: "pill", placeholder: "Email", onKlevuTextEnterPressed: () => __classPrivateFieldGet(this, _KlevuProductQueryPopup_instances, "m", _KlevuProductQueryPopup_register).call(this), onKlevuTextChanged: (e) => {
      this.email = e.detail;
    } }), index.h("klevu-button", { icon: "send", onClick: () => __classPrivateFieldGet(this, _KlevuProductQueryPopup_instances, "m", _KlevuProductQueryPopup_register).call(this) })), index.h("klevu-typography", { variant: "body-xs" }, this.tDataProtectionNotice))) : (index.h("div", { class: "sendmessage" }, index.h("klevu-textfield", { value: this.text, variant: this.textFieldVariant, placeholder: this.tTextFieldPlaceholder, onKlevuTextEnterPressed: () => __classPrivateFieldGet(this, _KlevuProductQueryPopup_instances, "m", _KlevuProductQueryPopup_sendMessage).call(this), onKlevuTextChanged: (e) => (this.text = e.detail) }), this.askButtonText ? (index.h("klevu-button", { onClick: () => __classPrivateFieldGet(this, _KlevuProductQueryPopup_instances, "m", _KlevuProductQueryPopup_sendMessage).call(this) }, this.askButtonText)) : (index.h("klevu-button", { icon: "chevron_right", onClick: () => __classPrivateFieldGet(this, _KlevuProductQueryPopup_instances, "m", _KlevuProductQueryPopup_sendMessage).call(this) }))))))));
}, _KlevuProductQueryPopup_renderFeedback = function _KlevuProductQueryPopup_renderFeedback() {
  return (index.h("div", { class: "pqa_feedback", part: "product-query-feedback" }, index.h("klevu-typography", { variant: "body-l-bold" }, this.tRateExperienceTitle), index.h("klevu-typography", { variant: "body-m" }, this.tRateExperienceText), index.h("div", null, index.h("klevu-icon", { onClick: () => __classPrivateFieldGet(this, _KlevuProductQueryPopup_instances, "m", _KlevuProductQueryPopup_pqafeedback).call(this, "up"), name: "thumb_up" }), index.h("klevu-icon", { onClick: () => __classPrivateFieldGet(this, _KlevuProductQueryPopup_instances, "m", _KlevuProductQueryPopup_pqafeedback).call(this, "down"), name: "thumb_down" }))));
};
KlevuProductQueryPopup.style = klevuProductQueryPopupCss;

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

const KlevuUtilPortal = class {
  constructor(hostRef) {
    index.registerInstance(this, hostRef);
  }
  connectedCallback() {
    this.createRoot();
  }
  createRoot() {
    if (this.root) {
      return;
    }
    this.root = document.createElement("div");
    const assignedContent = [];
    this.el.querySelectorAll("slot").forEach((slot) => {
      assignedContent.push(...slot.assignedElements());
    });
    this.root.appendChild(this.el);
    for (const element of assignedContent) {
      this.el.childNodes[0].appendChild(element);
    }
    this.root.setAttribute("data-name", "klevu-portal");
    document.body.appendChild(this.root);
  }
  disconnectedCallback() {
    if (!this.root) {
      return;
    }
    this.root.remove();
  }
  render() {
    return (index.h(index.Host, null, index.h("slot", null)));
  }
  get el() { return index.getElement(this); }
};
KlevuUtilPortal.style = klevuUtilPortalCss;

exports.klevu_product_query_popup = KlevuProductQueryPopup;
exports.klevu_util_portal = KlevuUtilPortal;

//# sourceMappingURL=klevu-product-query-popup_2.cjs.entry.js.map