<commit_before>import '../../polyfills/array-find';

const callbacks = [];
let currentMode = null;
let initialized = false;
let managerItem = null;
let userItem = null;

function callCallbacks(data) {
    callbacks.map((callback) => {
        if (callback && window.chayns.utils.isFunction(callback)) {
            callback(data);
        }
    });
}

function getChangeListener() {
    return (data) => {
        callCallbacks(data);

        currentMode = data;
    };
}

function setDefaultGroup(mode = 0) {
    initialized = true;

    getChangeListener()({
        id: mode
    });

    window.chayns.ui.modeSwitch.changeMode(0);
}

function getUacIds(group) {
    let retval = [];

    if (group.uacId && window.chayns.utils.isNumber(group.uacId)) {
        retval.push(group.uacId);
    }

    if (group.uacIds && window.chayns.utils.isArray(group.uacIds)) {
        retval = retval.concat(group.uacIds);
    }

    return retval;
}

function getAllowedUacIdsFromArray(uacArray) {
    const userGroups = window.chayns.env.user.groups;

    const allowedUacIds = [];

    for (let i = 0, x = userGroups.length; i < x; i += 1) {
        if (uacArray.indexOf(userGroups[i].id) !== -1) {
            allowedUacIds.push(userGroups[i].id);
        }
    }

    return allowedUacIds;
}

function convertToGroupObject(element) {
    if (window.chayns.utils.isObject(element)) {
        return element;
    }

    return {
        id: element,
        uacIds: [element]
    };
}

function getGroupObject(id, name, uacs) {
    return {
        id,
        uacIds: uacs,
        name
    };
}

function getSavedMode() {
    return window.chayns.utils.ls.get('react__modeSwitch--currentMode');
}

function setSavedMode(mode) {
    return window.chayns.utils.ls.set('react__modeSwitch--currentMode', mode.id);
}

function getDefaultMode() {
    const name = (window.chayns.env.user.isAuthenticated) ? window.chayns.env.user.name : '';

    return getGroupObject(0, name, null);
}

