import type {
  StorefrontCartItem,
  StorefrontProductCard,
  StorefrontProductDetail,
  StorefrontWishlistItem,
} from "@/types/storefront";

export function buildWishlistItemFromProductCard(
  product: StorefrontProductCard,
): StorefrontWishlistItem {
  return {
    id: product.id,
    productId: product.id,
    slug: product.slug,
    type: product.type,
    name: product.name,
    href: product.href,
    label: product.brand?.name || product.category?.name || "Proizvod",
    image: product.featuredImage,
    price: product.price,
    regularPrice: product.regularPrice,
    salePrice: product.salePrice,
  };
}

export function buildWishlistItemFromProductDetail(
  product: StorefrontProductDetail,
): StorefrontWishlistItem {
  return {
    id: product.id,
    productId: product.id,
    slug: product.slug,
    type: product.type,
    name: product.name,
    href: product.href,
    label: product.brand?.name || product.category?.name || "Proizvod",
    image: product.featuredImage,
    price: product.price,
    regularPrice: product.regularPrice,
    salePrice: product.salePrice,
  };
}

export function buildWishlistItemFromCartItem(
  cartItem: StorefrontCartItem,
): StorefrontWishlistItem {
  return {
    id: cartItem.variationId ?? cartItem.productId,
    productId: cartItem.productId,
    variationId: cartItem.variationId,
    slug: cartItem.href.split("/").filter(Boolean).at(-1) || String(cartItem.productId),
    type: cartItem.variationId ? "variation" : "simple",
    name: cartItem.name,
    href: cartItem.href,
    label: cartItem.label,
    image: cartItem.image,
    price: cartItem.unitPrice,
    regularPrice: cartItem.regularPrice,
    salePrice: cartItem.isOnSale ? cartItem.unitPrice : undefined,
  };
}
