<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;
use App\Http\Resources\SimpleLinksCollection;
use App\Models\GenericBackoffice\WebsiteObject;

class HighlightsCommunity extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */

    public function __construct($resource)
    {
        $this->resource = $resource;
    }

    public function toArray($request)
    {
        if ($this->highlightImage) {
            $image = getImage($this->highlightImage, '480x608');
        } else {
            $image = (object)[];
            $image->url = NULL;
            $image->alt = NULL;
        }

        $blockTitle = $this->block_title;
        if ($blockTitle) {
            if (array_key_exists('objectId', $blockTitle)) {
                $websiteObject = WebsiteObject::find($blockTitle['objectId']);
                if ($websiteObject) {
                    $blockTitle['url'] = $websiteObject->uri;
                }
            }
        }
        unset($blockTitle['objectId']);

        $out = [
            'id'          => (int)$this->id,
            'title'       => $blockTitle,
            'authors'     => new SimpleLinksCollection($this->authors),
            'subTitle'    => $this->sub_title,
            'info'        => $this->description,
            'image'       => $image,
        ];

        return $out;
    }
}
