<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;
use App\Http\Resources\BlocksCollection as BlocksCollectionResource;
use App\Http\Resources\Blocks as BlocksResource;

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

    protected $isDetail = FALSE;

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

    public function toArray($request)
    {
        $activeLang = getActiveLanguage();
        $url = $activeLang === 'en' ? $this->lang_uri : '/' . $activeLang . $this->lang_uri;

        $out = [
            'id'          => (int)$this->id,
            'title'       => $this->title,
            'slug'        => '/' . $this->lang_slug,
            'language_id' => $this->language_id,
            'url'         => $url,
            'partnerPage' => (boolean)$this->partner_page,
        ];

        if ($this->isDetail) {
            // SEO IMAGE
            $metaImage = $this->metaImage ? getImage($this->metaImage, '1200x630') : NULL;

            $out = array_merge($out, [
                'meta'        => (object)[
                    'title'       => $this->meta_title,
                    'description' => $this->meta_description,
                    'image'       => $metaImage
                ],
                'blocks' => new BlocksCollectionResource($this->blocks),
            ]);
        }

        return $out;
    }
}
