<?php

namespace CouponsPlus\App\Coupon\Tree;

use CouponsPlus\App\Conditions\Filter;
use CouponsPlus\App\Conditions\ItemsSet;
use CouponsPlus\Original\Collections\Collection;
use CouponsPlus\App\Coupon\Tree\Abilities\FilterableGroup;
use CouponsPlus\App\Conditions\Abilities\TestableComposite;

class IntersectionTestableGroup implements FilterableGroup
{
    public function filter(Collection $testables, ItemsSet $passedItems): ItemsSet
    {
        if ($testables->haveNone()) {
            return $passedItems;
        }

        return new ItemsSet(
            $testables->map(function(TestableComposite $filter) use ($passedItems) : Collection {
                return $filter->filterSet($passedItems)->getItems();
            })->intersect()->asArray()
        );
    }
}