"use client";

import {
    MarketplacesCrud,
    useMarketplacesCrud,
} from "@/app/clients/hooks/cruds/useMarketplacesCrud";
import useMarketplacesList from "@/app/clients/hooks/lists/useMarketplacesList";
import { ClientSchema, MarketplaceSchema } from "@/lib/client";
import makeContext from "@/lib/contextFactory/contextFactory";

type MarketplacesContextState = {
    client: ClientSchema;
    marketplaces: MarketplaceSchema[];
    refetchMarketplaces: () => void;
    marketplacesCrud: MarketplacesCrud;
};

type Props = {
    client: ClientSchema;
};

const useMarketplacesContextState = ({
    client,
}: Props): MarketplacesContextState => {
    const marketplacesList = useMarketplacesList({ clientId: client.id });

    const marketplacesCrud = useMarketplacesCrud(marketplacesList);

    return {
        client,
        marketplaces: marketplacesList.marketplaces,
        refetchMarketplaces: marketplacesList.refetch,
        marketplacesCrud,
    };
};

export const [MarketplacesContextProvider, useMarketplacesContext] =
    makeContext<MarketplacesContextState, Props>(
        useMarketplacesContextState,
        "Marketplaces"
    );
