import {useDealSummariesFull} from "../../LeadsPage/hooks/useDealSummaries.tsx";
import {useForm} from "@mantine/form";
import {useEffect, useState} from "react";
import {BaseMarketplaceSchema, ClientSchema} from "../../../client";
import {DealStatusType} from "../../../shared/enums/DealStatus.ts";

type State = {
    marketplace: BaseMarketplaceSchema | null;
    dealStatus: DealStatusType | null;
    client: ClientSchema | null;
}
const useDealsPageState = () => {
    const {objects} = useDealSummariesFull();
    const form = useForm<State>({
        initialValues: {
            marketplace: null,
            dealStatus: null,
            client: null
        }
    });
    const [data, setData] = useState(objects);
    const applyFilters = () => {
        let result = objects;
        if (form.values.marketplace) {
            result = result.filter(obj => obj.baseMarketplace?.key === form.values.marketplace?.key);
        }
        if (form.values.dealStatus) {
            result = result.filter(obj => obj.status === form.values.dealStatus?.id);
        }
        if (form.values.client) {
            result = result.filter(obj => obj.clientName === form.values.client?.name);

        }
        setData(result);
    }
    useEffect(() => {
        applyFilters();
    }, [form.values, objects])
    return {data, form}
}

export default useDealsPageState;