'use strict';

var intlNameInitials = function () {
};
var pattern = '{0}{1}';

function _firstLetter(text) {
    return text.charAt(0);
}

function _upperCase(letter) {

    if (letter === 'ı'){
        return 'I';
    }

    return letter.toUpperCase();
}

function _isHangul(l){

    if ((l > 44032) && (l < 55203)) {
        return true;
    }
    return false;

}

function _initials(letter) {
    var l = letter.charCodeAt(0);
    // Generated by regenerate and unicode-8.0.0
    // Greek 117
    // Latin 991
    // Cyrillic 302
    var alphaRegex = '[A-Za-z\xAA\xBA\xC0-\xD6\xD8-\xF6\xF8-\u02B8\u02E0-\u02E4\u0370-\u0373\u0375-\u0377\u037A-\u037D\u037F\u0384\u0386\u0388-\u038A\u038C\u038E-\u03A1\u03A3-\u03E1\u03F0-\u0484\u0487-\u052F\u1D00-\u1D25\u1D2C-\u1D5C\u1D62-\u1D65\u1D6B-\u1D77\u1D79-\u1DBE\u1E00-\u1EFE]';
    var re = new RegExp(alphaRegex,'i');

    if (re.test(letter)){
        return letter;
    }

    return '';
}

function _isSupportedInitials(letter) {

    var alphaRegex = '[A-Za-z\xAA\xBA\xC0-\xD6\xD8-\xF6\xF8-\u02B8\u02E0-\u02E4\u0370-\u0373\u0375-\u0377\u037A-\u037D\u037F\u0384\u0386\u0388-\u038A\u038C\u038E-\u03A1\u03A3-\u03E1\u03F0-\u0484\u0487-\u052F\u1D00-\u1D25\u1D2C-\u1D5C\u1D62-\u1D65\u1D6B-\u1D77\u1D79-\u1DBE\u1E00-\u1EFE]';
    var re = new RegExp(alphaRegex,'i');

    if (re.test(letter)){
        return true;
    }
    return false;
}


function isThai(a){

    var thaiRegex = '[\u0E01-\u0E3A\u0E40-\u0E5B]';
    var re = new RegExp(thaiRegex,'i');

    if (a.length === 1){
        return true;
    } else {
        var letter = _firstLetter(a);
        if (re.test(a)) {
            return true;
        }
    }
    return false;

}

function isCJK(a){

    // HANGUL SYLLABLES
    // We want to be sure the full name is Hangul
    if (a.length < 3){
        var i = 0;
        for(var c=0;c< a.length;c++){
            if (_isHangul(a.charCodeAt(c)) )
            {
                i++;
            }
        }
        if (i === a.length){
            return true;
        }
    }
    return false;
}

intlNameInitials.prototype.format = function (name, options) {

    var initials = '',
        a = '',
        b = '';
    var fields = ['firstName', 'lastName'],
        initialName = { firstName : '', lastName: '' };

    if (name === null || typeof name !== 'object' ) {
        return undefined;
    }

    fields.forEach(function(field){
        if (name.hasOwnProperty(field)) {
            if (name[field] === null || name[field].length === 0){
                // Nothing to do. but keeping it as placeholder
            } else {
                if (_isSupportedInitials(_firstLetter(name[field]))) {
                    initialName[field] = _firstLetter(name[field]);
                    initials = initials + _upperCase(_initials(initialName[field]));
                }
            }
        }

    });


    // for CJK
    if (name.hasOwnProperty("lastName")){

        if (name.lastName === null || name.lastName.length === 0){

        } else {
            if (isCJK(name.lastName)) {
                initials = name.lastName;
            }
        }
    }
    if (initials.length === 0){
        return undefined;
    }
    return initials;

};

module.exports = intlNameInitials;
