<?php

namespace CouponsPlus\App\Coupon\Tree\Notifications;

use CouponsPlus\Original\Collections\Collection;

class Notifications
{

    protected Collection $notifications;

    /**
     * Ugh, another singleton until we can refactor this to use dependency injection properly.
     */
    public static function getInstance(): Notifications
    {
        static $instance = null;

        if (is_null($instance)) {
            $instance = new self();
        }

        return $instance;
    }

    public function __construct()
    {
        $this->reset();
    }

    public function addUnique(Notification $notification)
    {
        if (!$this->has($notification)) {
            $this->notifications->add($notification->getId(), $notification);
        }
    }

    public function has(Notification $notification): bool
    {
        return (bool) $this->notifications->find(function (Notification $registeredNotification) use ($notification) {
            return $registeredNotification->getId() === $notification->getId();
        });
    }

    /**
     * Immutable
     */
    public function get(): Collection
    {
        return clone $this->notifications;
    }

    public function reset(): void
    {
        $this->notifications = new Collection([]);
    }
}