(function () {
    window.intgrtnI18n = {};
    window.intgrtnI18n.getLanguage = function (language) {
        switch (language) {
            case "mx":
            case "cl":
            case "co":
            case "ar":
            case "uy":
            case "cr":
            case "ec":
            case "pa":
            case "pe":
            case "ve":
                language = "es";
                break;
            case "be":
                language = "fr";
                break;
            case "at":
            case "ch":
                language = "de";
                break;
            case "oa":
            case "kw":
            case "qa":
            case "bh":
            case "sa":
                language = "ae";
                break;
        }

        return language;
    };

    window.intgrtnI18n.showDataTags = function () {
        $(".js-intgrtn-i18n-styles").remove();
    };

    window.intgrtnI18n.translateAll = function (language) {
        window.intgrtn.store('language', language);
        language = window.intgrtnI18n.getLanguage(language);
        window.intgrtnI18n.appendMedia(language);

        window.intgrtnI18n.getTranslations(language, function (response) {
            window.intgrtnI18n.translateDataTags(response);
            window.intgrtnI18n.translateIntgrtnOptions(response);
            window.intgrtnI18n.translatePasswordErrors(language)
            window.intgrtnI18n.showDataTags();
        }, function () {
            window.intgrtnI18n.showDataTags();
        });
    };

    window.intgrtnI18n.getTranslations = function (language, onSuccess, onError) {
        if (typeof onSuccess == "undefined") {
            onSuccess = function () { };
        }
        if (typeof onError == "undefined") {
            onError = function () { };
        }
        if (!window.availableLanguages) {
            console.error("Set window.availableLanguages");
        }
        if (window.availableLanguages && window.availableLanguages.includes(language + '.json')) {
            $.ajax({
                url: 'i18n/' + language + '.json',
                method: 'GET'
            }).done(function (response) {
                onSuccess(response);
            }).fail(function (response) {
                onError(response);
            });
        } else {
            onError({});
        }
    };

    window.intgrtnI18n.appendMedia = function (language) {
        $('.video').append($('<track kind="subtitles" src="subtitles/sub-' + language + '.vtt" default>'));
    };

    window.intgrtnI18n.translateDataTags = function (response) {
        $("[data-i18n]").each(function () {
            var t = $(this).attr("data-i18n");
            switch ($(this).prop("tagName")) {
                case "INPUT":
                    t || (t = $(this).attr("placeholder"));
                    $(this).attr("placeholder", response[t]);
                    break;
                default:
                    t || (t = $(this).text()), $(this).text(response[t])
            }
        });
    }

    window.intgrtnI18n.translateIntgrtnOptions = function (response) {
        window.intgrtn.setOptions({
            forms: {
                signup: {
                    placeholders: {
                        firstName: response.hasOwnProperty('First Name') ? response['First Name'] : "Enter your First Name",
                        lastName: response.hasOwnProperty('Last Name') ? response['Last Name'] : "Enter your Last Name",
                        email: response.hasOwnProperty('Email') ? response['Email'] : "Enter your Email Address",
                        password: response.hasOwnProperty('Password') ? response['Password'] : "Password",
                        areaCode: response.hasOwnProperty('Area-Code') ? response['Area-Code'] : "Area Code"
                    },
                    buttons: {
                        goToStep2: {
                            text: response.hasOwnProperty('NEXT') ? response['NEXT'] : "NEXT"
                        },
                        goToStep3: {
                            text: response.hasOwnProperty('NEXT') ? response['NEXT'] : "NEXT"
                        },
                        submit: {
                            text: response.hasOwnProperty('GET STARTED NOW') ? response['GET STARTED NOW'] : "Get started now"
                        },
                    },
                    fields: {
                        firstName: {
                            errors: {
                                minLength: response.hasOwnProperty('first-name-validation-error') ? response['first-name-validation-error'] : "First name should be at least 2 characters",
                            }
                        },
                        lastName: {
                            errors: {
                                minLength: response.hasOwnProperty('last-name-validation-error') ? response['last-name-validation-error'] : "Last name should be at least 2 characters",
                            }
                        },
                        email: {
                            errors: {
                                pattern: response.hasOwnProperty('email-validation-error') ? response['email-validation-error'] : "Email is invalid",
                            }
                        },
                        phone: {
                            errors: {
                                onlyDigits: response.hasOwnProperty('phone-validation-error') ? response['phone-validation-error'] : "Phone number should contain only digits",
                            }
                        },
                        passwordConfirm: {
                            required: false,
                        },
                    }
                },
            }
        }, true);
    }

    window.intgrtnI18n.translatePasswordErrors = function (language) {
        window.intgrtn.store('language', language);
        language = window.intgrtnI18n.getLanguage(language);
        if (window.availableLanguages.includes(language + '.json')) {
            $.ajax({
                url: 'i18n/password-translation.json',
                method: 'GET'
            }).done(function (response) {
                var resultObject = response.find(function (item) {
                    return item.locale == language;
                });

                if (typeof resultObject !== 'undefined') {
                    var passwordTranslationsArray = resultObject.string.split('. ');
                    var passwordTranslation = {
                        'Password should be {{passwordMinLength}}-{{passwordMaxLength}} characters long.': passwordTranslationsArray[0],
                        'Password should contain at least 1 number.': passwordTranslationsArray[1],
                        'Password should contain at least 1 letter.': passwordTranslationsArray[2],
                        'Only alphanumeric characters are allowed.': passwordTranslationsArray[3],
                        'Password and confirm password does not match.': passwordTranslationsArray[4],
                        'New!': passwordTranslationsArray[4],
                    };
                    applyTranslationsPasswordBullets(passwordTranslation);
                }
            });
        }
    }

    function applyTranslationsPasswordBullets(currentTranslation) {
        window.intgrtn.setOptions({
            forms: {
                signup: {
                    fields: {
                        password: {
                            verboseErrors: {
                                messages: [
                                    {
                                        id: 'passwordRegExpNoSpecialChars',
                                        text: currentTranslation.hasOwnProperty('Only alphanumeric characters are allowed.') ? currentTranslation['Only alphanumeric characters are allowed.'] : "Only alphanumeric characters are allowed.",
                                    },
                                    {
                                        id: 'password6To12Chars',
                                        text: currentTranslation.hasOwnProperty('Password should be {{passwordMinLength}}-{{passwordMaxLength}} characters long.') ? currentTranslation['Password should be {{passwordMinLength}}-{{passwordMaxLength}} characters long.'] : "Password should be {{passwordMinLength}}-{{passwordMaxLength}} characters long.",
                                    },
                                    {
                                        id: 'passwordRegExpNumber',
                                        text: currentTranslation.hasOwnProperty('Password should contain at least 1 number.') ? currentTranslation['Password should contain at least 1 number.'] : "Password should contain at least 1 number.",
                                    },
                                    {
                                        id: 'passwordRegExpLetter',
                                        text: currentTranslation.hasOwnProperty('Password should contain at least 1 letter.') ? currentTranslation['Password should contain at least 1 letter.'] : "Password should contain at least 1 letter.",
                                    },
                                ],
                            },
                        },
                    },
                },
            }
        }, true);
    }

    setTimeout(function () {
        window.intgrtnI18n.showDataTags();
    }, 3000);


    if($(".js-intgrtn-i18n-styles").length == 0) {
        console.error("Missing .js-intgrtn-i18n-styles tag");
    }
}());