define([], function() {
    return {

        /**
         * Splits text with the specified separators.
         * Default separators are "\' \t=,\/?:!\*-\+\|
         * @param {string} text
         * @param {array} separators 
         * @returns {array} Returns an array of the splitted text.
         */
        SplitExceptInsideBrackets : function(text, separators){
            if(!separators){separators = "[\' \t=,\/?:!\*-\+\|]";}
            var pattern = new RegExp(separators + "(?![^[]*\])");
            return text.split(pattern);
        },

        /**
         * Gets the index of the first values found among the searchedValues.
         * @param {string} text Input text.
         * @param {array} searchedValues Values to search for.
         * @param {boolean} backwardSearch Specifies whether to search the values from the end or the beginning of the text.
         * @returns {integer} The position of the values in the string. Returns -1 if the values is not found. 
         */
        IndexOf : function(text, values, backwardSearch){
            if (!text) return "";
            var r = 0;
            if(backwardSearch === true){
                r = text.length - 1;
                while(r > 0 && (text[r] !== values)){
                    r -=1;
                }
                if(r === 0){
                    r = -1;
                }
            }else{
                while(r < text.length - 1 && (text[r] !== values)){
                    r +=1;
                }
                if(r === text.length-1){
                    r = -1;
                }
            }
            return r;
        },

        /**
         * Removes characters before the first value found among the specified values.
         * @param {string} text Input text.
         * @param {array} values Values to search for.
         * @param {boolean} backwardSearch Specifies whether to search the values from the end or the beginning of the text.
         * @param {boolean} ignoreInsideBrackets Specifies whether the characters inside brackets are taken into account.
         * @returns {string} The modified text. 
         */
        RemoveBefore : function(text, values, backwardSearch, ignoreInsideBrackets){
            if(ignoreInsideBrackets === true){
                var splittedText = this.SplitExceptInsideBrackets(text, values);
                return splittedText[splittedText.length -1];
            }else{
            var indexOfValue = this.IndexOf(text, values, backwardSearch);
                return text.substring(0, indexOfValue);
            }
        },

        /**
         * Removes characters after the first value found among the specified values.
         * @param {string} text Input text.
         * @param {string} values Values to search for.
         * @param {boolean} backwardSearch Specifies whether to search the values from the end or the beginning of the text.
         * @param {boolean} ignoreInsideBrackets Specifies whether the characters inside brackets are taken into account.
         * @returns {string} The modified text.
         */
        RemoveAfter : function(text, values, backwardSearch, ignoreInsideBrackets){
            if(ignoreInsideBrackets === true){
                var splittedText = this.SplitExceptInsideBrackets(text, values);
                return splittedText[0];
            }else{
                var indexOfValue = this.IndexOf(text, values, backwardSearch);
                return text.substring(indexOfValue);
            }
        },

        /**
         * Determines wether the text has unclosed quotation marks.
         * @param {string} text
         * @returns {boolean} 
         */
        IsInsideQuote : function(text){
            var r = text.length - 1;
            var singleQuoteNumber = 0;
            var doubleQuoteNumber = 0;
            while(r >= 0){
                if(text[r] === "'" && text[r-1] != "\\"){
                    singleQuoteNumber += 1;
                }
                if(text[r] === '"' && text[r-1] != "\\"){
                    doubleQuoteNumber += 1;
                }
                r -=1;
            }
            return !(singleQuoteNumber % 2 === 0 && doubleQuoteNumber % 2 === 0);
        }

    };
});
