<?php

namespace TPS\API\Http\Resources\Checkout;

use Illuminate\Http\Resources\Json\JsonResource;
use TPS\API\Http\Resources\Catalog\Product as ProductResource;
use Webkul\Marketplace\Repositories\ProductRepository;

class CartItemResource extends JsonResource
{
    public function __construct($resource)
    {
//        $this->sellerProductRepository = app(ProductRepository::class);
        parent::__construct($resource);
    }

    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
//        $seller = $this->sellerProductRepository->getSellerByProductId($this->product_id);
        return [
            'id'                            => $this->id,
//            'vendor'                        => $this->when($seller,$seller->shop_title),
            'quantity'                      => $this->quantity,
            'name'                          => $this->name,
            'base_total_weight'             => $this->base_total_weight,
            'price'                         => $this->price,
            'formatted_price'               => core()->formatPrice($this->base_price, $this->cart->cart_currency_code),
            'custom_price'                  => $this->custom_price,
            'total'                         => $this->total,
            'formatted_total'               => core()->formatPrice($this->base_total, $this->cart->cart_currency_code),
            'discount_percent'              => $this->discount_percent,
            'discount_amount'               => $this->discount_amount,
            'formatted_discount_amount'     => core()->formatPrice($this->base_discount_amount, $this->cart->cart_currency_code),
            'additional'                    => is_array($this->resource->additional)
                ? $this->resource->additional
                : json_decode($this->resource->additional, true),
            'child'                         => new self($this->child),
            'product'                       => $this->when($this->product_id, new ProductResource($this->product)),
        ];
    }
}
