/**
 * @see https://umijs.org/zh-CN/plugins/plugin-access
 * */

type InitialStateType = { currentUser?: API.LoginOutput };

export default function access(initialState: InitialStateType | undefined) {
    const { currentUser } = initialState ?? {};
    const { permissions } = currentUser ?? {};

    return {
        hasPermission: (code: string) => {
            if (!permissions || permissions?.length === 0) {
                return false;
            }
            return permissions.includes(code.toLowerCase());
        },
    };
}
