<?php

namespace CouponsPlus\App\Coupon\Tree\Finders;

use CouponsPlus\App\Coupon\Tree\Abilities\NodeData;
use CouponsPlus\App\Coupon\Tree\Abilities\NodeDatas;
use CouponsPlus\Original\Collections\Collection;

/**
 * I know Data is plural. In my mind, adding an 's' at the end makes it easier to understand
 */
class NodeDatasCollection implements NodeDatas
{
    protected $ignoredNodes;
    protected $nodes;

    public function __construct(Collection $nodes)
    {
        $this->nodes = $nodes;
        $this->ignoredNodes = new Collection([]);
    } 

    /**
     * THIS SHOULD ALWAYS RETURN A COPY OF THE COLLECTION
     * IT IS MEANT TO BE INMUTABLE!
     *
     * Take this in mind if you change the implementation (eg: filter the nodes in addToIgnoreList() for performance idk)
     */
    public function get() : Collection
    {
        return $this->nodes->filter(function(NodeData $nodeData) : bool {
            return !$this->ignoredNodes->strictlyHave($nodeData);
        })->resetKeys();
    } 

    public function addToIgnoreList(NodeData $node)
    {
        $this->ignoredNodes->push($node);
    }
}