
import { punycode } from '/js/punycodelib.js?v=3.6';
/*
const unicodeDnsNames = [
    'mañana.rocks', '☃-⌘.design', 'джpумлатест.bрфa', 'hello.codes',
    '0.0.0.0', '::1', 'بعض.شركات.اختبار.النطاق'
];

unicodeDnsNames.forEach(name => {
    console.info(
        `${name}
  encoded: ${punycode.toASCII(name)}
  decoded: ${punycode.toUnicode(punycode.toASCII(name))}\n`);
});
*/

const isValidIDNDomain = function (domain, expr) {
    let idnDomain = domain;

    if (domain.match(/@/gi)) {
        idnDomain = punycode.toASCII(domain.split('@')[0]) + '@' + punycode.toASCII(domain.split('@')[1]);
    }
    else
        idnDomain = punycode.toASCII(domain);
    //console.log('idnDomain.toASCII', idnDomain);
    //console.log('idnDomain.toUnicode', punycode.toUnicode(idnDomain));
    let validatorExpression = new RegExp(expr,"gi");
    return validatorExpression.test(idnDomain);
}
/*
let result = convertToASCIIAndValidate('بعض.شركات.اختبار.النطاق', '^(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z0-9][a-z0-9-]{0,61}[a-z0-9]$');
console.log('result', result);
result = convertToASCIIAndValidate('@sometestdomaincom', '^(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z0-9][a-z0-9-]{0,61}[a-z0-9]$');
console.log('result', result);
*/

/*.Net Equivalent for Internationalized Domain Names*/
/*
 * System.Globalization.IdnMapping idn = new System.Globalization.IdnMapping();
            string punyCode = idn.GetAscii(Name);
            Name = idn.GetUnicode(punyCode);
*/