//** Shared **//

  // VALIDATORS
    function sharedSectionValidator(section_id) {
        const $section = $(`#${section_id}`);

        let is_valid = true;
        let section_error_message = "";
        let first_invalid = null;

        // Check not disabled inputs
        $section.find('input, select, textarea')
        .filter(':not(:disabled):not([type="hidden"])')
        .each(function () {
            const el = this;

            // Limpia estados previos
            el.setCustomValidity && el.setCustomValidity('');
            $(el).removeClass('is-invalid');

            // Pequeño trim en campos de texto/email/tel para evitar espacios accidentales
            if ($(el).is('input[type="text"], input[type="email"], input[type="tel"]'))
                el.value = el.value.trim();

            // Usa validación nativa HTML5
            if (!el.checkValidity()) {
                is_valid = false;
                first_invalid = el;

                if (el.validity.patternMismatch && el.title)
                    section_error_message = el.title;
                else
                    section_error_message = el.validationMessage;

                $(el).addClass('is-invalid');
                return false;
            }
        });

        // Report Errors
        if (!is_valid && first_invalid) {
            const $collapse = $(first_invalid).closest('.collapse');

            if ($collapse.length && !$collapse.hasClass('show')) {
                $collapse.collapse('show');

                $collapse.one('shown.bs.collapse', function () {
                    first_invalid.scrollIntoView({ behavior: 'smooth', block: 'center' });
                    first_invalid.focus({ preventScroll: true });
                    first_invalid.reportValidity();
                });

                return { section_valid: is_valid, show_error_detected: true, section_error_message };
            }

            first_invalid.scrollIntoView({ behavior: 'smooth', block: 'center' });
            first_invalid.focus({ preventScroll: true });
            first_invalid.reportValidity();
        }

        // Return validations
        return {section_valid: is_valid, show_error_detected: true, section_error_message};
    }

  // PARSERS
    function parseFormData(brute_data) {
        const result = {};

        brute_data.forEach(({ name, value }) => {
            const cleanName = name.replace(/\[\]$/, ""); // Remove "[]" from input groups

            // Si ya existe y es un array → agregamos
            if (result.hasOwnProperty(cleanName)) {
                if (Array.isArray(result[cleanName])) {
                    result[cleanName].push(value);
                } else {
                    // Convertir valor previo a array
                    result[cleanName] = [result[cleanName], value];
                }
            } else {
                // Si el nombre termina con [] => crear como array
                if (name.endsWith("[]")) {
                    result[cleanName] = [value];
                } else {
                    result[cleanName] = value;
                }
            }
        });

        return result;
    }

  // FIXERS
    function normalizeAmount(value, decimals = 4){
        const amount = Number(value);

        return Number.isFinite(amount)
            ? Number(amount.toFixed(decimals))
            : 0;
    }

    function toCents(value) {
        return Math.round( normalizeAmount(value, 2) * 100);
    }

    function fromCents(cents){
        return normalizeAmount(cents/100, 2);
    }

//** Single Payments **//

//** Multiple Payments **//
