<?php

namespace App\Classes\Modules\Companies\Services;


use App\Http\Resources\CurrencyResource;
use App\Models\Currency;
use Illuminate\Support\Collection;

class FetchesCompanyServices
{

    public function getServices(Collection $services){
        return $services->map(function($service){
            $currencies = collect($service->getConfigurations()->detail->currencies)->pluck('id');
            if(!empty($service->getCustomOptions())){
                $currencies = $currencies->merge(collect($service->getCustomOptions())->flatMap(function($configurations){
                    return collect($configurations->getConfigurationValue('currencies'))->pluck('id');
                }));
            }

            return [
                'id' => $service->getService()->id,
                'name' => $service->getService()->name,
                'currencies' => CurrencyResource::collection(Currency::whereIn('id', $currencies->filter()->unique()->toArray())->get())
            ];
        });
    }
}