System.register(["../../../common/modal-legacy.DcruUX9x.js","./product-question.scss-legacy.C8vOMVfq.js"],(function(e,t){"use strict";var s,a;return{setters:[e=>{s=e.c,a=e.s},null],execute:function(){((e,t,n)=>{const{__:i}=e.wp.i18n,o={init:()=>{n(t).on("click",".saai-ask-question-btn",o.initPopup),n(t).on("click",".saai-ask-question-widget",o.initPopup),n(t).on("click",".saai-qa-ask-btn",o.askQuestion),n(t).on("click",".saai-qa-reset-btn",o.askAnother),n(t).on("click",".saai-qa-close-btn",o.closePopup),n(t).on("keyup","textarea.saai-user-question",o.countTextCharacters),o.maybeForceShowQuestionsTab(),o.injectWidget()},injectWidget:()=>{if(!window.saaiPqObj||!window.saaiPqObj.widgetEnabled||"yes"!==window.saaiPqObj.widgetEnabled)return;let e="";e="bottom-right"===(window.saaiPqObj.widgetPosition||"bottom-right")?`\n          <div class="saai-ask-question-widget saai-ask-question-btn bottom-right">\n            <svg fill="#000000" width="34px" height="34px" viewBox="0 0 16 16" xmlns="http://www.w3.org/2000/svg"><path d="M8 .5A7.77 7.77 0 0 0 0 8a7.77 7.77 0 0 0 8 7.5A7.77 7.77 0 0 0 16 8 7.77 7.77 0 0 0 8 .5zm0 13.75A6.52 6.52 0 0 1 1.25 8 6.52 6.52 0 0 1 8 1.75 6.52 6.52 0 0 1 14.75 8 6.52 6.52 0 0 1 8 14.25z"/><circle cx="7.98" cy="10.95" r=".76"/><path d="M9.73 4.75A2.72 2.72 0 0 0 8 4.19a2.28 2.28 0 0 0-2.41 2.17v.11h1.24v-.1A1.12 1.12 0 0 1 8 5.33a1 1 0 0 1 1.12 1c0 .35-.24.73-.78 1.11a2 2 0 0 0-1 1.46v.36h1.24V9a.76.76 0 0 1 .23-.51A3.92 3.92 0 0 1 9.33 8l.17-.14a2 2 0 0 0 .91-1.67 1.85 1.85 0 0 0-.68-1.44z"/></svg>\n            <span class="saai-widget-tooltip">${i("Ask a Question","storeagent-ai-for-woocommerce")}</span>\n          </div>\n        `:`\n          <div class="saai-ask-question-widget saai-ask-question-btn mid-right">\n            <span>${i("Ask a Question","storeagent-ai-for-woocommerce")}</span>\n          </div>\n        `,n("body").append(e)},initPopup:()=>{a({id:"saai-qa-modal",content:n("#tmpl-saai-qa").html()||"",width:960,height:300,flexCenter:!0})},resizeTextArea:t=>{t.addClass("sent");const s=t.val().split("\n"),a=e.getComputedStyle(t[0]),n=parseInt(a.lineHeight||"0"),i=parseInt((null==a?void 0:a.paddingTop)||"0"),o=parseInt((null==a?void 0:a.paddingBottom)||"0"),r=parseInt((null==a?void 0:a.paddingLeft)||"0"),c=parseInt((null==a?void 0:a.paddingRight)||"0");let l=0;s.forEach((e=>{const s=e.length*n,a=t[0].clientWidth-r-c;l+=Math.ceil(s/a)}));const d=l*n+i+o;t.animate({height:`${d}px`},500)},askAnother:e=>{const t=n(e.target).closest(".qa-form"),s=t.find(".user-question");s.animate({height:"150px"},500),s.prop("readonly",!1),s.removeClass("sent"),t.find(".submit-btn-container").show(),t.find(".submit-btn-container button").prop("disabled",!0),t.find(".reset-btn-container").hide(),t.find(".close-btn-container").hide(),t.find(".ai-answer").html("")},closePopup:e=>{n(e.target).closest(".qa-form").find(".saai-qa-reset-btn").trigger("click"),s("saai-qa-modal")},askQuestion:e=>{e.preventDefault();const t=n(e.target).closest(".qa-form"),s=t.find(".user-question"),a=t.find(".submit-btn-container"),r=t.find(".reset-btn-container"),c=t.find(".close-btn-container"),l=s.val(),d=t.find(".loading");d.addClass("show"),o.resizeTextArea(s),s.prop("readonly",!0),a.hide(),n.ajax({url:window.saaiPqObj.restUrl,type:"POST",beforeSend:e=>{e.setRequestHeader("X-WP-Nonce",window.saaiPqObj.wpNonce)},data:{question:l},success:({answer:e})=>{const s=e?`<div class="answer-text">${e}</div>`:`<p class="error">${i("An error occurred","storeagent-ai-for-woocommerce")}</p>`;t.find(".ai-answer").html(`<p class="answer-label">${i("Answer:","storeagent-ai-for-woocommerce")}</p>${s}`),d.removeClass("show"),r.show(),c.show()},error:e=>{const s=JSON.parse(e.responseText);console.log((null==s?void 0:s.message)||"An error occurred"),null!=(null==s?void 0:s.message)&&t.find(".ai-answer").html(`<p class="error">${null==s?void 0:s.message}</p>`),d.removeClass("show"),r.show(),c.show()}})},maybeForceShowQuestionsTab:()=>{window.location.href.includes("questions-page")&&n(".woocommerce-tabs #tab-title-product_questions a").trigger("click")},countTextCharacters:e=>{const t=n(e.target),s=n(".user-question-field-wrap .character-count .current-count"),a=t.closest(".qa-form").find(".saai-qa-ask-btn"),i=t.val().length;s.text(null!=i?i:0),a.prop("disabled",!i)}};n((()=>{o.init()}))})(window,document,window.jQuery)}}}));
