/*
 * Copyright (c) 2023-2025 Datalayer, Inc.
 * Distributed under the terms of the Modified BSD License.
 */
import { useLocation } from './useLocation.js';
import { PlatformRoles, OrganizationRoles, TeamRoles, } from '../models/index.js';
import useNavigate from './useNavigate.js';
export const useAuthorization = () => {
    const navigate = useNavigate();
    const location = useLocation();
    const goToApplicationStateError = (message) => {
        console.warn(message);
        console.log('Application error while navigating to ', location);
        console.trace();
        navigate('/error/application/state');
    };
    // Account -------------------------------------------------------------------
    const checkUserAccountPermissions = (user, accountHandle) => {
        // Note: user object from API has handle_s property, not handle
        const userHandle = user.handle_s || user.handle;
        if (userHandle !== accountHandle) {
            goToApplicationStateError(`Permissions check failed for account handle: ${accountHandle} and user handle: ${userHandle}`);
        }
    };
    // Platform -------------------------------------------------------------------
    const checkIsPlatformAdmin = (user) => {
        return user.roles.includes(PlatformRoles.Admin.handle);
    };
    const checkIsPlatformGrowthManager = (user) => {
        return user.roles.includes(PlatformRoles.GrowthManager.handle);
    };
    const checkIsPlatformSuccessManager = (user) => {
        return user.roles.includes(PlatformRoles.SuccessManager.handle);
    };
    const checkIsPlatformMember = (user) => {
        return user.roles.includes(PlatformRoles.Member.handle);
    };
    const checkIsPlatformFeaturesPreviewer = (user) => {
        return user.roles.includes(PlatformRoles.FeaturesPreviewer.handle);
    };
    const checkIsPlatformUsageReviewer = (user) => {
        return user.roles.includes(PlatformRoles.UsageReviewer.handle);
    };
    // Organization -------------------------------------------------------------------
    const checkUserIsOrganizationOwner = (user, organization) => {
        for (let i = 0; i < organization.members.length; i++) {
            if (organization.members[i].id === user.id) {
                if (checkIsOrganizationOwner(organization.members[i])) {
                    return true;
                }
            }
        }
        return false;
    };
    const checkIsOrganizationOwner = (member) => {
        return member.roles.includes(OrganizationRoles.Owner.handle);
    };
    const checkIsOrganizationMember = (user, organization) => {
        for (let i = 0; i < organization.members.length; i++) {
            if (organization.members[i].id === user.id) {
                return true;
            }
        }
        return false;
    };
    const checkOrganizationsMembership = (organizationHandle, organizations) => {
        if (organizations) {
            let isAllowed = false;
            for (let i = 0; i < organizations.length; i++) {
                if (organizationHandle === organizations[i].handle) {
                    isAllowed = true;
                    break;
                }
            }
            if (!isAllowed) {
                goToApplicationStateError(`Check organizations membership fails for organization handle: ${organizationHandle} and organizations: ${organizations}`);
            }
        }
    };
    // Team -------------------------------------------------------------------
    const checkUserIsTeamOwer = (user, team) => {
        for (let i = 0; i < team.members.length; i++) {
            if (team.members[i].id === user.id) {
                if (checkIsTeamOwer(team.members[i])) {
                    return true;
                }
            }
        }
        return false;
    };
    const checkIsTeamOwer = (member) => {
        return member.roles.includes(TeamRoles.Owner.handle);
    };
    const checkIsTeamMember = (user, team) => {
        for (let i = 0; i < team.members.length; i++) {
            if (team.members[i].id === user.id) {
                return true;
            }
        }
        return false;
    };
    // Course -------------------------------------------------------------------
    const checkIsCourseStudent = (user, course) => {
        // TODO Implement this.
        return false;
    };
    // --------------------------------------------------------------------------
    return {
        checkIsCourseStudent,
        checkIsOrganizationMember,
        checkUserIsOrganizationOwner,
        checkIsPlatformAdmin,
        checkIsPlatformFeaturesPreviewer,
        checkIsPlatformGrowthManager,
        checkIsPlatformMember,
        checkIsPlatformSuccessManager,
        checkIsPlatformUsageReviewer,
        checkIsTeamMember,
        checkIsTeamOwer,
        checkOrganizationsMembership,
        checkUserAccountPermissions,
        checkIsOrganizationOwner,
        checkUserIsTeamOwer,
    };
};
export default useAuthorization;
