<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;

class PageResource 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 = LaravelLocalization::getCurrentLocale();
        $url = $activeLang === 'en' ? $this->uri : '/' . $activeLang . $this->uri;
        $out = [
            'id'          => (int)$this->id,
            'title'       => $this->title,
            'slug'        => $this->slug,
            'url'         => $url,
            'languageId'  => $this->language_id,
            'languages'   => new WebsiteObjectLanguageResource($this->getActiveWebsiteEntry()->languages),
            'content'     => $this->content,
            'blocks'      => new BlockCollectionResource($this->blocks),
            'template'    => $this->template
        ];

        /* $metaImage = $this->metaImage ? getImage($this->metaImage, 'w1200') : NULL;

        $out = array_merge($out, [
                'meta'        => (object)[
                'title'       => $this->meta_title,
                'description' => $this->meta_description,
                'keywords'    => $this->meta_keywords,
                'image'       => $metaImage
            ]
        ]); */

        return $out;
    }
}
