<?php

namespace CouponsPlus\App\Coupon\Tree\Finders;

use CouponsPlus\App\Conditions\ItemsSet;
use CouponsPlus\App\Coupon\CartComponentsSet;
use CouponsPlus\App\Coupon\Tree\Abilities\Branch;
use CouponsPlus\App\Coupon\Tree\Abilities\FindableOffersFromBranch;
use CouponsPlus\Original\Collections\Collection;

class OffersFinderFromBranch implements FindableOffersFromBranch
{
    private $finders;

    public function __construct(
        /** @var FindableOffersFromBranch&OverloadableFindableOffersFromBranch */
        FindableOffersFromBranch ...$finders,
    ) {
        $this->finders = new Collection($finders);
    }

    /**
     * Throws exception if no finder is compatible
     */
    public function findOffers(ItemsSet $itemsSet, Branch $branch): CartComponentsSet
    {
        /** @var FindableOffersFromBranch|null */
        (object) $finder = $this->finders->find(
            function(FindableOffersFromBranch $finder) use ($branch) : bool {
                 return $finder->canFindFrom($branch);
            }
        );

        return $finder->findOffers($itemsSet, $branch);
    } 
}