(()=>{/*!
 * Copyright 2024-2025 Simply.IN Sp. z o.o.
 *
 * NOTICE OF LICENSE
 * Licensed under the EUPL-1.2 or later.
 * You may not use this work except in compliance with the Licence.
 * Copy of the Licence is available at: https://joinup.ec.europa.eu/software/page/eupl
 * It is bundled with this package in the file LICENSE.txt
 *
 * @author    Simply.IN Sp. z o.o.
 * @copyright 2024-2025 Simply.IN Sp. z o.o.
 * @license   https://joinup.ec.europa.eu/software/page/eupl
 */(function(d,i){"use strict";var m=function(e){this.options=i.extend({checkoutType:"classic",emailFieldSelector:null,containerSelector:null,ajaxUrl:null,ajaxToken:null,debug:!1},e),this.state={isCheckboxVisible:!1,isPhoneValid:!1,isInitialized:!1,isLoggedIn:!1,userExistsInSimplyIn:!1},this.phones=null,this.checkboxElement=null,this.init()};m.prototype={init:function(){this.state.isInitialized||(typeof simplyin<"u"&&simplyin.customer&&simplyin.customer.has_token&&(this.state.isLoggedIn=!0),this.autoDetectSelectors(),this.loadPhoneCountries(),this.setupPaymentStepWatcher(),this.bindAddressCountryChange(),this.state.isInitialized=!0)},autoDetectSelectors:function(){this.options.emailFieldSelector||(this.options.checkoutType==="thecheckout"?(this.options.emailFieldSelector='.account-fields input[name="email"]',this.options.containerSelector="#confirm_order"):(this.options.emailFieldSelector='#customer-form input[name="email"]',this.options.containerSelector="#checkout-payment-step .payment-options"))},formatDigits:function(e){var n=e.replace(/(\d{3})(?=\d)/g,"1 ".replace("1","$1"));return n.replace(/ (\d)$/,"$1")},visibleCheckboxExists:function(){return i('[id="simplyin-phone-checkbox"]').filter(function(){return!this.closest('[id="simplyin-phone-prerender"]')}).length>0},removeAllPrerenders:function(){i('[id="simplyin-phone-prerender"]').remove()},revalidateVisible:function(){if(this.visibleCheckboxExists()){var e=i("#simplyin-phone-checkbox .phone-input input");if(e.length&&e.val()!==""){if(this.detectExplicitPrefix(e.val())){this.parseAndSetPhoneNumber(e.val());return}this.validatePhoneNumber()&&this.storePhoneNumber()}}},refreshFromPrerender:function(){var e=this,n=this.consumePrerender();!n||this.state.isLoggedIn||this.state.userExistsInSimplyIn||(i('[id="simplyin-phone-checkbox"]').filter(function(){return!this.closest('[id="simplyin-phone-prerender"]')}).remove(),this.insertCheckboxIntoDOM(n),this.initCheckboxEvents(),this.state.isCheckboxVisible=!0,this.syncCountryFromCurrentAddressSelection(),this.revalidateVisible())},setupPaymentStepWatcher:function(){var e=this;if(this.options.checkoutType==="classic"){var n=document.getElementById("checkout");if(n){var t=function(){var s=document.getElementById("checkout-payment-step");!s||!s.classList.contains("-current")||(e.visibleCheckboxExists()?i('[id="simplyin-phone-prerender"]').length&&e.refreshFromPrerender():e.showCheckbox())};t();var o=new MutationObserver(t);o.observe(n,{attributes:!0,childList:!0,subtree:!0,attributeFilter:["class"]})}}},showCheckbox:function(){var e=this;this.state.isLoggedIn||this.state.userExistsInSimplyIn||this._showingCheckbox||(this._showingCheckbox=!0,i("#simplyin-phone-checkbox").filter(function(){return!this.closest("#simplyin-phone-prerender")}).remove(),this.state.isCheckboxVisible=!1,this.fetchCheckboxHtml().then(function(n){e._showingCheckbox=!1,n&&(e.state.isLoggedIn||e.state.userExistsInSimplyIn||(e.insertCheckboxIntoDOM(n),e.initCheckboxEvents(),e.state.isCheckboxVisible=!0,e.syncCountryFromCurrentAddressSelection()))}))},hideCheckbox:function(e){e&&(this.state.isLoggedIn=!0),i("#simplyin-phone-checkbox").fadeOut(300,function(){i(this).remove()}),this.state.isCheckboxVisible=!1},fetchCheckboxHtml:function(){var e=this;return e.options.checkoutType!=="thecheckout"?e.waitForPrerender(600).then(function(n){return n||e.fetchCheckboxHtmlAjax()}):e.fetchCheckboxHtmlAjax()},consumePrerender:function(){var e=i('[id="simplyin-phone-prerender"]');if(!e.length)return null;var n=e.last().html();return e.remove(),n&&n.indexOf("simplyin-phone-checkbox")!==-1?n:null},waitForPrerender:function(e){var n=this,t=n.consumePrerender();return t?Promise.resolve(t):new Promise(function(o){var s=!1,r=new MutationObserver(function(){var h=n.consumePrerender();h&&!s&&(s=!0,r.disconnect(),o(h))});r.observe(document.body,{childList:!0,subtree:!0}),setTimeout(function(){s||(s=!0,r.disconnect(),o(null))},e)})},fetchCheckboxHtmlAjax:function(){var e=this;return new Promise(function(n){i.ajax({url:e.options.ajaxUrl,method:"POST",data:{action:"getNewAccountForm",ajax:1,ajax_token:e.options.ajaxToken,checkout_type:e.options.checkoutType},dataType:"json"}).done(function(t){t.success&&t.html?n(t.html):n(null)}).fail(function(){n(null)})})},insertCheckboxIntoDOM:function(e){this.removeAllPrerenders();var n=i(this.options.containerSelector);n.length&&(this.options.checkoutType==="thecheckout"?n.parent().before(e):n.append(e),this.checkboxElement=i("#simplyin-phone-checkbox"))},initCheckboxEvents:function(){var e=this;i(document).off("change.simplyin","#simplyin-phone-checkbox #simply-save-checkbox"),i(document).on("change.simplyin","#simplyin-phone-checkbox #simply-save-checkbox",function(){i(this).is(":checked")?e.showPhoneInput():e.hidePhoneInput()}),i(document).off("change.simplyin","#simplyin-phone-checkbox #simplyin-phone-country"),i(document).on("change.simplyin","#simplyin-phone-checkbox #simplyin-phone-country",function(){e.updateCountryFlag()}),i(document).off("input.simplyin change.simplyin","#simplyin-phone-checkbox .phone-input input"),i(document).on("input.simplyin change.simplyin","#simplyin-phone-checkbox .phone-input input",function(){var n=i(this).val();if(e.detectExplicitPrefix(n)){e.hideValidationWarning(),e.state.isPhoneValid=false,e.parseAndSetPhoneNumber(n);return}e._pendingPhoneNumber=null,i(this).val(n.replace(/[^\d\s]/g,"")),e.validatePhoneNumber()&&e.storePhoneNumber()}),i(document).off("keypress.simplyin","#simplyin-phone-checkbox .phone-input input"),i(document).on("keypress.simplyin","#simplyin-phone-checkbox .phone-input input",function(n){var t=n.which>=48&&n.which<=57,r=this.selectionStart===0&&this.selectionEnd===this.value.length,o=n.which===43&&r;!t&&!o&&n.preventDefault()}),i(document).off("click.simplyin","#simplyin-phone-checkbox .phone-input"),i(document).on("click.simplyin","#simplyin-phone-checkbox .phone-input",function(){i("#simplyin-phone-checkbox .phone-input input").focus()}),this.bindOrderButton(),i("#simplyin-phone-checkbox #simply-save-checkbox").prop("checked")&&this.showPhoneInput()},showPhoneInput:function(){i("#simplyin-phone-checkbox .simplyin-phone-input").slideDown(300);var e=i("#simplyin-phone-checkbox .phone-input input");e.val()===""&&this.options.checkoutType!=="thecheckout"?setTimeout(function(){e.focus()},350):e.val()!==""&&this.validatePhoneNumber()&&this.storePhoneNumber()},hidePhoneInput:function(){i("#simplyin-phone-checkbox .simplyin-phone-input").slideUp(300),this.hideValidationWarning(),this.storePhoneNumber(!0)},applyCountrySelectionDisplay:function(e){var n=e.data("flag"),t=e.data("prefix");i(".simplyin-flag-display img").attr("src",simplyin.files_url+"views/img/flags/"+n+".svg"),i(".phone-country-select .prefix").html(t),i("#simplyin-phone-checkbox .phone-input .prefix").html(t)},updateCountryFlag:function(){var e=i("#simplyin-phone-checkbox #simplyin-phone-country option:selected");this.applyCountrySelectionDisplay(e),i("#simplyin-phone-checkbox .phone-input input").focus(),i("#simplyin-phone-checkbox #simply-save-checkbox").trigger("change.simplyin")},bindAddressCountryChange:function(){var e=this;i(document).off("change.simplyin",".js-country"),i(document).on("change.simplyin",".js-country",function(){var n=parseInt(i(this).val(),10);n&&e.syncCountryFromAddressCountry(n)})},syncCountryFromCurrentAddressSelection:function(){var e=i(".js-country").filter(function(){return i(this).val()}).first();if(e.length){var n=parseInt(e.val(),10);n&&this.syncCountryFromAddressCountry(n)}},syncCountryFromAddressCountry:function(e){if(!this.visibleCheckboxExists())return;var n=i("#simplyin-phone-checkbox .phone-input input");if(n.length&&n.val()!=="")return;if(typeof simplyin>"u"||!simplyin.country_iso_by_id)return;var t=simplyin.country_iso_by_id[e];if(!t)return;var o=i("#simplyin-phone-checkbox #simplyin-phone-country"),s=o.find('option[value="'+t+'"]');if(!s.length||o.val()===s.val())return;o.val(s.val()),this.applyCountrySelectionDisplay(s)},validatePhoneNumber:function(e){var n=i("#simplyin-phone-checkbox .phone-input input");if(!n.length)return!0;var t=n.val().replace(/\D/g,""),o=i("#simplyin-phone-checkbox #simplyin-phone-country option:selected").data("prefix");if(!this.phones)return!1;var s=this.phones.find(function(a){return a.country_code===o});if(!s)return this.showValidationWarning(e),this.state.isPhoneValid=!1,!1;if(t.charAt(0)==="0"){var r=t.slice(1),h=s.phone_number_lengths.indexOf(r.length)!==-1&&s.mobile_begin_with.some(function(a){return r.indexOf(a)===0});h&&(t=r)}var l=s.phone_number_lengths.indexOf(t.length)!==-1,c=s.mobile_begin_with.some(function(a){return t.indexOf(a)===0}),p=this.formatDigits(t);return n.val(p),!l||!c?(this.showValidationWarning(e),this.state.isPhoneValid=!1,!1):(this.hideValidationWarning(),this.state.isPhoneValid=!0,!0)},showValidationWarning:function(e){var n=i("#simplyin-phone-checkbox .phone-input-warning");e?n.hide().fadeIn():n.show()},hideValidationWarning:function(){i("#simplyin-phone-checkbox .phone-input-warning").hide()},storePhoneNumber:function(e){var n="0";if(!e){var t=i("#simplyin-phone-checkbox .phone-input input"),o=i("#simplyin-phone-checkbox #simplyin-phone-country option:selected").data("prefix"),s=t.val().replace(/\D/g,"");n=o+" "+s}i.ajax({url:this.options.ajaxUrl,method:"POST",data:{action:"storeNewAccountDetails",ajax:1,ajax_token:this.options.ajaxToken,phone:n},dataType:"json"})},bindOrderButton:function(){var e=document.querySelector("#payment-confirmation button");if(e){var n=this,t=function(o){var s=i("#simplyin-phone-checkbox #simply-save-checkbox");if(s.is(":checked")){var r=i("#simplyin-phone-checkbox .phone-input input"),h=i("#simplyin-phone-checkbox #simplyin-phone-country option:selected").data("prefix"),l=r.val().replace(/\D/g,""),c=h+" "+l;if(i.ajax({url:n.options.ajaxUrl,method:"POST",data:{action:"storeNewAccountDetails",ajax:1,ajax_token:n.options.ajaxToken,phone:c},dataType:"json",async:!1}),!n.validatePhoneNumber(!0))return o.preventDefault(),o.stopPropagation(),o.stopImmediatePropagation(),!1}};e._simplyinPhoneHandler&&e.removeEventListener("click",e._simplyinPhoneHandler,{capture:!0}),e.addEventListener("click",t,{capture:!0}),e._simplyinPhoneHandler=t}},loadPhoneCountries:function(){var e=this;i.getJSON(simplyin.files_url+"views/json/phones.json",function(n){e.phones=n,e.revalidateVisible()}).fail(function(){})},removeCountryPrefix:function(e){if(!e)return e;var n=e.trim().replace(/\s+/g,""),t=i("#simplyin-phone-checkbox #simplyin-phone-country").find("option:selected");if(!t.length)return n;var o=t.data("prefix");if(!o)return n;var s=o,r=o.indexOf("+")===0?o.substring(1):o;return n.indexOf(s)===0?n.substring(s.length):n.indexOf(r)===0?n.substring(r.length):n},getMaxPhoneLengthForSelectedCountry:function(){var e=i("#simplyin-phone-checkbox #simplyin-phone-country").find("option:selected").data("prefix");if(!this.phones)return 9;var n=this.phones.find(function(t){return t.country_code===e});return!n||!n.phone_number_lengths?9:Math.max.apply(null,n.phone_number_lengths)},detectExplicitPrefix:function(e){var n=(e||"").replace(/[^\d+]/g,""),t=n.replace(/\+/g,"");return n.indexOf("+")===0?"+"+t:n.indexOf("00")===0?"+"+t.slice(2):null},parseAndSetPhoneNumber:function(e){var n=this;if(e){if(!this.phones){n._pendingPhoneNumber=e;if(!n._phoneNumberRetryScheduled){if(n._phoneNumberRetryCount=(n._phoneNumberRetryCount||0)+1,n._phoneNumberRetryCount>10){n._pendingPhoneNumber=null;return}n._phoneNumberRetryScheduled=true,setTimeout(function(){n._phoneNumberRetryScheduled=false;var i=n._pendingPhoneNumber;n._pendingPhoneNumber=null,n.parseAndSetPhoneNumber(i)},500)}return}n._pendingPhoneNumber=null;var r=n.detectExplicitPrefix(e);if(!r){i("#simplyin-phone-checkbox .phone-input input").val(e.replace(/\D/g,"")),n.validatePhoneNumber()&&n.storePhoneNumber();return}var o=null,s=0,g=r.replace(/\D/g,"");for(var h=0;h<this.phones.length;h++){var l=this.phones[h],c=l.country_code.replace(/\s/g,"");r.indexOf(c)===0&&c.length>s&&(o=l,s=c.length)}if(o){var p=i("#simplyin-phone-checkbox #simplyin-phone-country"),a=p.find('option[data-prefix="'+o.country_code+'"]');if(a.length===0)return i("#simplyin-phone-checkbox .phone-input input").val(g),!1;p.val(a.val()),n.applyCountrySelectionDisplay(a);var u=r.substring(s);if(u.charAt(0)==="0"){var f=u.slice(1),b=o.phone_number_lengths.indexOf(f.length)!==-1&&o.mobile_begin_with.some(function(v){return f.indexOf(v)===0});b&&(u=f)}var x=n.formatDigits(u);return i("#simplyin-phone-checkbox .phone-input input").val(x),setTimeout(function(){n.validatePhoneNumber()&&n.storePhoneNumber()},100),!0}return i("#simplyin-phone-checkbox .phone-input input").val(g),!1}},destroy:function(){i(document).off(".simplyin"),i("#simplyin-phone-checkbox").remove(),this.state.isInitialized=!1}},d.SimplyInPhoneCheckboxManager=m})(window,jQuery);})();
