class CountryDocumentsCheck {
    constructor() {
        this.masks = {}; // Store mask instances
        this.cpfMask();
        this.cnpjMask();
        this.dniArMask();
        this.ineMask();
        this.ccMask();
        this.ciMask();
        this.dniPeMask();
        this.ciUyMask();
        this.curpMask();
        this.rfcMask();
        this.rutMask();
        this.ceCOMask();
        this.cePEMask();
        this.removeFormatOnSubmit();
    }

    removeFormatOnSubmit() {
        jQuery('form.checkout').on('checkout_place_order', () => {
            Object.keys(this.masks).forEach(fieldId => {
                const input = document.getElementById(fieldId);
                if (input && input.value) {
                    input.value = input.value.replace(/[^a-zA-Z0-9]/g, '');
                }
            });
            return true;
        });
    }

    cpfMask() {
        let rgInput = document.getElementById('wc_koin_cpf');
        if (rgInput) {
            this.masks['wc_koin_cpf'] = IMask(rgInput, {
                mask: '000.000.000-00'
            });
        }
    }

    cnpjMask() {
        let cnpjInput = document.getElementById('wc_koin_cnpj');
        if (cnpjInput) {
            this.masks['wc_koin_cnpj'] = IMask(cnpjInput, {
                mask: '00.000.000/0000-00'
            });
        }
    }

    dniArMask() {
        let dniArInput = document.getElementById('wc_koin_dni_ar');
        if (dniArInput) {
            this.masks['wc_koin_dni_ar'] = IMask(dniArInput, {
                mask: '00.000.000'
            });
        }
    }

    ineMask() {
        let ineInput = document.getElementById('wc_koin_ci_mx');
        if (ineInput) {
            this.masks['wc_koin_ci_mx'] = IMask(ineInput, {
                mask: 'a00 000 000'
            });
        }
    }

    ccMask() {
        let ccInput = document.getElementById('wc_koin_cc');
        if (ccInput) {
            this.masks['wc_koin_cc'] = IMask(ccInput, {
                mask: '00.000.000'
            });
        }
    }

    ciMask() {
        let ciInput = document.getElementById('wc_koin_ci');
        if (ciInput) {
            this.masks['wc_koin_ci'] = IMask(ciInput, {
                mask: '00.000.000-0',
                definitions: {
                    '0': /[0-9Kk]/
                }
            });
        }
    }

    dniPeMask() {
        let dniPeInput = document.getElementById('wc_koin_dni_pe');
        if (dniPeInput) {
            this.masks['wc_koin_dni_pe'] = IMask(dniPeInput, {
                mask: '00000000'
            });
        }
    }

    ciUyMask() {
        let ciUyInput = document.getElementById('wc_koin_ci_uy');
        if (ciUyInput) {
            this.masks['wc_koin_ci_uy'] = IMask(ciUyInput, {
                mask: '0.000.000-0'
            });
        }
    }

    curpMask() {
        let curpInput = document.getElementById('wc_koin_curp');
        if (curpInput) {
            this.masks['wc_koin_curp'] = IMask(curpInput, {
                mask: 'aaaa000000aaaaaa00'
            });
        }
    }

    rfcMask() {
        let rfcInput = document.getElementById('wc_koin_rfc');
        if (rfcInput) {
            this.masks['wc_koin_rfc'] = IMask(rfcInput, {
                mask: 'aaa000000aaa'
            });
        }
    }

    rutMask() {
        let rutInput = document.getElementById('wc_koin_rut');
        if (rutInput) {
            this.masks['wc_koin_rut'] = IMask(rutInput, {
                mask: '00.000.000-0',
                definitions: {
                    '0': /[0-9Kk]/
                }
            });
        }
    }

    ceCOMask() {
        let ceCoInput = document.getElementById('wc_koin_ce_co');
        if (ceCoInput) {
            this.masks['wc_koin_ce_co'] = IMask(ceCoInput, {
                mask: '000000000'
            });
        }
    }

    cePEMask() {
        let cePeInput = document.getElementById('wc_koin_ce_pe');
        if (cePeInput) {
            this.masks['wc_koin_ce_pe'] = IMask(cePeInput, {
                mask: '000000000'
            });
        }
    }
}

document.addEventListener("DOMContentLoaded", () => {
    new CountryDocumentsCheck();
});
