import { modals } from "@mantine/modals";
import { useMarketplacesContext } from "@/app/clients/drawers/ClientMarketplacesDrawer/contexts/MarketplacesContext";
import { MarketplaceSchema } from "@/lib/client";

const useMarketplacesActions = () => {
    const { marketplacesCrud, client } = useMarketplacesContext();

    const onCreateClick = () => {
        modals.openContextModal({
            modal: "marketplaceEditorModal",
            title: "Создание маркетплейса",
            innerProps: {
                onCreate: values =>
                    marketplacesCrud.onCreate({ ...values, client }),
                isEditing: false,
            },
        });
    };

    const onUpdateClick = (marketplace: MarketplaceSchema) => {
        modals.openContextModal({
            modal: "marketplaceEditorModal",
            title: "Редактирование маркетплейса",
            innerProps: {
                onChange: updates =>
                    marketplacesCrud.onUpdate(marketplace.id, updates),
                entity: marketplace,
                isEditing: true,
            },
        });
    };

    return {
        onCreateClick,
        onUpdateClick,
    };
};

export default useMarketplacesActions;
