<commit_before>'use strict';

String.prototype.replaceAt = function(startIndex, replaceCount, character) {
    return this.substr(0, startIndex) + character + this.substr(startIndex + replaceCount);
};

var ServiceModule = angular.module('ServiceModule', ['ngCookies', 'DashboardConstantsModule']);

ServiceModule.factory('Utils', function($cookieStore, $location) {
    function getRoutePath(location_path) {
        return location_path.split("/").reverse()[0];
    }

    function storeUserDataInCookies(data) {
        $cookieStore.put('authenticated', data.authenticated);
        $cookieStore.put('user_display_name', data.display_name);
        $cookieStore.put('user_email', data.email);
        $cookieStore.put('user_image_url', data.image_url);
        $cookieStore.put('team_key', data.team_key);
        $cookieStore.put('team_name', data.team_name);
        $cookieStore.put('team_hash', data.team_hash);
        $cookieStore.put('user_team_token', angular.fromJson(data.user_team_token));
        $cookieStore.put('role', data.role);
    }

    function removeUserDataCookies() {
        $cookieStore.put('authenticated', false);
        $cookieStore.remove('user_display_name');
        $cookieStore.remove('user_email');
        $cookieStore.remove('user_image_url');
        $cookieStore.remove('team_key');
        $cookieStore.remove('team_name');
        $cookieStore.remove('team_hash');
        $cookieStore.remove('user_team_token');
        $cookieStore.remove('role');
    }

    function removeRedirectURL() {
        $cookieStore.remove("redirect_to");
    }

    function replaceURLWithLink(s) {
        var commentURLTemplate = '$1<a class="url" href="$2" target="_blank">$2</a>';
        s = s.replace(/(^|\W)\b((www\d{0,3}[.])(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))/ig, "$1http://$2");
        return s.replace(/(^|\W)\b((?:https?:\/\/|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))/ig, commentURLTemplate);
    }

    function replaceUniqueUserNameWithID(s, engaged_users) {
        engaged_users = engaged_users || [];

        var taggedUniqueName = s.match(/(^|\W)(@[a-z\d][\w]*)/ig) || [],
            taggedUserIDs = [];

        angular.forEach(taggedUniqueName, function(name) {
            name = name.trim();
            var filteredUser = engaged_users.filter(function(user) {
                return user.unique_name === name.split("@")[1];
            });
            if (filteredUser.length) {
                var userID = filteredUser[0]["id"];
                s = s.replace(name, "__" + userID + "__");
                if (taggedUserIDs.indexOf(userID) === -1) {
                    taggedUserIDs.push(userID);
                }
            }
        });

        return [s, taggedUserIDs];
    }

    function replaceEmailWithName(s, engaged_users, editMode) {
        engaged_users = engaged_users || [];

        var matchedEmailList = s.match(/(^|\W)(__[a-z\d][\w]*)/ig) || [];
        angular.forEach(matchedEmailList, function(id) {
            id = id.trim();
            var filteredUser = engaged_users.filter(function(user) {
                return user.id === id.split("__")[1];
            });
            if (filteredUser.length) {
                if (editMode) {
                    var userUniqueName = filteredUser[0]["unique_name"];
                    s = s.replace(id, "@" + userUniqueName);
                } else {
                    var userDisplayName = filteredUser[0]["display_name"],
                        userEmail = filteredUser[0]["user_email"];
                    s = s.replace(id, "<span class='tagged-user' title='" + userEmail + "'>" + userDisplayName + "</span>");
                }
            }
        });

        return s;
    }

    function replaceHashTagWithLink(s) {
        var linkScript = '$1<span class="hashtag">$2</span>';
        return s.replace(/(^|\W)(#[a-z\d][\w-]*)/ig, linkScript);
    }

    function findAncestor(el, cls) {
        while ((el = el.parentElement) && !el.classList.contains(cls));
        return el;
    }

    function getUniqueEngagedUsers(anno, teamUsers, perAnnoEngagedUsers) {
        var uniqueNames = [], uniqueUserName, engagedUsers;

        if (perAnnoEngagedUsers) {
            engagedUsers = [];
            if (angular.isObject(anno)) {
                if (anno.hasOwnProperty('engaged_users')) {
                    engagedUsers = engagedUsers.concat(anno.engaged_users);
                }
                if (anno.hasOwnProperty('team_notes_metadata')) {
                    if (anno.team_notes_metadata.hasOwnProperty('mentions')) {
                        engagedUsers = engagedUsers.concat(anno.team_notes_metadata.mentions);
                    }
                }
            }
        } else {
            engagedUsers = anno;
        }

        function uniqueCount(userName) {
            return uniqueNames.indexOf(userName) + 1;
        }

        angular.forEach(engagedUsers, function(mentionedUser, index) {
            if ((mentionedUser["display_name"] === "") ||
                (teamUsers &&  teamUsers.length &&
                 teamUsers.some(function(user) { return user["user_email"] === mentionedUser["user_email"]; }) && perAnnoEngagedUsers)) {
                delete engagedUsers[index];
            } else  if (!mentionedUser.hasOwnProperty('unique_name')) {
                var uniqueUserName = mentionedUser["display_name"].replace(/ /g, ""),
                    uniqueUserNameCount = uniqueCount(uniqueUserName);

                if (uniqueUserNameCount > 0) {
                    uniqueUserName += uniqueUserNameCount;
                }

                mentionedUser["unique_name"] = uniqueUserName;
                uniqueNames.push(uniqueUserName);
            }
        });

        return engagedUsers.concat(teamUsers).filter(function(user) { return angular.isDefined(user); });
    }

    function getAnnoById(anno_list, anno_id) {
        var annoData = {};

        if (anno_list.length) {
            var annoDataList = anno_list.filter(function(anno) {
                return anno.id === anno_id;
            });

            if (annoDataList.length) {
                annoData = annoDataList[0];
            }
        }

        return annoData;
    };

    function watchersContainedIn(scope) {
        // from http://www.closedinterval.com/count-angularjs-watchers/
        var slice = [].slice;
        var elems = slice.call(document.querySelectorAll("*"));

        return elems.map(function(elem) {
            var data = angular.element(elem).data();
            return data.$scope || null;
        }).filter(function(scope) {
            return scope && scope.$$watchers;
        }).reduce(function(tmp, scope) {
            if (tmp.cache[scope.$id]) {
                return tmp;
            }
            tmp.cache[scope.$id] = true;
            tmp.count += scope.$$watchers.length;
            return tmp;
        }, {
            count : 0,
            cache : {}
        }).count;
    };

    function getUniqueData(data, key) {
        var newArr = [], found;

        angular.forEach(data, function(obj) {
            found = undefined;
            angular.forEach(newArr, function(newObj) {
                if (obj[key] === newObj[key] && !found) found = true;
            });
            if (!found) newArr.push(obj);
        });

        return newArr;
    };

    function getFullDashboardURL(community_name, team_hash) {
        if (community_name && team_hash) {
            var url = $location.protocol() + "://" + $location.host();
            if ($location.port() !== 443) {
                url = url + ":" + $location.port();
            }
            url = url + "/dashboard/" + team_hash + "/" + community_name.replace(/\W+/g, "-").toLowerCase();
            return url;
        } else {
            return "";
        }
    }

    return {
        getRoutePath : getRoutePath,
        storeUserDataInCookies : storeUserDataInCookies,
        removeUserDataCookies : removeUserDataCookies,
        removeRedirectURL : removeRedirectURL,
        replaceURLWithLink : replaceURLWithLink,
        replaceEmailWithName : replaceEmailWithName,
        replaceHashTagWithLink : replaceHashTagWithLink,
        replaceUniqueUserNameWithID : replaceUniqueUserNameWithID,
        findAncestor : findAncestor,
        getUniqueEngagedUsers : getUniqueEngagedUsers,
        getAnnoById : getAnnoById,
        watchersContainedIn : watchersContainedIn,
        getUniqueData : getUniqueData,
        getFullDashboardURL : getFullDashboardURL
    };
});

ServiceModule.factory('Autocomplete', function(Utils) {<commit_msg>[Dashboard] Refactor code<commit_after>'use strict';

String.prototype.replaceAt = function(startIndex, replaceCount, character) {
    return this.substr(0, startIndex) + character + this.substr(startIndex + replaceCount);
};

var ServiceModule = angular.module('ServiceModule', ['ngCookies', 'DashboardConstantsModule']);

ServiceModule.factory('Utils', function($cookieStore, $location) {
    function getRoutePath(location_path) {
        return location_path.split("/").reverse()[0];
    }

    function storeUserDataInCookies(data) {
        $cookieStore.put('authenticated', data.authenticated);
        $cookieStore.put('user_display_name', data.display_name);
        $cookieStore.put('user_email', data.email);
        $cookieStore.put('user_image_url', data.image_url);
        $cookieStore.put('team_key', data.team_key);
        $cookieStore.put('team_name', data.team_name);
        $cookieStore.put('team_hash', data.team_hash);
        $cookieStore.put('user_team_token', angular.fromJson(data.user_team_token));
        $cookieStore.put('role', data.role);
    }

    function removeUserDataCookies() {
        $cookieStore.put('authenticated', false);
        $cookieStore.remove('user_display_name');
        $cookieStore.remove('user_email');
        $cookieStore.remove('user_image_url');
        $cookieStore.remove('team_key');
        $cookieStore.remove('team_name');
        $cookieStore.remove('team_hash');
        $cookieStore.remove('user_team_token');
        $cookieStore.remove('role');
    }

    function removeRedirectURL() {
        $cookieStore.remove("redirect_to");
    }

    function replaceURLWithLink(s) {
        var commentURLTemplate = '$1<a class="url" href="$2" target="_blank">$2</a>';
        s = s.replace(/(^|\W)\b((www\d{0,3}[.])(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))/ig, "$1http://$2");
        return s.replace(/(^|\W)\b((?:https?:\/\/|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))/ig, commentURLTemplate);
    }

    function replaceUniqueUserNameWithID(s, engaged_users) {
        engaged_users = engaged_users || [];

        var taggedUniqueName = s.match(/(^|\W)(@[a-z\d][\w]*)/ig) || [],
            taggedUserIDs = [];

        angular.forEach(taggedUniqueName, function(name) {
            name = name.trim();
            var filteredUser = engaged_users.filter(function(user) {
                return user.unique_name === name.split("@")[1];
            });
            if (filteredUser.length) {
                var userID = filteredUser[0]["id"];
                s = s.replace(name, "__" + userID + "__");
                if (taggedUserIDs.indexOf(userID) === -1) {
                    taggedUserIDs.push(userID);
                }
            }
        });

        return [s, taggedUserIDs];
    }

    function replaceEmailWithName(s, engaged_users, editMode) {
        engaged_users = engaged_users || [];

        var matchedEmailList = s.match(/(^|\W)(__[a-z\d][\w]*)/ig) || [];
        angular.forEach(matchedEmailList, function(id) {
            id = id.trim();
            var filteredUser = engaged_users.filter(function(user) {
                return user.id === id.split("__")[1];
            });
            if (filteredUser.length) {
                if (editMode) {
                    var userUniqueName = filteredUser[0]["unique_name"];
                    s = s.replace(id, "@" + userUniqueName);
                } else {
                    var userDisplayName = filteredUser[0]["display_name"],
                        userEmail = filteredUser[0]["user_email"];
                    s = s.replace(id, "<span class='tagged-user' title='" + userEmail + "'>" + userDisplayName + "</span>");
                }
            }
        });

        return s;
    }

    function replaceHashTagWithLink(s) {
        var linkScript = '$1<span class="hashtag">$2</span>';
        return s.replace(/(^|\W)(#[a-z\d][\w-]*)/ig, linkScript);
    }

    function findAncestor(el, cls) {
        while ((el = el.parentElement) && !el.classList.contains(cls));
        return el;
    }

    function getUniqueEngagedUsers(anno, teamUsers, perAnnoEngagedUsers) {
        var uniqueNames = [], uniqueUserName, engagedUsers;

        if (perAnnoEngagedUsers) {
            engagedUsers = [];
            if (angular.isObject(anno)) {
                if (anno.hasOwnProperty('engaged_users')) {
                    engagedUsers = engagedUsers.concat(anno.engaged_users);
                }
                if (anno.hasOwnProperty('team_notes_metadata')) {
                    if (anno.team_notes_metadata.hasOwnProperty('mentions')) {
                        engagedUsers = engagedUsers.concat(anno.team_notes_metadata.mentions);
                    }
                }
            }
        } else {
            engagedUsers = anno;
        }

        function uniqueCount(userName) {
            return uniqueNames.indexOf(userName) + 1;
        }

        angular.forEach(engagedUsers, function(mentionedUser, index) {
            if ((mentionedUser["display_name"] === "") ||
                (teamUsers &&  teamUsers.length &&
                 teamUsers.some(function(user) { return user["user_email"] === mentionedUser["user_email"]; }) && perAnnoEngagedUsers)) {
                delete engagedUsers[index];
            } else  if (!mentionedUser.hasOwnProperty('unique_name')) {
                var uniqueUserName = mentionedUser["display_name"].replace(/ /g, ""),
                    uniqueUserNameCount = uniqueCount(uniqueUserName);

                if (uniqueUserNameCount > 0) {
                    uniqueUserName += uniqueUserNameCount;
                }

                mentionedUser["unique_name"] = uniqueUserName;
                uniqueNames.push(uniqueUserName);
            }
        });

        return engagedUsers.concat(teamUsers).filter(function(user) { return angular.isDefined(user); });
    }

    function getAnnoById(anno_list, anno_id) {
        var annoData = {};

        if (anno_list.length) {
            var annoDataList = anno_list.filter(function(anno) {
                return anno.id === anno_id;
            });

            if (annoDataList.length) {
                annoData = annoDataList[0];
            }
        }

        return annoData;
    };

    function watchersContainedIn(scope) {
        // from http://www.closedinterval.com/count-angularjs-watchers/
        var slice = [].slice;
        var elems = slice.call(document.querySelectorAll("*"));

        return elems.map(function(elem) {
            var data = angular.element(elem).data();
            return data.$scope || null;
        }).filter(function(scope) {
            return scope && scope.$$watchers;
        }).reduce(function(tmp, scope) {
            if (tmp.cache[scope.$id]) {
                return tmp;
            }
            tmp.cache[scope.$id] = true;
            tmp.count += scope.$$watchers.length;
            return tmp;
        }, {
            count : 0,
            cache : {}
        }).count;
    };

    function getUniqueData(data, key) {
        var newArr = [], found;

        angular.forEach(data, function(obj) {
            found = undefined;
            angular.forEach(newArr, function(newObj) {
                if (obj[key] === newObj[key] && !found) found = true;
            });
            if (!found) newArr.push(obj);
        });

        return newArr;
    };

    function getTeamNameForURL(team_name) {
        return team_name.replace(/\W+/g, "-").toLowerCase();
    }

    function getFullDashboardURL(community_name, team_hash) {
        if (community_name && team_hash) {
            var url = $location.protocol() + "://" + $location.host();
            if ($location.port() !== 443) {
                url = url + ":" + $location.port();
            }
            url = url + "/dashboard/" + team_hash + "/" + this.getTeamNameForURL(community_name);
            return url;
        } else {
            return "";
        }
    }

    function getDashboardURL() {
        var team_name = $cookieStore.get('team_name'),
            team_hash = $cookieStore.get('team_hash'),
            url = "/dashboard";

        if (angular.isDefined(team_name) && angular.isDefined(team_hash)) {
            url = url + "/" + team_hash + "/" + this.getTeamNameForURL(team_name);
        }

        return url;
    }

    return {
        getRoutePath : getRoutePath,
        storeUserDataInCookies : storeUserDataInCookies,
        removeUserDataCookies : removeUserDataCookies,
        removeRedirectURL : removeRedirectURL,
        replaceURLWithLink : replaceURLWithLink,
        replaceEmailWithName : replaceEmailWithName,
        replaceHashTagWithLink : replaceHashTagWithLink,
        replaceUniqueUserNameWithID : replaceUniqueUserNameWithID,
        findAncestor : findAncestor,
        getUniqueEngagedUsers : getUniqueEngagedUsers,
        getAnnoById : getAnnoById,
        watchersContainedIn : watchersContainedIn,
        getUniqueData : getUniqueData,
        getTeamNameForURL : getTeamNameForURL,
        getFullDashboardURL : getFullDashboardURL,
        getDashboardURL : getDashboardURL
    };
});

ServiceModule.factory('Autocomplete', function(Utils) {
{'fraction_non_alphanumeric': 0.16056910569105692, 'fraction_numerical': 0.0034058448692595032, 'mean_word_length': 2.164435946462715, 'pattern_counts': {'":': 4, '<': 39, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '778871', 'n_tokens_mistral': 5456, 'n_tokens_neox': 5286, 'n_words': 786}