<?php

namespace CouponsPlus\App\Coupon\Tree\Factories;

use CouponsPlus\App\Coupon\CardData;
use CouponsPlus\App\Coupon\Tree\Abilities\OfferData;
use CouponsPlus\App\Coupon\Tree\Finders\Sets\OffersDataSet;
use CouponsPlus\App\Offers\Offer;
use CouponsPlus\App\Offers\OffersSet;
use CouponsPlus\App\Offers\OffersSetCollection;

class OfferSetsFromDataFactory
{
    private $offerFromDataFactory;

    public function __construct(ConditionFilterOrOfferFromDataFactory $offerFromDataFactory)
    {
        $this->offerFromDataFactory = $offerFromDataFactory;
    } 

    public function create(OffersSetCollection $offersDataSetCollection) : OffersSetCollection
    {
        return new OffersSetCollection($offersDataSetCollection->getItems()->map(function(OffersDataSet $offersDataSet) : OffersSet {
            (object) $offersSet = new OffersSet($offersDataSet->getItems()->map(function(CardData $offerData) : Offer {
                return $this->offerFromDataFactory->create('offer', $offerData);
            })->asArray());

            $offersSet->setOfferData($offersDataSet);
            $offersSet->getItemsSet()->setItems($offersDataSet->getItemsSet());

            return $offersSet;
        })->asArray());
    }
}