/home/bdqbpbxa/api-uniferx.goodface.com.ua/app/Http/Resources/Pages/Cards/MissionCardResource.php
<?php
namespace App\Http\Resources\Pages\Cards;

use App\Helpers\ResourceHelper;
use App\Http\Resources\Pages\Contacts\CountryCollection;
use App\Http\Resources\Pages\Contacts\RegionCollection;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Arr;

/**
 * @OA\Schema(title="MissionCardResource",description="MissionCard resource",
 *     @OA\Xml(name="MissionCardResource")
 * )
 */
class MissionCardResource extends JsonResource
{

    /**
     * @OA\Property(property="title", type="string", example="title"),
     * @OA\Property(property="text", type="string", example="text"),
     */
    public string|null $language;
    public string|null $country;
    public array|object|null $data;

    public function language($value): static
    {
        $this->language = $value;
        return $this;
    }

    public function country($value): static
    {
        $this->country = $value;
        return $this;
    }

    public function __construct($resource)
    {
        parent::__construct($resource);
//        dd($resource);
        $this->data = $resource['attributes'];
    }



    public function toArray(Request $request): array
    {
//        dd(json_decode($this->data['title']));
        return [
            'title' => ResourceHelper::getValue(json_decode($this->data['title']) ?? null, $this->country, $this->language),
            'text' => ResourceHelper::getValue($this->data['text'] ?? null, $this->country, $this->language),
        ];
    }

    public static function collection($resource): MissionCardCollection
    {
        return new MissionCardCollection($resource);
    }
}