<?php

namespace Modules\Product\Resources\Api;

use Illuminate\Http\Resources\Json\JsonResource;
use Modules\Category\Resources\Api\CategoryResource;

class ProductResource extends JsonResource
{

    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        $return = [
            'id' => (int) $this->id,
            'code' => (int) $this->code,
            'count' => (int) $this->count,
            'cost' => (double) $this->cost,
            'image' => $this->image ? asset($this->image) : null,
            'category'=>$this->category?new CategoryResource($this->category):null
        ];
        if (!$this['ar']) {
            $return['name'] = $this->currentDescription->title ?? "";
        }
        if ($this['ar']) {
            $return['name'] = $this['ar']->title ?? "";
            $return['name_en'] = $this['en']->title ?? "";
        }
        return $return;
    }

}
