<?php
declare(strict_types=1);

namespace FreshCloud\Mail\BaseUtils;

final readonly class Encoding
{
    public function __construct(
        public string $charset = 'utf-8',
        public string $transferEncoding = '7bit'
    ) {
        $validEncodings = ['7bit', '8bit', 'quoted-printable', 'base64'];
        if (!in_array($this->transferEncoding, $validEncodings)) {
            throw new InvalidArgumentException("Invalid transfer encoding: {$this->transferEncoding}");
        }
    }

    public static function quotedPrintableDecode(string $input): string
    {
        return quoted_printable_decode($input);
    }

    public static function base64Decode(string $input): string
    {
        return base64_decode($input, true);
    }

    public static function detectCharset(string $bytes): string
    {
        return mb_check_encoding($bytes, 'ASCII') ? 'ASCII' : 'utf-8';
    }
}