<?php

declare(strict_types=1);

namespace Flasher\Prime\Storage\Filter;

use Flasher\Prime\Notification\Envelope;
use Flasher\Prime\Storage\Filter\Criteria\CriteriaInterface;

final class Filter implements FilterInterface
{
    /**
     * @var CriteriaInterface[]
     */
    private array $criteriaChain = [];

    /**
     * @param Envelope[] $envelopes
     *
     * @return Envelope[]
     */
    public function apply(array $envelopes): array
    {
        foreach ($this->criteriaChain as $criteria) {
            $envelopes = $criteria->apply($envelopes);
        }

        return $envelopes;
    }

    public function addCriteria(CriteriaInterface $criteria): void
    {
        $this->criteriaChain[] = $criteria;
    }
}
