import React, { useMemo, useState } from "react";
import { useTranslation } from 'react-i18next';
import type { TFunction } from 'i18next';
import { Check, Package as PackageIcon, Loader2 } from "lucide-react";
import { Button } from "@/components/ui/button";
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card";
import { Badge } from "@/components/ui/badge";
import { useCheckoutWizard } from "../WizardContext";
import type { CheckoutPackage } from "../types";
const currencyFormatter = new Intl.NumberFormat("de-DE", {
style: "currency",
currency: "EUR",
minimumFractionDigits: 2,
});
function translateFeature(feature: string, t: TFunction<'marketing'>) {
const fallback = feature.replace(/_/g, ' ');
return t(`packages.feature_${feature}`, { defaultValue: fallback });
}
function PackageSummary({ pkg, t }: { pkg: CheckoutPackage; t: TFunction<'marketing'> }) {
const isFree = pkg.price === 0;
return (
{pkg.name}
{pkg.description}
{pkg.price === 0 ? t('packages.free') : currencyFormatter.format(pkg.price)}
{pkg.type === 'reseller' ? t('packages.subscription') : t('packages.one_time')}
{pkg.gallery_duration_label && (
{t('packages.gallery_days_label')}: {pkg.gallery_duration_label}
)}
{Array.isArray(pkg.description_breakdown) && pkg.description_breakdown.length > 0 && (
{t('packages.breakdown_label')}
{pkg.description_breakdown.map((row, index) => (
{row.title && (
{row.title}
)}
{row.value}
))}
)}
{Array.isArray(pkg.features) && pkg.features.length > 0 && (
{pkg.features.map((feature, index) => (
-
{translateFeature(feature, t)}
))}
)}
);
}
function PackageOption({ pkg, isActive, onSelect, t }: { pkg: CheckoutPackage; isActive: boolean; onSelect: () => void; t: TFunction<'marketing'> }) {
const isFree = pkg.price === 0;
return (
);
}
export const PackageStep: React.FC = () => {
const { t } = useTranslation('marketing');
const { selectedPackage, packageOptions, setSelectedPackage, resetPaymentState, nextStep } = useCheckoutWizard();
const [isLoading, setIsLoading] = useState(false);
// Early return if no package is selected
if (!selectedPackage) {
return (
{t('checkout.package_step.no_package_selected')}
);
}
const comparablePackages = useMemo(() => {
// Filter by type and sort: free packages first, then by price ascending
return packageOptions
.filter((pkg: CheckoutPackage) => pkg.type === selectedPackage.type)
.sort((a: CheckoutPackage, b: CheckoutPackage) => {
// Free packages first
if (a.price === 0 && b.price > 0) return -1;
if (a.price > 0 && b.price === 0) return 1;
// Then sort by price ascending
return a.price - b.price;
});
}, [packageOptions, selectedPackage]);
const handlePackageChange = (pkg: CheckoutPackage) => {
if (pkg.id === selectedPackage.id) {
return;
}
setSelectedPackage(pkg);
resetPaymentState();
};
const handleNextStep = async () => {
setIsLoading(true);
// Kleine Verzögerung für bessere UX
setTimeout(() => {
nextStep();
setIsLoading(false);
}, 300);
};
return (
);
};