*/ public function toArray(Request $request): array { $attachment = $this->attachments->first() ?? $this->variants ->flatMap(fn (Variant $variant) => $variant->attachments) ->first(); return [ 'id' => $this->id, 'type' => $this->type->value, 'nombre' => $this->nombre, 'descripcion' => $this->descripcion, 'precio' => $this->precio, 'image' => $attachment?->getTemporaryUrl(1440), 'stock_tecnico' => $this->availableStock(), 'variants' => $this->variants ->map(fn (Variant $variant): array => [ 'id' => $variant->id, 'stock_tecnico' => $this->inventory_policy === InventoryPolicy::Unlimited ? null : $variant->inventory?->availableStock(), 'values' => $variant->definitions ->mapWithKeys(fn ($definition) => [ $definition->itemAttribute?->attribute?->codigo => $definition->value, ]) ->filter(fn ($value, $key): bool => $key !== null), ]) ->values(), ]; } }