export default class ModeSwitchHelper {
    static init(options) {
        userItem = null;
        managerItem = null;

        if (options.groups) {
            if (window.chayns.utils.isFunction(options.onChange)) {
                callbacks.push(options.onChange);
            }

            if (options.save) {
                callbacks.push(setSavedMode);
            }

            const allowedGroups = [];
            let isChaynsIdAdmin = false;

            const groups = [];
            if (options.groups) {
                options.groups.map((element) => {
                    const group = convertToGroupObject(element);

                    groups.push(group);
                });
            }

            if (window.chayns.env.user.isAuthenticated) {
                userItem = getGroupObject(0, window.chayns.env.user.name, [0]);

                const managerGroup = ModeSwitchHelper.findManagerGroup(groups);
                if (managerGroup) {
                    const { id, name, uacIds } = managerGroup;

                    managerItem = getGroupObject(id, name, uacIds);
                }
            }

            chayns.ready.then((data) => {
                if (window.chayns.env.user.isAuthenticated) {
                    // Condition if adminMode ChaynsId
                    let groupObject;

                    if (managerItem && data && data.AppUser.AdminMode && hasAdminSwitch()) {
                        groupObject = managerItem;
                        isChaynsIdAdmin = true;
                    } else {
                        groupObject = userItem;
                        groupObject.default = true;
                    }

                    allowedGroups.push(groupObject);


                    let savedModeId = null;
                    let changeGroupIndex = 0;
                    if (options.save) {
                        savedModeId = getSavedMode();
                    }

                    if (savedModeId === null && options.defaultMode) {
                        savedModeId = options.defaultMode;
                    }

                    let changeGroup = false;
                    let changeGroupValue = null;

                    for (let i = 0, x = groups.length; i < x; i += 1) {
                        if (!groups[i].uacId && !groups[i].uacIds) {
                            const addGroupObject = getGroupObject(groups[i].id, groups[i].name, [0]);
                            allowedGroups.push(addGroupObject);

                            if (addGroupObject.id === savedModeId) {
                                changeGroup = true;
                                changeGroupIndex = allowedGroups.length - 1;
                                changeGroupValue = addGroupObject;
                            }
                        } else {
                            const uacIds = getUacIds(groups[i]);
                            const allowedUacs = getAllowedUacIdsFromArray(uacIds);

                            if (allowedUacs.length > 0 && (!hasAdminSwitch() || !managerItem || groups[i].id !== managerItem.id)) {
                                const addGroupObject = getGroupObject(groups[i].id, groups[i].name, allowedUacs);
                                allowedGroups.push(addGroupObject);<commit_msg>Move mode-selection to own function<commit_after>import '../../polyfills/array-find';

const callbacks = [];
let currentMode = null;
let initialized = false;
let managerItem = null;
let userItem = null;

function callCallbacks(data) {
    callbacks.map((callback) => {
        if (callback && window.chayns.utils.isFunction(callback)) {
            callback(data);
        }
    });
}

function getChangeListener() {
    return (data) => {
        callCallbacks(data);

        currentMode = data;
    };
}

function setDefaultGroup(mode = 0) {
    initialized = true;

    getChangeListener()({
        id: mode
    });

    window.chayns.ui.modeSwitch.changeMode(0);
}

function getUacIds(group) {
    let retval = [];

    if (group.uacId && window.chayns.utils.isNumber(group.uacId)) {
        retval.push(group.uacId);
    }

    if (group.uacIds && window.chayns.utils.isArray(group.uacIds)) {
        retval = retval.concat(group.uacIds);
    }

    return retval;
}

function getAllowedUacIdsFromArray(uacArray) {
    const userGroups = window.chayns.env.user.groups;

    const allowedUacIds = [];

    for (let i = 0, x = userGroups.length; i < x; i += 1) {
        if (uacArray.indexOf(userGroups[i].id) !== -1) {
            allowedUacIds.push(userGroups[i].id);
        }
    }

    return allowedUacIds;
}

function convertToGroupObject(element) {
    if (window.chayns.utils.isObject(element)) {
        return element;
    }

    return {
        id: element,
        uacIds: [element]
    };
}

function getGroupObject(id, name, uacs) {
    return {
        id,
        uacIds: uacs,
        name
    };
}

function getSavedMode() {
    return window.chayns.utils.ls.get('react__modeSwitch--currentMode');
}

function setSavedMode(mode) {
    return window.chayns.utils.ls.set('react__modeSwitch--currentMode', mode.id);
}

function getDefaultMode() {
    const name = (window.chayns.env.user.isAuthenticated) ? window.chayns.env.user.name : '';

    return getGroupObject(0, name, null);
}

function getPreferredMode(options) {
    let savedModeId = null;

    if (options.save) {
        savedModeId = getSavedMode();
    }

    if (savedModeId === null && options.defaultMode) {
        savedModeId = options.defaultMode;
    }

    return savedModeId;
}

export default class ModeSwitchHelper {
    static init(options) {
        userItem = null;
        managerItem = null;

        if (options.groups) {
            if (window.chayns.utils.isFunction(options.onChange)) {
                callbacks.push(options.onChange);
            }

            if (options.save) {
                callbacks.push(setSavedMode);
            }

            const allowedGroups = [];
            let savedModeId = getPreferredMode(options);
            let isChaynsIdAdmin = false;

            const groups = [];
            if (options.groups) {
                options.groups.map((element) => {
                    const group = convertToGroupObject(element);

                    groups.push(group);
                });
            }

            if (window.chayns.env.user.isAuthenticated) {
                userItem = getGroupObject(0, window.chayns.env.user.name, [0]);

                const managerGroup = ModeSwitchHelper.findManagerGroup(groups);
                if (managerGroup) {
                    const { id, name, uacIds } = managerGroup;

                    managerItem = getGroupObject(id, name, uacIds);
                }
            }

            chayns.ready.then((data) => {
                if (window.chayns.env.user.isAuthenticated) {
                    // Condition if adminMode ChaynsId
                    let groupObject;

                    if (managerItem && data && data.AppUser.AdminMode && hasAdminSwitch()) {
                        groupObject = managerItem;
                        isChaynsIdAdmin = true;
                    } else {
                        groupObject = userItem;
                        groupObject.default = true;
                    }

                    allowedGroups.push(groupObject);


                    let changeGroupIndex = 0;
                    let changeGroup = false;
                    let changeGroupValue = null;

                    for (let i = 0, x = groups.length; i < x; i += 1) {
                        if (!groups[i].uacId && !groups[i].uacIds) {
                            const addGroupObject = getGroupObject(groups[i].id, groups[i].name, [0]);
                            allowedGroups.push(addGroupObject);

                            if (addGroupObject.id === savedModeId) {
                                changeGroup = true;
                                changeGroupIndex = allowedGroups.length - 1;
                                changeGroupValue = addGroupObject;
                            }
                        } else {
                            const uacIds = getUacIds(groups[i]);
                            const allowedUacs = getAllowedUacIdsFromArray(uacIds);

                            if (allowedUacs.length > 0 && (!hasAdminSwitch() || !managerItem || groups[i].id !== managerItem.id)) {
                                const addGroupObject = getGroupObject(groups[i].id, groups[i].name, allowedUacs);
                                allowedGroups.push(addGroupObject);
{'fraction_non_alphanumeric': 0.11012298959318827, 'fraction_numerical': 0.0026490066225165563, 'mean_word_length': 1.5947471772214041, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '1459788', 'n_tokens_mistral': 3017, 'n_tokens_neox': 2880, 'n_words': 536}