class d{constructor(){this.addressSelector=null,this.form=null,this.formContainer=null,this.countrySelect=null,this.citySelect=null,this.init()}init(){if(this.addressSelector=document.getElementById("addressSelector"),this.form=document.getElementById("billingAddressForm"),this.formContainer=document.getElementById("addressFormContainer"),this.countrySelect=document.getElementById("country_id"),this.citySelect=document.getElementById("city_id"),!this.addressSelector||!this.form||!this.formContainer)return;this.bindEvents();const i=new URLSearchParams(window.location.search).get("selected");i&&this.addressSelector.querySelector(`option[value="${i}"]`)&&(this.addressSelector.value=i,this.handleAddressSelection(i))}bindEvents(){this.addressSelector.addEventListener("change",i=>{this.handleAddressSelection(i.target.value)}),this.countrySelect&&this.countrySelect.addEventListener("change",i=>{this.loadCities(i.target.value)});const e=document.getElementById("cancelFormBtn");e&&e.addEventListener("click",()=>{this.hideForm()})}handleAddressSelection(e){if(!e){this.hideForm();return}e==="new"?this.showNewAddressForm():this.loadAndEditAddress(e)}showNewAddressForm(){this.form.reset(),document.getElementById("methodField").value="POST",document.getElementById("addressId").value="",this.form.action=window.billingAddressStoreUrl,this.clearCities(),this.showForm()}async loadAndEditAddress(e){var i,s;try{const t=await fetch(`${window.billingAddressApiUrl}/${e}/show`,{method:"GET",headers:{"X-CSRF-TOKEN":window.csrfToken,"Content-Type":"application/json",Accept:"application/json"}});if(!t.ok)throw new Error("Failed to load address data");const o=await t.json();o.success?(this.populateForm(o.address),document.getElementById("methodField").value="PUT",document.getElementById("addressId").value=e,this.form.action=`${window.billingAddressStoreUrl.replace("/store","")}/${e}`,this.showForm(),o.address.country_id&&await this.loadCities(o.address.country_id,o.address.city_id)):this.showError(o.message||((i=window.translations)==null?void 0:i.loadingError)||"Failed to load address")}catch(t){console.error("Error loading address:",t),this.showError(((s=window.translations)==null?void 0:s.loadingError)||"Failed to load address")}}async loadCities(e,i=null){if(!(!e||!this.citySelect))try{const s=await fetch(`${window.citiesEndpoint}/${e}`);if(!s.ok)throw new Error("Failed to load cities");const t=await s.json();this.citySelect.innerHTML='<option value="">Select City</option>';let o=[];Array.isArray(t)?o=t:t.cities&&Array.isArray(t.cities)?o=t.cities:t.data&&Array.isArray(t.data)&&(o=t.data),o.forEach(n=>{const r=document.createElement("option");r.value=n.id,r.textContent=n.name,i&&parseInt(n.id)===parseInt(i)&&(r.selected=!0),this.citySelect.appendChild(r)})}catch(s){console.error("Error loading cities:",s),this.clearCities()}}clearCities(){this.citySelect&&(this.citySelect.innerHTML='<option value="">Select City</option>')}populateForm(e){["first_name","last_name","title","company_name","email","phone","country_id","city_id","address1","address2","state","postcode","tax_id","is_default"].forEach(s=>{const t=this.form.querySelector(`[name="${s}"]`);t&&e[s]!==void 0&&(t.type==="checkbox"?t.checked=!!e[s]:t.value=e[s]||"")}),e.custom_fields&&Object.keys(e.custom_fields).forEach(s=>{const t=this.form.querySelector(`[name="custom_fields[${s}]"]`);if(t){const o=e.custom_fields[s];t.type==="checkbox"?t.checked=!!o:t.value=o||""}})}showForm(){this.formContainer.style.display="block"}hideForm(){this.formContainer.style.display="none",this.addressSelector.value=""}showError(e){window.showError(e)}}document.addEventListener("DOMContentLoaded",function(){window.billingAddressManager=new d});
