import { useQueryClient } from "@tanstack/react-query";
import { getQueryKey } from "@trpc/react-query";
import { DataNode, DepartmentDto, ObjectType, TreeDataNode } from "@nice/common";
import { api } from "../trpc";
import { findQueryData, getCacheDataFromQuery } from "../utils";
import { CrudOperation, emitDataChange } from "../../event";
export function useDepartment() {
	const queryClient = useQueryClient();
	const queryKey = getQueryKey(api.department);
	const create = api.department.create.useMutation({
		onSuccess: (result) => {
			queryClient.invalidateQueries({ queryKey });
			emitDataChange(
				ObjectType.DEPARTMENT,
				result as any,
				CrudOperation.CREATED
			);
		},
	});

	const update = api.department.update.useMutation({
		onSuccess: (result) => {
			queryClient.invalidateQueries({ queryKey });
			emitDataChange(
				ObjectType.DEPARTMENT,
				result as any,
				CrudOperation.UPDATED
			);
		},
	});

	const softDeleteByIds = api.department.softDeleteByIds.useMutation({
		onSuccess: (result) => {
			queryClient.invalidateQueries({ queryKey });
			emitDataChange(
				ObjectType.DEPARTMENT,
				result as any,
				CrudOperation.DELETED
			);
		},
	});

	const buildTree = (
		data: DepartmentDto[],
		parentId: string | null = null
	): TreeDataNode[] => {
		return data
			.filter((department) => department.parentId === parentId)
			.sort((a, b) => a.order - b.order)
			.map((department) => {
				const node: TreeDataNode = {
					title: department.name,
					key: department.id,
					value: department.id,
					hasChildren: department.hasChildren,
					children: department.hasChildren
						? buildTree(data, department.id)
						: undefined,
					data: department,
				};
				return node;
			});
	};
	const getDept = <T = DepartmentDto>(key: string) => {
		return findQueryData<T>(queryClient, api.department, key);
	};
	return {
		softDeleteByIds,
		update,
		create,
		// getTreeData,
		getDept,
	};
}
