<?php

namespace CouponsPlus\App\Coupon\Tree\Testable;

use CouponsPlus\App\Conditions\ItemsSet;
use CouponsPlus\Original\Collections\Collection;
use CouponsPlus\App\Coupon\Tree\Abilities\NodeData;
use CouponsPlus\App\Coupon\Tree\Abilities\NodeDatas;
use CouponsPlus\App\Coupon\Tree\Finders\NodeDatasCollection;
use CouponsPlus\App\Coupon\Tree\Abilities\NodeDataFilterableSet;

class PathRecorderFilterableSet extends SetFiltererFromNodeDataDecorator implements NodeDataFilterableSet
{
    public function filterSet(ItemsSet $itemsSet, NodeData $nodeData, NodeDatas $relatedNodeDatas): ItemsSet
    {
        $originalPath = $itemsSet->getSourcePath();

        /**
         * Here get are giving in a path with steps and are getting back a path with NO steps!
         * Because there are all sorts of ways in which an itemsset may be created, let's just manually add the source paths
         * after filtering.
         */
        (object) $filteredSet = $this->setFilterer->filterSet(
            $itemsSet,
            $nodeData,
            $relatedNodeDatas
        );

        $filteredSet->setSourcePath($originalPath);
        // this will add the source as a step to the path if we had previous steps, this will be added after them
        // it will ONLY add a step if there's an actual source, if not, it will do nothing
        $filteredSet->commitSourceToPath();

        return $filteredSet;
    }
}