<?php

declare(strict_types=1);

namespace App\Http\Resources;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class MenuResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @return array<string, mixed>
     */
    public function toArray(Request $request): array
    {
        return [
            'title' => $this->title,
            'url' => $this->url,
            'order_id' => $this->order_id,
            'icon' => $this->icon ? url($this->icon) : null,
            'category_id' => $this->category_id,
            'children' => self::collection($this->whenLoaded('childrenRecursive')),
        ];
    }
}
