1if(!customElements.get("tax-exempt-manager")){class TaxExemptManager extends HTMLElement{static Selectors={INPUT_FIELD:"[data-vat-id-input]",HIDDEN_INPUT_FIELD:"[data-vat-id-hidden-input]",SAVE_BUTTON:"[data-save-button]",UNSET_BUTTON:"[data-unset-button]",CONFIG:"[data-config]",INPUT_ICON:"[data-input-icon]",MESSAGE:"[data-message]"};static ErrorCodes={VAT_DOES_NOT_EXIST:"VAT_DOES_NOT_EXIST",VAT_NOT_VALID:"VAT_NOT_VALID",UNSET_ERROR:"UNSET_ERROR",SOMETHING_WENT_WRONG:"SOMETHING_WENT_WRONG"};_checkmarkSvg='<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"><path fill="#008000" d="M20.285 2l-11.285 11.567-5.286-5.011-3.714 3.716 9 8.728 15-15.285z" /></svg>';_redmarkSvg='<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="5 5 30 30"><path stroke="#b22222" stroke-width="4" d="M 10,10 L 30,30 M 30,10 L 10,30" /></svg>';constructor(){super(),this._loggingEnabled=!0}connectedCallback(){this._log("INFO","Component connected and initialized."),this._log("INFO","Configuration",this._configData),this._saveButton?this._saveButton.addEventListener("click",this._handleSaveClick):this._log("ERROR","Save button not found"),this._unsetButton?this._unsetButton.addEventListener("click",this._handleUnsetClick):this._log("WARNING","Unset button not found")}disconnectedCallback(){this._log("INFO","Component disconnected. Cleaning up listeners."),this._saveButton&&this._saveButton.removeEventListener("click",this._handleSaveClick),this._unsetButton&&this._unsetButton.removeEventListener("click",this._handleUnsetClick)}get _configData(){const dataScript=this._config;return dataScript?JSON.parse(dataScript.text):null}get _saveButton(){return this.querySelector(TaxExemptManager.Selectors.SAVE_BUTTON)}get _unsetButton(){return this.querySelector(TaxExemptManager.Selectors.UNSET_BUTTON)}get _inputField(){return this.querySelector(TaxExemptManager.Selectors.INPUT_FIELD)}get _hiddenInputField(){return this.querySelector(TaxExemptManager.Selectors.HIDDEN_INPUT_FIELD)}get _config(){return this.querySelector(TaxExemptManager.Selectors.CONFIG)}get _inputIcon(){return this.querySelector(TaxExemptManager.Selectors.INPUT_ICON)}get _message(){return this.querySelector(TaxExemptManager.Selectors.MESSAGE)}_handleSaveClick=async e=>{e.preventDefault(),this._resetUi(),this._log("INFO","Save button clicked."),this._toggleButtonLoading(this._saveButton,!0);const value=this._inputField.value.replace(/\s/g,"").toUpperCase(),{error}=await this._validateAndStoreVatID(value);error?this._handleError(error):(this._handleSuccess(this._configData.strings.vat_id_added),this._unsetButton&&(this._log("INFO","Removing Unset button disabled status"),this._unsetButton.removeAttribute("disabled")),this._updateHiddenInput(value)),this._toggleButtonLoading(this._saveButton,!1)};_handleUnsetClick=async e=>{e.preventDefault(),this._resetUi(),this._log("INFO","Unset button clicked."),this._toggleButtonLoading(this._unsetButton,!0);const{error,success}=await this._unsetCustomerVat();error?this._handleError(TaxExemptManager.ErrorCodes.UNSET_ERROR):(this._handleSuccess(this._configData.strings.vat_deleted),this._inputField.value="",this._updateHiddenInput("")),this._toggleButtonLoading(this._unsetButton,!1),success&&(this._log("INFO","Setting Unset button to disabled"),this._unsetButton.setAttribute("disabled",""))};async _validateAndStoreVatID(vatId){const payload={vat:{vat_id:vatId}},{shop,app}=this._configData;this._log("INFO","Validating VAT ID:",payload);try{const data=await this._fetchAPI("POST",`${app.url}/api/v1/vat/validate?shopify_domain=${shop.domain}`,payload);return data.vat.exists?data.vat.valid?(this._configData.customer.id!==""&&await this._setCustomerVat(vatId),{success:!0}):(this._log("WARNING","Vat ID is not valid"),{error:TaxExemptManager.ErrorCodes.VAT_NOT_VALID}):(this._log("WARNING","Vat ID does not exist"),{error:TaxExemptManager.ErrorCodes.VAT_DOES_NOT_EXIST})}catch(error){return this._log("ERROR","Run into an issue Validating Vat ID",error),{error:TaxExemptManager.ErrorCodes.SOMETHING_WENT_WRONG}}}_setCustomerVat=async vatId=>{this._log("INFO",`Setting customer VAT ID to "${vatId}".`);const{customer,shop,app}=this._configData,payload={customer:{vat_id:vatId,customer_id:customer.id,shopify_domain:shop.domain}};try{await this._fetchAPI("POST",`${app.url}/api/v1/customer/set`,payload),this._log("INFO","Customer Vat ID updated")}catch{this._log("ERROR","Something went wrong updating Customer Vat ID")}};_unsetCustomerVat=async()=>{this._log("INFO","Unsetting customer VAT ID.");const{customer,shop,app}=this._configData,payload={customer:{customer_id:customer.id,shopify_domain:shop.domain}};try{return await this._fetchAPI("POST",`${app.url}/api/v1/customer/unset`,payload),this._log("INFO","Customer Vat ID unset"),{success:!0}}catch{return this._log("ERROR","Something went wrong unsetting Customer Vat ID"),{error:!0}}};_fetchAPI=async(method,endpoint,payload=null)=>{this._log("INFO",`Fetching [${method}]`,endpoint),payload&&this._log("INFO","Payload:",payload);const controller=new AbortController,timeoutId=setTimeout(()=>controller.abort(),1e4);try{const response=await fetch(endpoint,{method:method.toUpperCase(),headers:{"Content-Type":"application/json"}
1,body:payload?JSON.stringify(payload):null,signal:controller.signal});if(clearTimeout(timeoutId),!response.ok)throw new Error(`HTTP error! Status: ${response.status}`);const responseText=await response.text();if(!responseText)return this._log("WARNING","API Response was empty."),null;const data=JSON.parse(responseText);return this._log("INFO","API Response:",data),data}catch(error){throw this._log("ERROR","Fetch API Error:",error.message),clearTimeout(timeoutId),error}};_resetUi(){this._clearMessage(),this._toggleButtonLoading(this._saveButton,!1),this._toggleButtonLoading(this._deleteButton,!1)}_handleSuccess(message){this._inputIcon.innerHTML=this._checkmarkSvg,this._renderMessage(message,"SUCCESS")}_handleError(errorCode){this._inputIcon.innerHTML=this._redmarkSvg;const{strings}=this._configData,errorMessage=(()=>{switch(errorCode){case TaxExemptManager.ErrorCodes.VAT_DOES_NOT_EXIST:return strings.vat_does_not_exist;case TaxExemptManager.ErrorCodes.VAT_NOT_VALID:return strings.vat_not_valid;case TaxExemptManager.ErrorCodes.UNSET_ERROR:return strings.vat_deleted_error;default:return strings.error_general}})();this._renderMessage(errorMessage,"ERROR")}_renderMessage=(msg,type="INFO")=>{this._log("INFO","Displaying message",type,msg);const className=(()=>{switch(type){case"ERROR":return"text-error";case"SUCCESS":return"text-success";default:return""}})();this._message.innerHTML=`<div class="${className}">${msg}</div>`};_clearMessage(){this._log("INFO","Clearing message"),this._message.innerHTML=""}_toggleButtonLoading=(btn,loading)=>{if(btn){this._log("INFO","Setting button loading state",btn,loading),btn.toggleAttribute("disabled",loading),btn.classList.toggle("loading",loading);const loadingSpinner=btn.querySelector(".loading__spinner");loadingSpinner&&loadingSpinner.classList.toggle("hidden",!loading)}};_updateHiddenInput(vatId){this._hiddenInputField&&(this._hiddenInputField.value=vatId)}_log=(type,...args)=>{if(!this._loggingEnabled)return;const styles={INFO:"background: #007bff; color: #ffffff; font-weight: bold; padding: 2px 5px; border-radius: 3px;",WARNING:"background: #ffc107; color: #000000; font-weight: bold; padding: 2px 5px; border-radius: 3px;",ERROR:"background: #dc3545; color: #ffffff; font-weight: bold; padding: 2px 5px; border-radius: 3px;"};console.log(`%c[${type}]`,styles[type]||styles.INFO,...args)}}customElements.define("tax-exempt-manager",TaxExemptManager)} 2//# sourceMappingURL=/cdn/shop/t/143/assets/latori-tem.js.map?v=129021958695202408771771497557
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.