<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

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

    public function toArray($request)
    {
        $out = [
            'id' => (int)$this->id,
            'type' => $this->type,
            'slug' => $this->lang_slug
        ];

        // Specific block data per type
        $out = array_merge($out, $this->_getBlockData());

        return $out;
    }

    private function _getBlockData()
    {
        $out = [];
        switch ($this->type) {
            case 'blockDocumentLists':
                $out = $this->_getBlockDocumentLists();
                break;
            case 'blockTableEditor':
                $out = $this->_getBlockTableEditor();
                break;
        }
        return $out;
    }

    /**************
     * Blocks
     **************/

    public function _getBlockDocumentLists(): array
    {
        return [
            'blockTitle' => $this->block_title,
            'title' => $this->title,
            'documentLists' => DocumentListsResource::collection($this->documentLists),
        ];
    }

    public function _getBlockTableEditor(): array
    {
        return [
            'blockTitle' => $this->block_title,
            'title' => $this->title,
            'wysiwyg' => $this->wysiwyg,
        ];
    }
}
