<?php

namespace CouponsPlus\App\Coupon\Tree;

use CouponsPlus\App\Conditions\ItemsSet;
use CouponsPlus\App\Conditions\Abilities\TestableComposite;

use function CouponsPlus\App\Coupon\Tree\TestableHooks\useBranch;

/**
 * Hooks to the current branch for subset sum calculations per branch
 */
class BranchSubsetSumTestableGroup implements TestableComposite
{

    protected TestableGroup $testableGroup;

    public function __construct(TestableGroup $testableGroup)
    {
        $this->testableGroup = $testableGroup;
    }

    public function filterSet(ItemsSet $itemsSet): ItemsSet
    {
        (object) $branch = useBranch();

        if (!$branch) {
            return $this->testableGroup->filterSet($itemsSet);
        }

        return $branch->filterItemsSetContextQuantityBeforePassingToBranch($itemsSet, $this->testableGroup);
    }
}