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

import { eventOpenColumnFilters, headerButtons } from "shared/lib/slickgrid";
import { IColumnFilterValue } from "shared/model/types";

import { defaultValue } from "../../model";

import Styled from "./ButtonReset.style";

export const ButtonReset: FC<IProps> = ({ setIsOpened, setValue, value }) => {
	function reset() {
		const { columnId, table } = eventOpenColumnFilters;
		if (!table) return;
		const grid = table.grid;
		if (!grid) return;
		const args = table.filter.args;
		delete args.columnFilters?.[columnId];
		table.filter.args = args;
		headerButtons.resetColumn(table, columnId);
		setValue(defaultValue);
		setIsOpened(false);
	}
	return (
		<Styled.Button
			disabled={!value[1] && !value[2]}
			onClick={reset}
			variant={"outlined"}
			size={"small"}
		>
			Reset
		</Styled.Button>
	);
};

interface IProps {
	setIsOpened: Dispatch<SetStateAction<boolean>>;
	setValue: Dispatch<SetStateAction<IColumnFilterValue>>;
	value: IColumnFilterValue;
}
