import { proxyCustomElement, HTMLElement, h, Host, Fragment } from '@stencil/core/internal/client';
import { K as KlevuInit } from './klevu-init2.js';
import { g as getTranslation } from './getTranslation.js';
import { p as partsExports } from './partsExports.js';
import { d as defineCustomElement$d } from './klevu-button2.js';
import { d as defineCustomElement$c } from './klevu-chat-bubble2.js';
import { d as defineCustomElement$b } from './klevu-chat-layout2.js';
import { d as defineCustomElement$a } from './klevu-chat-messages2.js';
import { d as defineCustomElement$9 } from './klevu-icon2.js';
import { d as defineCustomElement$8 } from './klevu-loading-indicator2.js';
import { d as defineCustomElement$7 } from './klevu-popup2.js';
import { d as defineCustomElement$6 } from './klevu-product2.js';
import { d as defineCustomElement$5 } from './klevu-rating2.js';
import { d as defineCustomElement$4 } from './klevu-slides2.js';
import { d as defineCustomElement$3 } from './klevu-textfield2.js';
import { d as defineCustomElement$2 } from './klevu-typography2.js';
import { d as defineCustomElement$1 } from './klevu-util-scrollbars2.js';
import { s as startMoi } from './moi.js';

const klevuProductQueryPopupCss = ":host{display:block;text-align:left;--popup-height:var(--klevu-product-query-popup-height, 500px);--popup-width:var(--klevu-product-query-popup-width, 520px)}.content{padding:var(--klevu-spacing-04);box-sizing:border-box;display:flex;flex-direction:column;height:100%}klevu-chat-layout{height:var(--popup-height)}@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-popup-header\"]{width:100%}div[part=\"product-query-popup-footer\"] .inputs{display:flex;gap:var(--klevu-spacing-04)}div[part=\"product-query-popup-footer\"] .sendmessage{display:flex;gap:var(--klevu-spacing-04)}div[part=\"product-query-popup-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}klevu-popup{--klevu-popup-width:var(--popup-width)}";

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 = /*@__PURE__*/ proxyCustomElement(class KlevuProductQueryPopup extends HTMLElement {
  constructor() {
    super();
    this.__registerHost();
    this.__attachShadow();
    _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("productQueryPopup.tTextFieldPlaceholder");
    this.tPopupTitle = getTranslation("productQueryPopup.tPopuptitle");
    this.tButtonText = getTranslation("productQueryPopup.tButtonText");
    this.tFinePrint = getTranslation("productQueryPopup.tFinePrint");
    this.tLoadingSorry = getTranslation("productQueryPopup.tLoadingSorry");
    this.tRateExperienceTitle = getTranslation("productQueryPopup.tRateExperienceTitle");
    this.tRateExperienceText = getTranslation("productQueryPopup.tRateExperienceText");
    this.tDataProtectionNotice = 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.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 (h(Host, null, h("klevu-popup", { ref: (el) => (__classPrivateFieldSet(this, _KlevuProductQueryPopup_popup, el, "f")), exportparts: partsExports("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, originElement: this.originElement, closeAtOutsideClick: !this.disableCloseOutsideClick, fullscreenOnMobileSize: true }, 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 this; }
  static get style() { return klevuProductQueryPopupCss; }
}, [1, "klevu-product-query-popup", {
    "url": [1],
    "productId": [1, "product-id"],
    "pqaWidgetId": [1, "pqa-widget-id"],
    "textFieldVariant": [1, "text-field-variant"],
    "tTextFieldPlaceholder": [1, "t-text-field-placeholder"],
    "tPopupTitle": [1, "t-popup-title"],
    "tButtonText": [1, "t-button-text"],
    "tFinePrint": [1, "t-fine-print"],
    "tLoadingSorry": [1, "t-loading-sorry"],
    "tRateExperienceTitle": [1, "t-rate-experience-title"],
    "tRateExperienceText": [1, "t-rate-experience-text"],
    "tDataProtectionNotice": [1, "t-data-protection-notice"],
    "askButtonText": [1, "ask-button-text"],
    "useBackground": [4, "use-background"],
    "popupAnchor": [1, "popup-anchor"],
    "popupOffset": [2, "popup-offset"],
    "settings": [16],
    "originElement": [16],
    "disableCloseOutsideClick": [4, "disable-close-outside-click"],
    "config": [16],
    "useNativeScrollbars": [4, "use-native-scrollbars"],
    "text": [32],
    "name": [32],
    "email": [32],
    "registered": [32],
    "showFeedback": [32],
    "showLoading": [32],
    "showLoadingSorry": [32],
    "feedbacks": [32],
    "showMessageFeedbackFor": [32],
    "messages": [32]
  }, [[0, "klevuMessageFeedbackReason", "onMessageFeedback"]]]);
_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 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 (h(Fragment, null, h("klevu-chat-layout", { useNativeScrollbars: this.useNativeScrollbars, ref: (el) => (__classPrivateFieldSet(this, _KlevuProductQueryPopup_layoutElement, el, "f")) }, h("div", { part: "product-query-popup-header", slot: "header" }, h("div", { class: "header" }, h("klevu-typography", { variant: "body-m-bold" }, this.tPopupTitle), h("klevu-icon", { name: "close", onClick: () => { var _a; return (_a = __classPrivateFieldGet(this, _KlevuProductQueryPopup_popup, "f")) === null || _a === void 0 ? void 0 : _a.closeModal(); } })), h("klevu-typography", { variant: "body-xs", class: "fineprint" }, this.tFinePrint, h("slot", { name: "after-fineprint" }))), 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 }, h("div", { slot: "chat-messages-after" }, this.showLoading ? (h("klevu-loading-indicator", { exportparts: partsExports("klevu-loading-indicator") })) : null, this.showLoadingSorry ? (h("klevu-typography", { class: "loading-sorry", variant: "body-xs" }, this.tLoadingSorry)) : null)), h("div", { part: "product-query-popup-footer", slot: "footer" }, !this.registered ? (h(Fragment, null, h("div", { class: "inputs" }, h("klevu-textfield", { exportparts: partsExports("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) }), h("klevu-textfield", { exportparts: partsExports("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;
    } }), h("klevu-button", { icon: "send", onClick: () => __classPrivateFieldGet(this, _KlevuProductQueryPopup_instances, "m", _KlevuProductQueryPopup_register).call(this) })), h("klevu-typography", { variant: "body-xs" }, this.tDataProtectionNotice))) : (h("div", { class: "sendmessage" }, h("klevu-textfield", { exportparts: partsExports("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 ? (h("klevu-button", { onClick: () => __classPrivateFieldGet(this, _KlevuProductQueryPopup_instances, "m", _KlevuProductQueryPopup_sendMessage).call(this) }, this.askButtonText)) : (h("klevu-button", { icon: "chevron_right", onClick: () => __classPrivateFieldGet(this, _KlevuProductQueryPopup_instances, "m", _KlevuProductQueryPopup_sendMessage).call(this) }))))))));
}, _KlevuProductQueryPopup_renderFeedback = function _KlevuProductQueryPopup_renderFeedback() {
  return (h("div", { class: "pqa_feedback", part: "product-query-popup-feedback" }, h("klevu-typography", { variant: "body-l-bold" }, this.tRateExperienceTitle), h("klevu-typography", { variant: "body-m" }, this.tRateExperienceText), h("div", null, h("klevu-icon", { onClick: () => __classPrivateFieldGet(this, _KlevuProductQueryPopup_instances, "m", _KlevuProductQueryPopup_pqafeedback).call(this, "up"), name: "thumb_up" }), h("klevu-icon", { onClick: () => __classPrivateFieldGet(this, _KlevuProductQueryPopup_instances, "m", _KlevuProductQueryPopup_pqafeedback).call(this, "down"), name: "thumb_down" }))));
};
function defineCustomElement() {
  if (typeof customElements === "undefined") {
    return;
  }
  const components = ["klevu-product-query-popup", "klevu-button", "klevu-chat-bubble", "klevu-chat-layout", "klevu-chat-messages", "klevu-icon", "klevu-loading-indicator", "klevu-popup", "klevu-product", "klevu-rating", "klevu-slides", "klevu-textfield", "klevu-typography", "klevu-util-scrollbars"];
  components.forEach(tagName => { switch (tagName) {
    case "klevu-product-query-popup":
      if (!customElements.get(tagName)) {
        customElements.define(tagName, KlevuProductQueryPopup);
      }
      break;
    case "klevu-button":
      if (!customElements.get(tagName)) {
        defineCustomElement$d();
      }
      break;
    case "klevu-chat-bubble":
      if (!customElements.get(tagName)) {
        defineCustomElement$c();
      }
      break;
    case "klevu-chat-layout":
      if (!customElements.get(tagName)) {
        defineCustomElement$b();
      }
      break;
    case "klevu-chat-messages":
      if (!customElements.get(tagName)) {
        defineCustomElement$a();
      }
      break;
    case "klevu-icon":
      if (!customElements.get(tagName)) {
        defineCustomElement$9();
      }
      break;
    case "klevu-loading-indicator":
      if (!customElements.get(tagName)) {
        defineCustomElement$8();
      }
      break;
    case "klevu-popup":
      if (!customElements.get(tagName)) {
        defineCustomElement$7();
      }
      break;
    case "klevu-product":
      if (!customElements.get(tagName)) {
        defineCustomElement$6();
      }
      break;
    case "klevu-rating":
      if (!customElements.get(tagName)) {
        defineCustomElement$5();
      }
      break;
    case "klevu-slides":
      if (!customElements.get(tagName)) {
        defineCustomElement$4();
      }
      break;
    case "klevu-textfield":
      if (!customElements.get(tagName)) {
        defineCustomElement$3();
      }
      break;
    case "klevu-typography":
      if (!customElements.get(tagName)) {
        defineCustomElement$2();
      }
      break;
    case "klevu-util-scrollbars":
      if (!customElements.get(tagName)) {
        defineCustomElement$1();
      }
      break;
  } });
}
defineCustomElement();

export { KlevuProductQueryPopup as K, defineCustomElement as d };

//# sourceMappingURL=klevu-product-query-popup2.js.map