<?php

namespace App\Http\Resources;

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

class Resellers 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)
    {
        $data = $this->block_title;
        if ($data) {
            if (array_key_exists('objectId', $data)) {
                $websiteObject = WebsiteObject::find($data['objectId']);
                if ($websiteObject) {
                    $data['url'] = $websiteObject->uri;
                }
            }
        }
        unset($data['objectId']);

        if ($this->image) {
            $image = getImage($this->image, 'resize181x90');
        } else {
            $image = (object)[];
            $image->url = null;
            $image->alt = null;
        }
        $country = $this->countries ? $this->countries->title : null;
        $out = [
            'id'          => (int)$this->id,
            'title'       => $data,
            'address'     => $this->email_address,
            'phoneNumber' => $this->phone_number,
            'country'     => $country,
            'image'       => $image
        ];

        return $out;
    }
}
