import { BoardSchema, StatusSchema, StatusService } from "../../../../../client";
import { modals } from "@mantine/modals";
import { notifications } from "../../../../../shared/lib/notifications.ts";


type Props = {
    refetch: () => void;
}

const useStatus = ({ refetch }: Props) => {
    const onEditStatusClick = (status: StatusSchema) => {
        modals.openContextModal({
            modal: "statusModal",
            title: "Редактирование статуса",
            withCloseButton: false,
            innerProps: {
                status,
                refetch,
            },
        });
    };

    const onCreateStatusClick = (board: BoardSchema) => {
        modals.openContextModal({
            modal: "statusModal",
            title: "Добавление статуса",
            withCloseButton: false,
            innerProps: {
                boardId: board.id,
                refetch,
            },
        });
    };

    const onDeleteStatusClick = (status: StatusSchema) => {
        if (!status) return;
        StatusService.deleteStatus({
            statusId: status.id,
        })
            .then(({ ok, message }) => {
                if (!ok) {
                    notifications.error({ message });
                    return;
                }
                refetch();
            })
            .catch(err => console.log(err));
    };

    return {
        onEditStatusClick,
        onCreateStatusClick,
        onDeleteStatusClick,
    };
};

export default useStatus;
