<?php

namespace App\Http\Resources\Models;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class ItemProtoResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @return array<string, mixed>
     */
    public function toArray(Request $request): array
    {
        $maxLimits = 2;
        $maxApplies = 3;

        return [
            'vnum' => $this->vnum,
            'name' => $this->locale_name,
            'type' => $this->type->getLabel(),
            'subtype' => $this->subtype,
            // 'refined_vnum',
            'refine_set' => new ItemProtoRefineResource($this->refineSet),
            'magic_pct' => $this->magic_pct,
            // Limits
            'limits' => collect(range(0, $maxLimits - 1))
                ->map(fn ($index) => [
                    'type' => $this->{"limittype$index"},
                    'value' => $this->{"limitvalue$index"},
                ])
                ->filter(fn ($row) => $row['type']->value !== 0)
                ->map(fn ($row) => ['type' => $row['type']->getLabel(), 'value' => $row['value']]),
            // Applies
            'applies' => collect(range(0, $maxApplies - 1))
                ->map(fn ($index) => [
                    'type' => $this->{"applytype$index"},
                    'value' => $this->{"applyvalue$index"},
                ])
                ->filter(fn ($row) => $row['type']->value !== 0)
                ->map(fn ($row) => ['type' => $row['type']->getLabel(), 'value' => $row['value']]),
        ];
    }
}
