/*
 * Licensed Materials - Property of IBM
 *
 * IBM Cognos Products: SHARE
 *
 * (C) Copyright IBM Corp. 2016, 2018
 *
 * US Government Users Restricted Rights - Use, duplication or disclosure
 * restricted by GSA ADP Schedule Contract with IBM Corp.
 */
define(['jquery'], function($) {
    'use strict';

    var _FORMATTER = '...';
    var _DEFAULT_BEGIN_MAX_LENGTH = 5;
    var _DEFAULT_END_MAX_LENGTH = 15;
    var textFormatter = {};
    /**
     * middleShortenString is a function that shortens a given String from the middle.
     * @param  {String} stringToShorten - The String to shorten
     * @param  {int} [beginMaxLength=_DEFAULT_BEGIN_MAX_LENGTH=5] - (Optional) The position where to start the extraction.
     * @param  {int} [endMaxLength=_DEFAULT_END_MAX_LENGTH=15] - (Optional) The position where to end the extraction.
     * @return {String} stringShorten - The shortened String
     */
    textFormatter.middleShortenString = function(stringToShorten, beginMaxLength, endMaxLength) {
        var stringShorten = stringToShorten;
        var beginMaxLength = (typeof beginMaxLength !== 'number') ? _DEFAULT_BEGIN_MAX_LENGTH : beginMaxLength;
        var endMaxLength = (typeof endMaxLength !== 'number') ? _DEFAULT_END_MAX_LENGTH : endMaxLength;
        if (stringToShorten.length > (beginMaxLength + endMaxLength)) {
            stringShorten = stringToShorten.substring(0, beginMaxLength) + _FORMATTER + stringToShorten.substring(stringToShorten.length - endMaxLength, stringToShorten.length);
        }
        return stringShorten;
    };

    /**
     * Transforms a string so that special characters are escaped with a leading '\'
     * @param {String} rawString - the unescaped raw String to be converted
     * @param {String[]} [specialCharactersArray] - an array of characters to escape.  If this parameter
     * is omitted, the default ['\' , '/' ] is used.
     */
    textFormatter.escapeSpecialCharacters = function (rawString, specialCharactersArray) {

        var charArray = (specialCharactersArray) ? specialCharactersArray : [ '\\' , '/' ];
        var escapedString = '';
    
        for (var i = 0; i < rawString.length; i++) {
    
            var isSpecialCharacter = false;
          
            for (var j = 0; j < charArray.length; j++) {
    
                if (rawString.charAt(i) === charArray[j]) {
                    escapedString += "\\";
                    break;
                }
    
            }
    
            escapedString += rawString.charAt(i);
    
        }
    
        return escapedString;
    
    };
    return textFormatter;
});