import React, { useEffect } from "react";

import { useRouter } from "next/router";
import Module from "@Front/Config/Module";
import { IBlock } from "@Front/Components/DesignSystem/SearchBlockList/BlockList/Block";
import DefaultDashboardWithList, { IPropsDashboardWithList } from "../DefaultDashboardWithList";
import { OfficeRole } from "le-coffre-resources/dist/Notary";
import OfficeRoles, { IGetRolesParams } from "@Front/Api/LeCoffreApi/Admin/OfficeRoles/OfficeRoles";

import RoleService from "src/common/Api/LeCoffreApi/sdk/RoleService";

type IProps = IPropsDashboardWithList;

export default function DefaultRoleDashboard(props: IProps) {
	const [roles, setRoles] = React.useState<OfficeRole[] | null>(null);
	const router = useRouter();
	const { roleUid } = router.query;
	useEffect(() => {
		/* TODO: review
		const query: IGetRolesParams = {
			include: { rules: true },
		};

		OfficeRoles.getInstance()
			.get(query)
			.then((roles) => setRoles(roles));
			*/

		RoleService.getRoles().then((processes: any[]) => {
			if (processes.length > 0) {
				const roles: any[] = processes.map((process: any) => process.processData);
				setRoles(roles);
			}
		});
	}, []);

	const onSelectedBlock = (block: IBlock) => {
		router.push(Module.getInstance().get().modules.pages.Roles.pages.RolesInformations.props.path.replace("[uid]", block.id));
	};

	return (
		<DefaultDashboardWithList
			{...props}
			onSelectedBlock={onSelectedBlock}
			blocks={
				roles
					? roles.map((role) => ({
						id: role.uid!,
						primaryText: role.name,
						isActive: role.uid === roleUid,
					}))
					: []
			}
			bottomButton={{
				link: Module.getInstance().get().modules.pages.Roles.pages.Create.props.path,
				text: "Créer un rôle",
			}}
		/>
	);
}
