import { DataScopeType, MenuType } from '@/services/enums';

/** 预设操作项 */
export const DefaultActionOptions = [
    { label: '添加', value: 'add' },
    { label: '修改', value: 'edit' },
    { label: '删除', value: 'delete' },
    { label: '清除', value: 'clear' },
    { label: '查询', value: 'query' },
    { label: '查看', value: 'view' },
    { label: '提交', value: 'submit' },
    { label: '导入', value: 'import' },
    { label: '导出', value: 'export' },
    { label: '审核', value: 'audit' },
    { label: '反审核', value: 'cancel-audit' },
    { label: '关闭', value: 'close' },
    { label: '反关闭', value: 'cancel-close' },
    { label: '发布', value: 'publish' },
    { label: '取消发布', value: 'cancel-publish' },
    { label: '取消', value: 'cancel' },
    { label: '撤消', value: 'revoke' },
    { label: '同步', value: 'sync' },
    { label: '打印', value: 'print' },
    { label: '生成', value: 'generate' },
    { label: '重置', value: 'reset' },
    { label: '授权', value: 'grant' },
].map((t) => ({ value: t.value, label: `${t.value} - ${t.label}` }));

// /**
//  * 生成权限代码
//  * @param menus 所有菜单列表
//  * @param pid 父菜单ID
//  * @returns
//  */
// export function buildPermissionConstant(menus: API.SysMenuOutput[], pid: number = 0) {
//     const items = menus.filter((t) => t.pid === pid);

//     let perm = '';
//     items.forEach((item) => {
//         if (item.menuType !== MenuType.BUTTON) {
//             perm = `${perm}
//             /** ${item.name} **/
//             ${item.code?.toUpperCase()?.replaceAll('-', '_')}:
//             {
//                 ${buildPermissionConstant(menus, item.id)}
//             },`;
//         } else {
//             perm = `${perm}
//             /** ${item.name} **/
//             OP_${item.code?.toUpperCase()?.replaceAll('-', '_')}: '${item.permission}',`;
//         }
//     });

//     return perm;
// }

/**
 * 生成权限代码
 * @param menus 所有菜单列表
 * @param pid 父菜单ID
 * @returns
 */
export function buildPermissionConstant(menus: API.SysMenuOutput[]) {
    let perm = '';
    menus.forEach((item) => {
        if (item.menuType !== MenuType.BUTTON) {
            perm = `${perm}
            /** ${item.name} **/
            ${item.code?.toUpperCase()?.replaceAll('-', '_')}: {${buildPermissionConstant(item.children ?? [])}
            },`;
        } else {
            perm = `${perm}
            /** ${item.name} **/
            OP_${item.code?.toUpperCase()?.replaceAll('-', '_')}: '${item.permission}',`;
        }
    });

    return perm;
}

/** 角色授权菜单项 */
export type RoleMenuActionItem = {
    id: number;
    pid: number;
    name: string;
    menuType: number;
    dataScopeType: number;
    permission?: string;
    /** 操作项 */
    actions?: RoleMenuActionItem[];
    children?: RoleMenuActionItem[] | null;
};

/** 获取用于角色授权菜单的菜单操作项列表 */
export function buildMenuActionTree(menus: API.SysMenuOutput[]) {
    const buildMenu = (parent_id: number) => {
        const list = menus.filter((t) => {
            return t.pid === parent_id && t.menuType !== MenuType.BUTTON;
        });
        if (list.length === 0) {
            return null;
        }

        const actionMenus: RoleMenuActionItem[] = [];
        list.forEach((item) => {
            const actions = menus.filter((t) => {
                return t.pid === item.id && t.menuType === MenuType.BUTTON;
            });
            actionMenus.push({
                id: item.id ?? 0,
                pid: item.pid ?? 0,
                name: item.name ?? '',
                menuType: item.menuType ?? MenuType.FOLDER,
                dataScopeType: DataScopeType.NONE,
                permission: item.permission,
                children: buildMenu(item.id ?? 0),
                actions: actions as RoleMenuActionItem[],
            });
        });

        return actionMenus;
    };

    return buildMenu(0) ?? [];
}

/** 获取用于角色授权菜单的菜单操作项列表 */
export function buildRoleMenuActions(menus: (API.SysMenuOutput | undefined)[]) {
    const actionMenus: RoleMenuActionItem[] = [];
    menus
        .filter((t) => t?.menuType !== MenuType.BUTTON)
        .forEach((item) => {
            const actions = menus.filter((t) => {
                return t?.pid === item?.id && t?.menuType === MenuType.BUTTON;
            });
            actionMenus.push({
                id: item?.id ?? 0,
                pid: item?.pid ?? 0,
                name: item?.name ?? '',
                menuType: item?.menuType ?? MenuType.FOLDER,
                permission: item?.permission,
                actions: actions as RoleMenuActionItem[],
                dataScopeType: DataScopeType.NONE,
            });
        });

    return actionMenus;
}
