import { Dispatch, FC, SetStateAction } from "react";

import { eventContextMenu } from "shared/lib/slickgrid";

import Styled from "../ContextMenu.style";

export const ButtonDownloadCSV: FC<TProps> = ({ setIsOpened }) => {
	const onClick = (): void => {
		const { clientId, clientName, table } = eventContextMenu;
		const grid = table?.grid;
		if (!table || !grid) {
			setIsOpened(false);
			return;
		}
		table.downloadManager.download(table, clientId, clientName);
		setIsOpened(false);
	};

	return (
		<Styled.Li onClick={onClick}>
			<Styled.Icon className={"sgi sgi-download"}></Styled.Icon>Download as CSV
		</Styled.Li>
	);
};

type TProps = {
	setIsOpened: Dispatch<SetStateAction<boolean>>;
};
