<?php
declare(strict_types=1);

namespace App\Framework\SyntaxHighlighter;

final readonly class Tokenizer {
    public function __construct(
        private TokenMapper $tokenMapper = new TokenMapper()
    ) {}

    public function tokenize(string $code): array {
        $phpTokens = token_get_all($code);
        $tokens = [];
        $line = 1;
        $column = 1;

        foreach ($phpTokens as $phpToken) {
            if (is_array($phpToken)) {
                $tokenType = $this->tokenMapper->getTokenType($phpToken[0]);
                $value = $phpToken[1];
                $tokenLine = $phpToken[2] ?? $line;

                $tokens[] = new Token($tokenType, $value, $tokenLine, $column);

                // Zeilen- und Spaltenzählung aktualisieren
                $newlines = substr_count($value, "\n");
                if ($newlines > 0) {
                    $line += $newlines;
                    $column = strlen(substr($value, strrpos($value, "\n") + 1)) + 1;
                } else {
                    $column += strlen($value);
                }
            } else {
                // Einzelne Zeichen
                $tokens[] = new Token(TokenType::DEFAULT, $phpToken, $line, $column);

                if ($phpToken === "\n") {
                    $line++;
                    $column = 1;
                } else {
                    $column++;
                }
            }
        }

        return $tokens;
    }
}
