//* Auto Fill & Behaviors (Fields Interactions)
  // Fill Address
    async function getTaxAddressByZipAndFillFields({
        zip,
        municipality_field,
        city_field,
        state_field,
        neighborhood_field
    }) {
      // Control Data
        const fields = `${municipality_field}, ${city_field}, ${state_field}`;
        const neighborhood_select = $(neighborhood_field);

      // Abort if Zip Code is invalid
        const is_foreign_receiver = (zip === '00000');
        const is_valid_zip = /^\d{5}$/.test(zip);

        if(!is_valid_zip || is_foreign_receiver) {
          // Reset fields
            $(fields).val('');
            neighborhood_select.empty();
            neighborhood_select.selectpicker('refresh');

            return null;
        }

      // Disable fields while query is requested
        $(fields)
            .val('')
            .attr('placeholder', translateAndReplace("common_responses_and_messages.loading"))
            .prop('readonly', true).toggleClass('disabled-fields');

        neighborhood_select.prop('disabled', true).selectpicker('refresh');

      // Request Data
        try{
          // Request to Postalia
            const response = await getDynamicTaxAddress(zip);
            neighborhood_select.empty();

            if(response) {
              // Insert returned data
                const addresses = response.addresses;
                    $(municipality_field).val(addresses.municipio);
                    $(city_field).val(addresses.ciudad);
                    $(state_field).val(addresses.estado);

                    addresses.colonias.forEach(colony => {
                        neighborhood_select.append(
                            `<option value="${colony.nombre}">
                                ${colony.nombre}
                            </option>`
                        );
                    });

              // Update colony selectpicker & Select first option
                    neighborhood_select.selectpicker('refresh');

                    if(addresses.colonias.length > 0){
                        neighborhood_select.selectpicker(
                            'val', addresses.colonias[0].nombre
                        );
                    }
            }

            return response
        } catch(error){
          // Clean Fields on error
            $(fields).val('');

            neighborhood_select.empty();
            neighborhood_select.selectpicker('refresh');

            throw error;
        } finally {
          // Re-enable fields after query
            $(fields).prop('readonly', false).removeClass('disabled-fields');
            $(municipality_field).attr('placeholder', translateAndReplace('entity_fields.common.tax_address.municipality') + '...');
            $(city_field).attr('placeholder', translateAndReplace('entity_fields.common.tax_address.city') + '...');
            $(state_field).attr('placeholder', translateAndReplace('entity_fields.common.tax_address.state') + '...');

            neighborhood_select.prop('disabled', false).selectpicker('refresh');
        }
    }

  // Fill Colony Select
    async function loadNeighborhoodsFromPostaliaOrFail(zip_request = null, neighborhood_selected = '', neighborhood_field) {
      // Validate Zip Code
        const is_foreign_receiver = zip_request === '00000';
        const is_valid_zip = /^\d{5}$/.test(zip_request);
            if(!zip_request || !is_valid_zip || is_foreign_receiver) return null;

      // Request Data
        try{
          // Control Data
            let addresses = null;
            let neighborhood_existing = null;

          // Request to Postalia
            const response = await getDynamicTaxAddress(zip_request);

            neighborhood_field.empty(); // Limpiamos el select
                if(!neighborhood_selected) return false; // Sin colonia registrada

            if(!response || !response.addresses) throw new Error(translateAndReplace('apis.common_responses_and_messages.service_unavailable', {service: 'Postalia'}));

            // Ensure Colony exists on Postalia response
            addresses = response.addresses;
            neighborhood_existing = addresses.colonias.find(
                colony => colony.nombre === neighborhood_selected
            );

            if(neighborhood_existing) {
              // Add colonies as options
                addresses.colonias.forEach(colony => {
                    neighborhood_field.append(
                        `<option value="${colony.nombre}">
                            ${colony.nombre}
                        </option>`
                    );
                });

              // Update selectpicker & Select client colony
                neighborhood_field.selectpicker('refresh');
                neighborhood_field.selectpicker(
                    'val', neighborhood_existing.nombre
                );
            } else {
                // Use original info as option
                throw new Error(translateAndReplace('apis.postalia.errors.neighborhood_listed_not_exist'));
            }

            return response;
        } catch(error) {
          // Add unique colony as options
            neighborhood_field.append(
                `<option value="${neighborhood_selected}">
                    ${neighborhood_selected}
                </option>`
            );

          // Update selectpicker & Select client colony
            neighborhood_field.selectpicker('refresh');
            neighborhood_field.selectpicker(
                'val', neighborhood_selected
            );

            throw error;
        }
    }
