ACC.achBillingAddress = {
    _autoload: [
        ["renderAchBillingAddressSelector", $('#achBillingAddressSelector').length !== 0]
    ],

    renderAchBillingAddressSelector: function() {
        document.getElementById("achNewBillingAddressForm").style.display = 'none';
        let ownershipType = document.getElementById('ownership-type');
        let saveAchBillingAddress = document.querySelector("#saveAddressAchComponent");
        
        if(saveAchBillingAddress !== null){
            saveAchBillingAddress.style.display = 'none';
        }

        let achCurrentSelectedOption = document.getElementById("achBillingAddressSelector")?.selectedOptions[0];
       
        if (achCurrentSelectedOption?.value === "newAddress") {
            document.querySelector("#addressFormSelectorContainer").style.display = 'block';
        }

        if (achCurrentSelectedOption?.value !== "newAddress" && achCurrentSelectedOption?.value !== "noAddress") {
            document.getElementById("achNewBillingAddressForm").style.display = 'block'
        }

        document.getElementById('achBillingAddressSelector').addEventListener('change', function (event) {
            if (event.target.value === 'newAddress') {
                document.querySelector("#achNewBillingAddressForm").style.display = 'block';
                document.getElementById("selected-address").innerHTML = "";
                if (saveAchBillingAddress !== null && ownershipType.value !== 'business') {
                    saveAchBillingAddress.style.display = 'block';
                }
            } else {
                document.querySelector("#achNewBillingAddressForm").style.display = 'none';
                if(saveAchBillingAddress !== null){
                    saveAchBillingAddress.style.display = 'none';
                }
            }
        })

        ownershipType.addEventListener('change', function (event) {
            if (event.target.value !== 'business') {
                saveAchBillingAddress.style.display = 'block';
            } else {
                saveAchBillingAddress.style.display = 'none';
            }
            document.getElementById("shouldSaveAddressAchMyAccount").checked = false;
        });

    }
}
