/**
 * UNITY Eleitoral - Máscaras Automáticas de Entrada
 * Formata CPF (000.000.000-00), Telefone ((00)00000-0000 ou (00)0000-0000) e Data de Nascimento (00/00/0000)
 */

function formatCpf(val) {
    let digits = val.replace(/\D/g, '').slice(0, 11);
    if (digits.length > 9) {
        return digits.replace(/(\d{3})(\d{3})(\d{3})(\d{1,2})/, '$1.$2.$3-$4');
    } else if (digits.length > 6) {
        return digits.replace(/(\d{3})(\d{3})(\d{1,3})/, '$1.$2.$3');
    } else if (digits.length > 3) {
        return digits.replace(/(\d{3})(\d{1,3})/, '$1.$2');
    }
    return digits;
}

function formatPhone(val) {
    let digits = val.replace(/\D/g, '').slice(0, 11);
    if (digits.length > 10) {
        return digits.replace(/(\d{2})(\d{5})(\d{4})/, '($1)$2-$3');
    } else if (digits.length > 6) {
        return digits.replace(/(\d{2})(\d{4})(\d{1,4})/, '($1)$2-$3');
    } else if (digits.length > 2) {
        return digits.replace(/(\d{2})(\d{1,5})/, '($1)$2');
    } else if (digits.length > 0) {
        return '(' + digits;
    }
    return digits;
}

function formatDate(val) {
    let digits = val.replace(/\D/g, '').slice(0, 8);
    if (digits.length > 4) {
        return digits.replace(/(\d{2})(\d{2})(\d{1,4})/, '$1/$2/$3');
    } else if (digits.length > 2) {
        return digits.replace(/(\d{2})(\d{1,2})/, '$1/$2');
    }
    return digits;
}

function formatLoginInput(val) {
    if (val.includes('@')) return val;
    if (/[a-zA-Z]/.test(val)) return val;
    let digits = val.replace(/\D/g, '');
    if (digits.length > 0) {
        return formatCpf(digits);
    }
    return val;
}

function initMasks() {
    // 1. CPF (000.000.000-00)
    const cpfInputs = document.querySelectorAll('input[name="cpf"], input[id*="cpf"], .mask-cpf');
    cpfInputs.forEach(input => {
        input.setAttribute('maxlength', '14');
        input.setAttribute('placeholder', '000.000.000-00');
        input.addEventListener('input', (e) => {
            e.target.value = formatCpf(e.target.value);
        });
        if (input.value) {
            input.value = formatCpf(input.value);
        }
    });

    // 2. Campo Login (CPF ou E-mail)
    const loginInputs = document.querySelectorAll('input[name="login_input"]');
    loginInputs.forEach(input => {
        input.addEventListener('input', (e) => {
            e.target.value = formatLoginInput(e.target.value);
        });
    });

    // 3. Telefone / WhatsApp ((00)00000-0000 ou (00)0000-0000)
    const phoneInputs = document.querySelectorAll('input[name="telefone"], input[id*="telefone"], .mask-phone');
    phoneInputs.forEach(input => {
        input.setAttribute('maxlength', '14');
        input.setAttribute('placeholder', '(92)99999-9999');
        input.addEventListener('input', (e) => {
            e.target.value = formatPhone(e.target.value);
        });
        if (input.value) {
            input.value = formatPhone(input.value);
        }
    });

    // 4. Data de Nascimento (00/00/0000)
    const dateInputs = document.querySelectorAll('input[name="data_nascimento"], input[id*="nascimento"], .mask-date');
    dateInputs.forEach(input => {
        input.setAttribute('type', 'text');
        input.setAttribute('maxlength', '10');
        input.setAttribute('placeholder', '00/00/0000');
        input.addEventListener('input', (e) => {
            e.target.value = formatDate(e.target.value);
        });
        if (input.value) {
            input.value = formatDate(input.value);
        }
    });
}

document.addEventListener('DOMContentLoaded', initMasks);
