type key_val = {
    [key: string]: any;
};

// Hierarchical Order (Highest to Lowest)
export const access_level_order = [
    'super',
    'manager',
    'administrator',
    'trusted',
    'public',
    'authenticated',
    'anonymous'
];

/**
 * Compares two access levels based on the hierarchy.
 * @returns 1 if level_a is higher, -1 if level_b is higher, 0 if equal.
 */
export const compare_access_levels = function (
    level_a: string,
    level_b: string
): number {
    const index_a = access_level_order.indexOf(level_a || 'anonymous');
    const index_b = access_level_order.indexOf(level_b || 'anonymous');

    // LOWER index means HIGHER priority in the array
    if (index_a < index_b) return 1;
    if (index_a > index_b) return -1;
    return 0;
};

// NOTE: I know there is a better more efficient way to do this, but I don't have time for that right now.
// Reminder: super > manager > administrator > trusted > public > authenticated > anonymous
// Super is the highest level. Anonymous is the lowest level.
export const process_permission_checks = function process_permission_checks(
    access_type: string
) {
    // let access_checks = { 'access_type': null, 'super_check': null };
    const access_checks: key_val = {};

    if (access_type == 'super') {
        access_checks.allow_access = true;
        access_checks.access_type = 'super';

        access_checks.super_check = true;
        access_checks.manager_check = false;
        access_checks.administrator_check = false;
        access_checks.support_check = false;
        access_checks.assistant_check = false;
        access_checks.trusted_check = false;
        access_checks.verified_check = false;
        access_checks.provisional_check = false;
        access_checks.public_check = false;
        access_checks.authenticated_check = true;
        access_checks.anonymous_check = false;

        access_checks.super_access = true;
        access_checks.manager_access = true;
        access_checks.administrator_access = true;
        access_checks.support_access = true;
        access_checks.assistant_access = true;
        access_checks.trusted_access = true;
        access_checks.verified_access = true;
        access_checks.provisional_access = true;
        access_checks.public_access = true;
        access_checks.authenticated_access = true;
        access_checks.anonymous_access = true;
    } else if (access_type == 'manager') {
        access_checks.allow_access = true;
        access_checks.access_type = 'manager';

        access_checks.super_check = false;
        access_checks.manager_check = true;
        access_checks.administrator_check = false;
        access_checks.support_check = false;
        access_checks.assistant_check = false;
        access_checks.trusted_check = false;
        access_checks.verified_check = false;
        access_checks.provisional_check = false;
        access_checks.public_check = false;
        access_checks.authenticated_check = true;
        access_checks.anonymous_check = false;

        access_checks.super_access = false;
        access_checks.manager_access = true;
        access_checks.administrator_access = true;
        access_checks.support_access = true;
        access_checks.assistant_access = true;
        access_checks.trusted_access = true;
        access_checks.verified_access = true;
        access_checks.provisional_access = true;
        access_checks.public_access = true;
        access_checks.authenticated_access = true;
        access_checks.anonymous_access = true;
    } else if (access_type == 'administrator') {
        access_checks.allow_access = true;
        access_checks.access_type = 'administrator';

        access_checks.super_check = false;
        access_checks.manager_check = false;
        access_checks.administrator_check = true;
        access_checks.support_check = false;
        access_checks.assistant_check = false;
        access_checks.trusted_check = false;
        access_checks.verified_check = false;
        access_checks.provisional_check = false;
        access_checks.public_check = false;
        access_checks.authenticated_check = false;
        access_checks.anonymous_check = false;

        access_checks.super_access = false;
        access_checks.manager_access = false;
        access_checks.administrator_access = true;
        access_checks.support_access = true;
        access_checks.assistant_access = true;
        access_checks.trusted_access = true;
        access_checks.verified_access = true;
        access_checks.provisional_access = true;
        access_checks.public_access = true;
        access_checks.authenticated_access = true;
        access_checks.anonymous_access = true;
    } else if (access_type == 'trusted') {
        access_checks.allow_access = true; // Should this be true?? -2024-10-03
        access_checks.access_type = 'trusted';

        access_checks.super_check = false;
        access_checks.manager_check = false;
        access_checks.administrator_check = false;
        access_checks.support_check = false;
        access_checks.assistant_check = false;
        access_checks.trusted_check = true;
        access_checks.verified_check = false;
        access_checks.provisional_check = false;
        access_checks.public_check = false;
        access_checks.authenticated_check = true;
        access_checks.anonymous_check = false;

        access_checks.super_access = false;
        access_checks.manager_access = false;
        access_checks.administrator_access = false;
        access_checks.support_access = false;
        access_checks.assistant_access = false;
        access_checks.trusted_access = true;
        access_checks.verified_access = true;
        access_checks.provisional_access = true;
        access_checks.public_access = true;
        access_checks.authenticated_access = true;
        access_checks.anonymous_access = true;
    } else if (access_type == 'public') {
        access_checks.access_type = 'public';

        access_checks.super_check = false;
        access_checks.manager_check = false;
        access_checks.administrator_check = false;
        access_checks.support_check = false;
        access_checks.assistant_check = false;
        access_checks.trusted_check = false;
        access_checks.verified_check = false;
        access_checks.provisional_check = false;
        access_checks.public_check = true;
        access_checks.authenticated_check = false;
        access_checks.anonymous_check = false;

        access_checks.super_access = false;
        access_checks.manager_access = false;
        access_checks.administrator_access = false;
        access_checks.support_access = false;
        access_checks.assistant_access = false;
        access_checks.trusted_access = false;
        access_checks.verified_access = false;
        access_checks.provisional_access = false;
        access_checks.public_access = true;
        access_checks.authenticated_access = true;
        access_checks.anonymous_access = true;
    } else if (access_type == 'authenticated') {
        access_checks.access_type = 'authenticated';

        access_checks.super_check = false;
        access_checks.manager_check = false;
        access_checks.administrator_check = false;
        access_checks.support_check = false;
        access_checks.assistant_check = false;
        access_checks.trusted_check = false;
        access_checks.verified_check = false;
        access_checks.provisional_check = false;
        access_checks.public_check = false;
        access_checks.authenticated_check = true;
        access_checks.anonymous_check = false;

        access_checks.super_access = false;
        access_checks.manager_access = false;
        access_checks.administrator_access = false;
        access_checks.support_access = false;
        access_checks.assistant_access = false;
        access_checks.trusted_access = false;
        access_checks.verified_access = false;
        access_checks.provisional_access = false;
        access_checks.public_access = false;
        access_checks.authenticated_access = true;
        access_checks.anonymous_access = true;
    } else {
        access_checks.access_type = 'anonymous';

        access_checks.super_check = false;
        access_checks.manager_check = false;
        access_checks.administrator_check = false;
        access_checks.support_check = false;
        access_checks.assistant_check = false;
        access_checks.trusted_check = false;
        access_checks.verified_check = false;
        access_checks.provisional_check = false;
        access_checks.public_check = false;
        access_checks.authenticated_check = false;
        access_checks.anonymous_check = true;

        access_checks.super_access = false;
        access_checks.manager_access = false;
        access_checks.administrator_access = false;
        access_checks.support_access = false;
        access_checks.assistant_access = false;
        access_checks.trusted_access = false;
        access_checks.verified_access = false;
        access_checks.provisional_access = false;
        access_checks.public_access = false;
        access_checks.authenticated_access = false;
        access_checks.anonymous_access = true;
    }

    return access_checks;
};
