import { menuConfig } from '../assets/data/menuConfig';

export const getMenuConfig = (userInfo) => {
	return Object.entries(menuConfig).map(([key, group]) => ({
		title: group.title,
		link: `/${key}`,
		access: hasGroupAccess(userInfo, group),
		submenu: Object.entries(group.items).map(([itemKey, item]) => ({
			title: item.title,
			link: `/${key}/${itemKey}`,
			id: item.permissions.read
		}))
	}));
};

// 권한 체크 유틸리티 함수들
export const hasGroupAccess = (userInfo, group) => {
	return userInfo.auth_list?.some(auth =>
		Object.values(group.items).some(item =>
			Object.values(item.permissions).includes(auth.id)
		)
	);
};

export const isGroupFullySelected = (group, selectedPermissions) => {
	const allPermissions = group.items.flatMap(item =>
		Object.values(item.permissions).filter(Boolean)
	);
	return allPermissions.every(id => selectedPermissions.includes(id));
};

export const isItemDisabled = (item, selectedPermissions) => {
	// 상위 권한이 있는 경우 하위 권한이 이미 선택되어 있으면 disabled
	const higherPermissions = [
		item.permissions.update,
		item.permissions.delete,
		item.permissions.confirm
	].filter(Boolean);

	return higherPermissions.some(permission =>
		selectedPermissions.includes(permission)
	);
};