<?php
namespace ElixForms\Auth;

use InvalidArgumentException;

/**
 * Enum-like di flag combinabili per gli stati delle istanze elixForms.
 *
 * Esempio: ElixFormsRequestStatus::IN_PROGRESS | ElixFormsRequestStatus::PROCESSED
 */
final class ElixFormsRequestStatus
{
    public const IN_PROGRESS = 1;
    public const SUBMITTED = 2;
    public const PROCESSED = 4;
    public const ALL = self::IN_PROGRESS | self::SUBMITTED | self::PROCESSED;

    private const API_VALUES = [
        self::IN_PROGRESS => 'IN_PROGRESS',
        self::SUBMITTED => 'SUBMITTED',
        self::PROCESSED => 'PROCESSED',
    ];

    private function __construct()
    {
    }

    /**
     * @return array<int,string>
     */
    public static function toApiValues(int $status): array
    {
        if ($status <= 0 || ($status & ~self::ALL) !== 0) {
            throw new InvalidArgumentException('La combinazione di stati elixForms non è valida.');
        }

        $values = [];
        foreach (self::API_VALUES as $flag => $apiValue) {
            if (($status & $flag) === $flag) {
                $values[] = $apiValue;
            }
        }

        return $values;
    }
}
