﻿if (!Cltr) var Cltr = {};
if (!Cltr.A4) Cltr.A4 = {};
if (!Cltr.A4.Data) Cltr.A4.Data = {};

var ValidationDictionary;

(function () {
    ValidationDictionary = Cltr.A4.Data.ValidationDictionary = {
        /**
         * PROPERTIES
         */
        AUTHENTICATION_ERROR: "AuthenticationError.messageInvalidToken",
        TYPE_ERROR: "ERROR",
        TYPE_VALIDATION: "VALIDATION",
        TYPE_WARNING: "WARNING",
        TYPE_FATAL: "FATAL",
        /**
         * METHODS
         */
        Create: function(keyProperty, typeProperty, messageProperty){
            /// <summary>
            /// Creates a new ValidationDictionary based on a supplied properties
            /// for the supplied key and type
            /// </summary>
            return {ErrorCount:1, ErrorList:[{key:keyProperty, type:typeProperty, message:messageProperty}]};
        },
        ErrorListFromString: function(value){
            /// <summary>
            /// Takes the string representation of ValidationDictionary's ErrorList property
            /// and converts it into an array for easy consumption via javascript.
            /// </summary>
            /// <param name="value">String - ErrorList property</param>
            /// <returns>Array of error objects. If none are found, 'null' will be returned</returns>
            result = null;
            if (value instanceof Array){ return value; } //catch if it is already transformed!
            if (value != "" && value !== null && value !== undefined) {
                result = [];
                var tempErrors = value.split("[ErrorType.");
                var tempErrorCount = tempErrors.length;
                for (var x = 0; x < tempErrorCount; x++) {
                    var pair = tempErrors[x];
                    if (pair.indexOf("]") != -1) {
                        var typePair = pair.split("]", 2);
                        var errorType = typePair[0];
                        var errorProperty = String(typePair[1]).substr(0, String(typePair[1]).indexOf(":"));
                        var errorMessage = String(typePair[1]).substr(String(typePair[1]).indexOf(":") + 1);
                        result.push({key: errorProperty, type: errorType, message: errorMessage});
                    }
                }
            }
            return result;
        },
        ListErrorMessagesAsString: function(validationDictionary, delimiter){
            /// <summary>
            /// Takes a validation dictionary and builds a string with the supplied
            /// delimiter for easy output of error messages only
            /// </summary>
            if (validationDictionary !== null  && validationDictionary !== undefined){
                var errorList = this.ErrorListFromString(validationDictionary.ErrorList);
                if (errorList !== null && errorList !== undefined){
                    var messages = [];
                    for (var o = 0; o < errorList.length; o++){
                        var currentMessage = errorList[o].message;
                        if (currentMessage == this.AUTHENTICATION_ERROR){
                            currentMessage = "Your session has expired. Please re-authenticate.";
                        }
                        messages.push(currentMessage);
                    }
                    return messages.join(delimiter);
                }
            }
            return "";
        },
        GetInstanceFromJSONResponse: function(response){
            /// <summary>
            /// Iterates over the top-level object and 1-level deep
            /// for the supplied response's validation dictionary instance
            /// </summary>
            /// <param name="response">Object to search for the validation dictionary</param>
            /// <returns>Found validation dictionary. If not found, returns undefined</returns>
            if (response["ErrorCount"] !== undefined && response["ErrorList"] !== undefined){
                return response;
            }
            for (var o in response){
                if (response[o]["ErrorCount"] !== undefined && response[o]["ErrorList"] !== undefined){
                    return response[o];
                }
            }
            return undefined;
        }

    }
})();
