<?php

namespace App\Framework\Ulid;

use App\Framework\DateTime\Clock;

final readonly class UlidGenerator
{
    public function generate(Clock $clock): string
    {
        $stringConverter = new StringConverter();

        $timestamp = $clock->now()->getTimestamp() . $clock->now()->getMicrosecond();
        $time = (int)$timestamp /1000;
        #$time = (int)(microtime(true) * 1000);
        $timeBin = substr(pack('J', $time), 2, 6);
        $random  = random_bytes(10);
        $bin     = $timeBin . $random;

        return $stringConverter->encodeBase32($bin);
    }
}
