//#region src/utils/permissions.ts
const ROLE_POLICY = {
	admin: {
		"organization:read": () => true,
		"organization:write": () => true,
		"organization:admin": () => true,
		"project:read": () => true,
		"project:write": () => true,
		"project:admin": () => true,
		"dictionary:read": () => true,
		"dictionary:write": () => true,
		"dictionary:admin": () => true,
		"tag:read": () => true,
		"tag:write": () => true,
		"tag:admin": () => true,
		"user:read": () => true,
		"user:write": () => true,
		"user:admin": () => true
	},
	user: {
		"user:read": ({ user, targetUsers }) => targetUsers.every((targetUser) => String(targetUser.id) === String(user?.id) && targetUser.email === user?.email),
		"user:write": ({ user, targetUsers }) => targetUsers.every((targetUser) => String(targetUser.id) === String(user?.id) && targetUser.email === user?.email),
		"user:admin": ({ user, targetUsers }) => targetUsers.every((targetUser) => String(targetUser.id) === String(user?.id) && targetUser.email === user?.email),
		"organization:read": ({ user, targetOrganizations }) => targetOrganizations.every((organization) => organization?.membersIds?.map(String).includes(String(user?.id)))
	},
	org_admin: {
		"organization:read": ({ organization, targetOrganizations }) => targetOrganizations.every((targetOrg) => String(targetOrg.id) === String(organization?.id)),
		"organization:write": ({ organization, targetOrganizations }) => targetOrganizations.every((targetOrg) => String(targetOrg.id) === String(organization?.id)),
		"organization:admin": ({ organization, targetOrganizations }) => targetOrganizations.every((targetOrg) => String(targetOrg.id) === String(organization?.id)),
		"project:read": ({ organization, project }) => String(project?.organizationId) === String(organization?.id),
		"project:write": ({ organization, project }) => String(project?.organizationId) === String(organization?.id),
		"project:admin": ({ organization, project }) => String(project?.organizationId) === String(organization?.id),
		"tag:read": ({ organization, project }) => String(project?.organizationId) === String(organization?.id),
		"tag:write": ({ organization, project }) => String(project?.organizationId) === String(organization?.id),
		"tag:admin": ({ organization, project }) => String(project?.organizationId) === String(organization?.id),
		"user:write": ({ organization, targetUsers }) => targetUsers.every((targetUser) => organization?.membersIds?.map(String).includes(String(targetUser?.id)))
	},
	org_user: {
		"organization:read": ({ organization, targetOrganizations }) => targetOrganizations.every((targetOrg) => String(targetOrg.id) === String(organization?.id)),
		"project:read": ({ user, targetProjects }) => targetProjects?.every((project) => project?.membersIds?.map(String).includes(String(user?.id))),
		"user:read": ({ organization, targetUsers }) => targetUsers.every((targetUser) => organization?.membersIds?.map(String).includes(String(targetUser?.id)))
	},
	project_admin: {
		"project:read": ({ project, targetProjectIds }) => targetProjectIds?.every((projectId) => String(project?.id) === String(projectId)),
		"project:write": ({ project, targetProjectIds }) => targetProjectIds?.every((projectId) => String(project?.id) === String(projectId)),
		"project:admin": ({ project, targetProjectIds }) => targetProjectIds?.every((projectId) => String(project?.id) === String(projectId)),
		"tag:read": ({ project, targetTags }) => targetTags.every((tag) => String(project?.id) === String(tag?.projectId)),
		"tag:write": ({ project, targetTags }) => targetTags.every((tag) => String(project?.id) === String(tag?.projectId)),
		"tag:admin": ({ project, targetTags }) => targetTags.every((tag) => String(project?.id) === String(tag?.projectId)),
		"user:write": ({ project, targetUsers }) => targetUsers.every((targetUser) => project?.membersIds?.map(String).includes(String(targetUser?.id))),
		"dictionary:read": ({ project, user }) => project?.adminsIds?.map(String).includes(String(user?.id)),
		"dictionary:write": ({ project, user }) => project?.adminsIds?.map(String).includes(String(user?.id)),
		"dictionary:admin": ({ project, user }) => project?.adminsIds?.map(String).includes(String(user?.id))
	},
	project_user: {
		"project:read": ({ user, organization, project }) => String(organization?.id) === String(project?.organizationId) && organization?.membersIds?.map(String).includes(String(user?.id)) && project?.membersIds?.map(String).includes(String(user?.id)),
		"dictionary:read": ({ user, project, targetDictionaries }) => project?.membersIds?.map(String).includes(String(user?.id)) && targetDictionaries.every((dictionary) => dictionary?.projectIds?.map(String).includes(String(project?.id))),
		"dictionary:write": ({ user, project }) => project?.adminsIds?.map(String).includes(String(user?.id)),
		"tag:read": ({ project, targetTags, user }) => project?.membersIds?.map(String).includes(String(user?.id)) && targetTags.every((tag) => String(project?.id) === String(tag?.projectId)),
		"tag:write": ({ project, targetTags, user }) => project?.membersIds?.map(String).includes(String(user?.id)) && targetTags.every((tag) => String(project?.id) === String(tag?.projectId)),
		"tag:admin": ({ user, project, targetTags }) => project?.adminsIds?.map(String).includes(String(user?.id)) && targetTags.every((tag) => String(project?.id) === String(tag?.projectId)),
		"user:read": ({ project, targetUsers }) => targetUsers.every((targetUser) => project?.membersIds?.map(String).includes(String(targetUser?.id)))
	},
	project_reviewer: {
		"dictionary:read": ({ user, project }) => project?.membersIds?.map(String).includes(String(user?.id)),
		"dictionary:write": ({ user, project }) => project?.membersIds?.map(String).includes(String(user?.id)),
		"tag:read": () => true
	}
};
const getSessionRoles = ({ user, organization, project }) => {
	const roles = [];
	if (!user) return roles;
	roles.push("user");
	if (user.role === "admin") roles.push("admin");
	if (!organization) return roles;
	if (organization.adminsIds?.map(String).includes(String(user?.id))) roles.push("org_admin");
	if (organization.membersIds?.map(String).includes(String(user?.id))) roles.push("org_user");
	if (!project) return roles;
	if (project.adminsIds?.map(String).includes(String(user?.id))) roles.push("project_admin");
	if (project?.membersIds?.map(String).includes(String(user?.id))) roles.push("project_user");
	return roles;
};
const computeEffectivePermission = (roles) => Array.from(new Set(roles.flatMap((role) => Object.keys(ROLE_POLICY[role]))));
/**
* Intersect two permission lists
* @param permissionList1 - The first permission list
* @param permissionList2 - The second permission list
* @returns The intersection of the two permission lists (only permissions present in both)
*/
const intersectPermissions = (permissions1, permissions2) => permissions1.filter((permission) => permissions2.includes(permission));
const hasPermission = (roles, permission) => {
	const rolesCheck = roles.map((role) => ROLE_POLICY[role]?.[permission] ?? (() => false));
	return ((args) => rolesCheck.some((check) => check(args)));
};

//#endregion
export { ROLE_POLICY, computeEffectivePermission, getSessionRoles, hasPermission, intersectPermissions };
//# sourceMappingURL=permissions.mjs.map