"use client";
import { useState } from "react";
import Image from "next/image";
import Link from "next/link";
import { motion, AnimatePresence } from "framer-motion";
import { ChevronDown, Star, Minus, Plus } from "lucide-react";
import type { Product } from "@/types/saleor";
import { useSaleorCheckoutStore } from "@/stores/saleorCheckoutStore";
import { getProductPrice, getLocalizedProduct } from "@/lib/saleor";
import ProductCard from "@/components/product/ProductCard";
import ProductBenefits from "@/components/product/ProductBenefits";
import ProductReviews from "@/components/product/ProductReviews";
import AsSeenIn from "@/components/home/AsSeenIn";
import TrustBadges from "@/components/home/TrustBadges";
import BeforeAfterGallery from "@/components/home/BeforeAfterGallery";
import HowItWorks from "@/components/home/HowItWorks";
import NewsletterSection from "@/components/home/NewsletterSection";
interface ProductDetailProps {
product: Product;
relatedProducts: Product[];
locale?: string;
}
// Expandable Section Component
function ExpandableSection({
title,
children,
defaultOpen = false
}: {
title: string;
children: React.ReactNode;
defaultOpen?: boolean;
}) {
const [isOpen, setIsOpen] = useState(defaultOpen);
return (
setIsOpen(!isOpen)}
className="w-full py-5 flex items-center justify-between text-left group"
>
{title}
{isOpen && (
{children}
)}
);
}
// Star Rating Component
function StarRating({ rating = 5, count = 0 }: { rating?: number; count?: number }) {
return (
{[...Array(5)].map((_, i) => (
))}
{count > 0 && (
({count})
)}
);
}
export default function ProductDetail({ product, relatedProducts, locale = "SR" }: ProductDetailProps) {
const [selectedImage, setSelectedImage] = useState(0);
const [quantity, setQuantity] = useState(1);
const [isAdding, setIsAdding] = useState(false);
const { addLine, openCart } = useSaleorCheckoutStore();
const localized = getLocalizedProduct(product, locale);
const variant = product.variants?.[0];
// Get all images from media
const images = product.media?.length > 0
? product.media.filter(m => m.type === "IMAGE")
: [{ id: "0", url: "/placeholder-product.jpg", alt: localized.name, type: "IMAGE" as const }];
const handleAddToCart = async () => {
if (!variant?.id) return;
setIsAdding(true);
try {
await addLine(variant.id, quantity);
openCart();
} finally {
setIsAdding(false);
}
};
const isAvailable = variant?.quantityAvailable > 0;
const price = getProductPrice(product);
// Extract short description (first sentence or first 100 chars)
const shortDescription = localized.description
? localized.description.split('.')[0] + '.'
: locale === "EN" ? "Premium natural oil for your beauty routine." : "Premium prirodno ulje za vašu rutinu lepote.";
// Parse benefits from product metadata or use defaults
const benefits = product.metadata?.find(m => m.key === "benefits")?.value?.split(',') || [
locale === "EN" ? "Natural" : "Prirodno",
locale === "EN" ? "Organic" : "Organsko",
locale === "EN" ? "Cruelty-free" : "Bez okrutnosti",
];
return (
<>
{/* Breadcrumb - with proper top padding for fixed header */}
{locale === "EN" ? "Home" : "Početna"}
/
{localized.name}
{/* Product Content */}
{/* Image Gallery - Left Side */}
{/* Thumbnails - Vertical on Desktop, Hidden on Mobile */}
{images.length > 1 && (
{images.map((image, index) => (
setSelectedImage(index)}
className={`relative aspect-square w-full overflow-hidden border-2 transition-colors ${
selectedImage === index
? "border-black"
: "border-transparent hover:border-[#999999]"
}`}
>
))}
)}
{/* Main Image */}
{images[selectedImage] && (
)}
{/* Award Badge - Optional */}
{locale === "EN" ? "Bestseller" : "Najprodavanije"}
{/* Product Info - Right Side */}
{/* Product Name */}
{localized.name}
{/* Short Description */}
{shortDescription}
{/* Price & Rating */}
{price || (locale === "EN" ? "Contact for price" : "Kontaktirajte za cenu")}
{/* Divider */}
{/* Size Selector */}
{product.variants && product.variants.length > 1 && (
{locale === "EN" ? "Size" : "Veličina"}
{product.variants.map((v) => (
{v.name}
))}
)}
{/* Quantity */}
{locale === "EN" ? "Qty" : "Kol"}
setQuantity(Math.max(1, quantity - 1))}
className="w-12 h-12 flex items-center justify-center hover:bg-[#f8f9fa] transition-colors"
disabled={quantity <= 1}
>
{quantity}
setQuantity(quantity + 1)}
className="w-12 h-12 flex items-center justify-center hover:bg-[#f8f9fa] transition-colors"
>
{/* Add to Cart Button - Action verb + value */}
{isAvailable ? (
{isAdding
? (locale === "EN" ? "Adding..." : "Dodavanje...")
: (locale === "EN" ? "Transform My Hair & Skin" : "Transformiši kosu i kožu")
}
) : (
{locale === "EN" ? "Out of Stock" : "Nema na stanju"}
)}
{/* Free Shipping Note - with urgency */}
{locale === "EN"
? "Free shipping on orders over 3,000 RSD"
: "Besplatna dostava za porudžbine preko 3.000 RSD"}
{/* Trust Indicators */}
{locale === "EN" ? "30-Day Guarantee" : "30-dnevna garancija"}
{locale === "EN" ? "Secure Checkout" : "Sigurno plaćanje"}
{locale === "EN" ? "Easy Returns" : "Lak povrat"}
{/* Divider */}
{/* Benefits */}
{locale === "EN" ? "Benefits" : "Prednosti"}
{benefits.map((benefit, index) => (
{benefit.trim()}
))}
{/* Expandable Sections */}
{locale === "EN"
? "Apply a small amount to clean, damp hair or skin. Massage gently until absorbed. Use daily for best results."
: "Nanesite malu količinu na čistu, vlažnu kosu ili kožu. Nežno masirajte dok se ne upije. Koristite svakodnevno za najbolje rezultate."
}
{locale === "EN"
? "100% Pure Natural Oil. No additives, preservatives, or artificial fragrances."
: "100% čisto prirodno ulje. Bez dodataka, konzervansa ili veštačkih mirisa."
}
{/* SKU */}
{variant?.sku && (
SKU: {variant.sku}
)}
{/* Customer Reviews */}
{/* As Featured In - Full Width */}
{/* Before/After Gallery */}
{/* Related Products */}
{relatedProducts && relatedProducts.length > 0 && (
{locale === "EN" ? "You May Also Like" : "Možda će vam se svideti"}
{locale === "EN" ? "Similar Products" : "Slični proizvodi"}
{relatedProducts.filter(p => p && p.id).slice(0, 4).map((relatedProduct, index) => (
))}
)}
{/* Product Benefits */}
{/* Trust Badges */}
{/* How It Works */}
{/* Newsletter */}
>
);
}