<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

class Locations extends JsonResource
{
    /**
     * Accesses constructor.
     * @param $resource
     */
    public function __construct($resource)
    {
        $this->resource = $resource;
    }

    public function toArray($request)
    {
        if ($this->icon) {
            $icon = getImage($this->icon);
        } else {
            $icon = (object)[
                'url' => null,
                'alt' => null,
            ];
        }

        $out = [
            'id'          => (int) $this->id,
            'name'        => $this->title,
            'language_id' => $this->language_id,
            'icon'        => $icon
        ];

        $out['children'] = [];

        // Get child info
        // if there is any
        if ($this->children) {
            foreach ($this->children as $child) {
                $out['children'][] = new Themes($child);
            }
        }

        return $out;
    }
}
