<?php

namespace CouponsPlus\App\Coupon\Tree\Factories;

use CouponsPlus\App\Conditions\Abilities\TestableComposite;
use CouponsPlus\App\Coupon\Tree\Abilities\TestableGroupData;
use CouponsPlus\App\Coupon\Tree\TestableGroup;

class TestableGroupFactoryFromData implements TestableGroupFactoryFromDataInterface
{
    private $filterableGroupFactory;
    private $contextFactory;

    public function __construct(FilterableGroupFactoryInterface $filterableGroupFactory, ContextFromTestableGroupDataFactory $contextFactory)
    {
        $this->filterableGroupFactory = $filterableGroupFactory;
        $this->contextFactory = $contextFactory;
    }

    public function create($testableGroupData) : TestableComposite
    {
        if ($testableGroupData->getType()->is('context')) {
            return $this->contextFactory->create($testableGroupData);
        }
        
        return new TestableGroup(
            // if and, an ANDTestable, if or an ORTestable, etc,
            $filterableGroup = $this->filterableGroupFactory->create(
                $testableGroupData->getType(),
                $testableGroupData
            ),
            // here's wehre we'll create the context with the filters/conditions and/or other nested testable groups.
            $testableGroups = $testableGroupData->getChildren()->map([$this, 'create'])
        );
    }
}