"use client";

import { DealTagsCrud, useDealTagsCrud } from "@/hooks/cruds/useDealTagsCrud";
import useDealTagsList from "@/hooks/lists/useDealTagsList";
import { DealTagSchema, ProjectSchema } from "@/lib/client";
import makeContext from "@/lib/contextFactory/contextFactory";

type DealTagsContextState = {
    dealTags: DealTagSchema[];
    refetchDealTags: () => void;
    dealTagsCrud: DealTagsCrud;
};

type Props = {
    project: ProjectSchema;
};

const useDealTagsContextState = ({ project }: Props): DealTagsContextState => {
    const dealTagsList = useDealTagsList({ projectId: project.id });

    const dealTagsCrud = useDealTagsCrud({
        ...dealTagsList,
        projectId: project.id,
    });

    return {
        dealTags: dealTagsList.dealTags,
        refetchDealTags: dealTagsList.refetch,
        dealTagsCrud,
    };
};

export const [DealTagsContextProvider, useDealTagsContext] = makeContext<
    DealTagsContextState,
    Props
>(useDealTagsContextState, "DealTags");
