<?php

declare(strict_types=1);

namespace App\Framework\OutputBuffer;

/**
 * Collection of output buffer flags
 *
 * Combines multiple OutputBufferFlag values into a bitmask
 */
final readonly class OutputBufferFlags
{
    /** @var array<OutputBufferFlag> */
    private array $flags;

    /**
     * @param OutputBufferFlag ...$flags Variadic flags
     */
    public function __construct(
        OutputBufferFlag ...$flags
    ) {
        $this->flags = $flags;
    }

    /**
     * Create standard flags (CLEANABLE | FLUSHABLE | REMOVABLE)
     */
    public static function standard(): self
    {
        return new self(OutputBufferFlag::STDFLAGS);
    }

    /**
     * Create default flags (CLEANABLE)
     */
    public static function default(): self
    {
        return new self(OutputBufferFlag::CLEANABLE);
    }

    /**
     * Create cleanable flags
     */
    public static function cleanable(): self
    {
        return new self(OutputBufferFlag::CLEANABLE);
    }

    /**
     * Create cleanable and flushable flags
     */
    public static function cleanableAndFlushable(): self
    {
        return new self(
            OutputBufferFlag::CLEANABLE,
            OutputBufferFlag::FLUSHABLE
        );
    }

    /**
     * Convert flags to PHP bitmask
     */
    public function toBitmask(): int
    {
        if (empty($this->flags)) {
            return 0;
        }

        return array_reduce(
            $this->flags,
            fn(int $carry, OutputBufferFlag $flag) => $carry | $flag->value,
            0
        );
    }

    /**
     * Check if a specific flag is set
     */
    public function has(OutputBufferFlag $flag): bool
    {
        return in_array($flag, $this->flags, true);
    }

    /**
     * Get all flags
     *
     * @return array<OutputBufferFlag>
     */
    public function toArray(): array
    {
        return $this->flags;
    }
}
