<?php

namespace CouponsPlus\App\Coupon\Tree\Factories;

use CouponsPlus\App\Coupon\Tree\Abilities\CouponModelFactory;
use CouponsPlus\Original\Collections\Collection;

class ModelFactories
{
    protected $factories;

    public function __construct()
    {
        /**
         * hard coded for now
         */
        $this->factories = new Collection([
            new TreeCouponModelFactory,
            new RowsCouponModelFactory
        ]);    
    } 

    /**
     * Defaults to the rows coupon model factory for backwards compatibility
     */
    public function create(string $type) : CouponModelFactory
    {
        return $this->factories->find(function(CouponModelFactory $couponModelFactory) use($type) : bool {
            return $couponModelFactory->getModelType() === $type;
        }) ?: new RowsCouponModelFactory;
    }

    public function getTypes() : Collection
    {
        return $this->factories->map(function(CouponModelFactory $couponModelFactory) : string {
            return $couponModelFactory->getModelType();
        });
    }
}