import { useCrudOperations } from "@/hooks/cruds/baseCrud";
import {
    CreateMarketplaceSchema,
    MarketplaceSchema,
    UpdateMarketplaceSchema,
} from "@/lib/client";
import {
    createMarketplaceMutation,
    deleteMarketplaceMutation,
    updateMarketplaceMutation,
} from "@/lib/client/@tanstack/react-query.gen";

type UseMarketplacesProps = {
    queryKey: any[];
};

export type MarketplacesCrud = {
    onCreate: (mp: CreateMarketplaceSchema) => void;
    onUpdate: (
        mpId: number,
        mp: UpdateMarketplaceSchema,
        onSuccess?: () => void
    ) => void;
    onDelete: (mp: MarketplaceSchema) => void;
};

export const useMarketplacesCrud = ({
    queryKey,
}: UseMarketplacesProps): MarketplacesCrud => {
    return useCrudOperations<
        MarketplaceSchema,
        UpdateMarketplaceSchema,
        CreateMarketplaceSchema
    >({
        key: "getMarketplaces",
        queryKey,
        mutations: {
            create: createMarketplaceMutation(),
            update: updateMarketplaceMutation(),
            delete: deleteMarketplaceMutation(),
        },
        getUpdateEntity: (old, update) => ({
            ...old,
            baseMarketplace: update.baseMarketplace ?? old.baseMarketplace,
            name: update.name ?? old.name,
            authData: update.authData ?? old.authData,
        }),
        getDeleteConfirmTitle: () => "Удаление маркетплейса",
    });
};
