<?php

namespace Modules\TPConnector\Transformers;

use Illuminate\Http\Request;
use App\Utils\Util;
use Illuminate\Http\Resources\Json\ResourceCollection;

class SellAPIResource extends ResourceCollection
{
    /**
     * The "data" wrapper that should be applied.
     *
     * @var string
     */
    public static $wrap = null;

    /**
     * Transform the resource into an array.
     *
     * @param Request
     * @return array
     */
    public function toArray($request)
    {
        $taxes = $this->collection->pluck('tax')->unique()->filter()->values();
        $linesTaxes = $this->collection->pluck('sell_lines')->collapse()->pluck('line_tax')->unique()->filter()->values();
        $variations = $this->collection->pluck('sell_lines')->collapse()->pluck('variations')->unique()->filter()->values();
        $products = $this->collection->pluck('sell_lines')->collapse()->pluck('product')->unique()->filter()->values();
        $categories = $products->pluck('category')->unique()->filter()->values();
        $subCategories = $products->pluck('sub_category')->unique()->filter()->values();
        $taxes->merge($linesTaxes)->unique()->values();
        return [
            'data' => \Modules\TPConnector\Transformers\SellResource::collection($this->collection),
            'taxes' => $taxes->map->only(['id', 'name', 'amount']),
            'variations' => $variations->map->only(['id', 'name', 'sub_sku']),
            'products' => $products->map->only(['id', 'name', 'product_custom_field1', 'product_custom_field2', 'type', 'category_id', 'sub_category_id']),
            'categories' => $categories->map->only(['id', 'name']),
            'sub_categories' => $subCategories->map->only(['id', 'name', 'parent_id']),
        ];
    }
}
