/home/bdqbpbxa/api-uniferx.goodface.com.ua/app/Http/Resources/Pages/Catalogs/CatalogResource.php
<?php
namespace App\Http\Resources\Pages\Catalogs;

use App\Helpers\ResourceHelper;
use App\Http\Resources\Pages\Cards\MissionCardCollection;
use App\Http\Resources\Pages\Contacts\CountryCollection;
use App\Http\Resources\Pages\Contacts\RegionCollection;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Arr;

/**
 * @OA\Schema(title="CatalogResource",description="Catalog resource",
 *     @OA\Xml(name="CatalogResource")
 * )
 */
class CatalogResource extends JsonResource
{

    /**
     * @OA\Property(property="title", type="string", example=""),
     * @OA\Property(property="type", type="string", example=""),
     * @OA\Property(property="image", type="string", example=""),
     * @OA\Property(property="description", type="string", example=""),
     * @OA\Property(property="file", type="string", example=""),
     * @OA\Property(property="flyers", type="array",
     *       @OA\Items(type="object",ref="#/components/schemas/FlyerResource"),
     *  ),
     */
    public string|null $language;
    public string|null $country;
    public int $id;
    public string|null $title;
    public string|null $type;
    public array|string|null $image;
    public string|null $description;
    public array|string|null $file;
    public object|null $flyers;
//    public array|object|null $data;

    public function language($value): static
    {
        $this->language = $value;
        return $this;
    }

    public function country($value): static
    {
        $this->country = $value;
        return $this;
    }

    public function __construct($resource)
    {
        parent::__construct($resource);
//        dd($resource);
        $this->id = $resource->id;
        $this->title = $resource->title;
        $this->type = $resource->type;
        $this->image = $resource->image;
        $this->description = $resource->description;
        $this->file = $resource->file;
        $this->flyers = $resource->flyers;
//        $this->data = $resource['attributes'];
    }



    public function toArray(Request $request): array
    {
//        dd(json_decode($this->data['title']));
        return [
            'id' => $this->id,
            'title' => ResourceHelper::getValue($this->title ?? null, $this->country, $this->language),
            'type' => $this->type ?? null,
            'image' => ResourceHelper::getImage($this->image ?? null),
            'description' => ResourceHelper::getValue($this->description ?? null, $this->country, $this->language),
            'file' => ResourceHelper::getImage($this->file ?? null),
            'has_flyers' => (boolean)$this->flyers->count(),
        ];
    }

    public static function collection($resource): CatalogCollection
    {
        return new CatalogCollection($resource);
    }
}