<?php

declare(strict_types=1);

namespace App\Framework\Ulid;

use App\Framework\Core\Encoding\Base32Alphabet;
use App\Framework\Core\Encoding\Base32Encoder;

/**
 * ULID String Converter using Crockford's Base32 alphabet
 *
 * Migrated to use the framework's Base32Encoder with Crockford alphabet
 * for consistency and better maintainability.
 */
final readonly class StringConverter
{
    /**
     * Encodes a binary string into a Base32 encoded string using Crockford's alphabet
     *
     * @param string $binary The binary string to be encoded.
     * @return string The Base32 encoded string.
     */
    public function encodeBase32(string $binary): string
    {
        return Base32Encoder::encodeCrockford($binary);
    }

    /**
     * Decodes a Base32-encoded string into its binary representation
     *
     * @param string $base32 The Base32-encoded string to decode.
     * @return string The binary representation of the decoded string.
     * @throws \InvalidArgumentException If the provided string contains invalid Base32 characters.
     */
    public function decodeBase32(string $base32): string
    {
        return Base32Encoder::decodeCrockford($base32);
    }

    /**
     * Validate if string is valid Crockford Base32
     */
    public function isValidBase32(string $base32): bool
    {
        return Base32Alphabet::CROCKFORD->isValidEncoded($base32);
    }

    /**
     * Get the Crockford Base32 alphabet used by ULID
     */
    public function getAlphabet(): string
    {
        return Base32Alphabet::CROCKFORD->getAlphabet();
    }
}
