<?php

namespace App\Http\Resources;

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

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)
    {
        $defaultLang = getDefaultLanguage();
        $activeLang = getActiveLanguage();
        $url = $activeLang === $defaultLang ? $this->lang_uri : '/' . $activeLang . $this->lang_uri;

        $out = [
            'id'          => (int)$this->id,
            'title'       => $this->title,
            'slug'        => '/' . $this->lang_slug,
            'language_id' => $this->language_id,
            'languages'   => new WebsiteObjectLanguages($this->getActiveWebsiteEntry()->languages),
            'url'         => $url,
            'content'     => $this->content,
            'blocks'      => new BlocksCollectionResource($this->blocks)
        ];

        //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,
                    'keywords'    => $this->meta_keywords,
                    'image'       => $metaImage
                ]
            ]);
        //}

        return $out;
    }
}
