import { getQueryKey } from "@trpc/react-query";
import { api } from "../trpc"; // Adjust path as necessary
import { useQueryClient } from "@tanstack/react-query";
import { CrudOperation, emitDataChange, EventBus } from "../../event";
import { ObjectType } from "@nice/common";
type RoleMapHookReturn = {
	create: ReturnType<typeof api.rolemap.setRoleForObject.useMutation>;
	update: ReturnType<typeof api.rolemap.update.useMutation>;
	setRoleForObjects: ReturnType<typeof api.rolemap.setRoleForObjects.useMutation>;
	deleteMany: ReturnType<typeof api.rolemap.deleteMany.useMutation>;
	addRoleForObjects: ReturnType<typeof api.rolemap.addRoleForObjects.useMutation>;
};

export function useRoleMap(): RoleMapHookReturn {
	const queryClient = useQueryClient();
	const queryKey = getQueryKey(api.rolemap);

	const create = api.rolemap.setRoleForObject.useMutation({
		onSuccess: () => {
			queryClient.invalidateQueries({ queryKey });
		},
	});
	const setRoleForObjects = api.rolemap.setRoleForObjects.useMutation({
		onSuccess: () => {
			queryClient.invalidateQueries({ queryKey });
		},
	});
	const addRoleForObjects = api.rolemap.addRoleForObjects.useMutation({
		onSuccess: (result) => {
			queryClient.invalidateQueries({ queryKey });
			emitDataChange(ObjectType.ROLE_MAP, result as any, CrudOperation.CREATED)
		},
	});
	const update = api.rolemap.update.useMutation({
		onSuccess: () => {
			queryClient.invalidateQueries({ queryKey });
		},
	});
	const deleteMany = api.rolemap.deleteMany.useMutation({
		onSuccess: (result) => {

			queryClient.invalidateQueries({ queryKey });
			emitDataChange(ObjectType.ROLE_MAP, result as any, CrudOperation.DELETED)

		},
	});

	return {
		create,
		update,
		setRoleForObjects,
		deleteMany,
		addRoleForObjects
	};
}

