import { useCallback } from 'react';
import { useAdminPortalUIConfig } from '../StytchClientContext.mjs';

const filterToRBACFields = (role)=>{
    const { role_id, description, permissions } = role;
    return {
        role_id,
        description,
        permissions
    };
};
const useGetRoleSortOrder = ()=>{
    const config = useAdminPortalUIConfig();
    return useCallback((roles)=>{
        const filteredRoles = roles.map(filterToRBACFields);
        const sortOrder = config?.getRoleSortOrder ? config.getRoleSortOrder(filteredRoles) : [];
        if (!sortOrder.length) {
            return roles.slice();
        }
        const remainingRoles = new Map(roles.map((role)=>[
                role.role_id,
                role
            ]));
        const customSortedRoles = [];
        for (const roleId of sortOrder){
            if (remainingRoles.has(roleId)) {
                customSortedRoles.push(remainingRoles.get(roleId));
                remainingRoles.delete(roleId);
            }
        }
        return customSortedRoles.concat([
            ...remainingRoles.values()
        ]);
    }, [
        config
    ]);
};

export { useGetRoleSortOrder };
//# sourceMappingURL=useGetRoleSortOrder.mjs.map
