<?php
declare(strict_types=1);

namespace App;

final class Flash
{
    public function __construct(private SessionManager $session) {}

    public function set(string $type, string $message): void
    {
        $this->session->start();
        $_SESSION['flash'] = [
            'type' => $type,
            'message' => $message,
        ];
    }

    public function get(): ?array
    {
        $this->session->start();
        if (empty($_SESSION['flash']) || !is_array($_SESSION['flash'])) {
            return null;
        }
        $f = $_SESSION['flash'];
        unset($_SESSION['flash']);

        return [
            'type' => (string)($f['type'] ?? 'info'),
            'message' => (string)($f['message'] ?? ''),
        ];
    }
}
