/home/bdqbpbxa/api-uniferx.goodface.com.ua/app/Http/Resources/Pages/LegalInfoResource.php
<?php

namespace App\Http\Resources\Pages;

use App\Helpers\ResourceHelper;
use App\Http\Resources\Pages\TermsConditions\BlockCollection;
use App\Models\Post;
use App\Models\Product;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

/**
 * @OA\Schema(title="LegalInfoResource", description="LegalInfo resource",
 *     @OA\Xml(name="LegalInfoResource")
 * )
 */
class LegalInfoResource extends JsonResource
{
    /**
     * @OA\Property(property="fs_title", type="string", example="<p>Terms &amp; Conditions</p>"),
     * @OA\Property(property="fs_background", type="string", example="https://uniferx-backend.test/storage/image.png"),
     * @OA\Property(property="fs_button", type="string", example="Read more"),
     * @OA\Property(property="blocks", type="array",
     *       @OA\Items(type="object",ref="#/components/schemas/BlockResource"),
     *  ),
     */
    public string|null $name;
    public string|null $language;
    public string|null $country;
    public array|null $data;

    public function __construct($resource, $language, $country)
    {
        parent::__construct($resource);
        $this->name = $resource->name;
        $this->language = $language;
        $this->country = $country;
        $this->data = $resource->data;
    }

    /**
     * Transform the resource into an array.
     *
     * @return array<string, mixed>
     */
    public function toArray(Request $request): array
    {
        return [
            'fs_title' => ResourceHelper::getValue($this->data['fs_title'] ?? null, $this->country, $this->language),
            'fs_background' => ResourceHelper::getImage($this->data['fs_title_background'] ?? null),
            'fs_button' => ResourceHelper::getValue($this->data['fs_view_button'] ?? null, $this->country, $this->language),
            'blocks' => isset($this->data['blocks']) ? BlockCollection::make( $this->data['blocks'] ?? null)->language($this->language)->country($this->country) : [],
        ];
    }

}