<?php

declare(strict_types=1);

namespace Numinix\SeekmodoWordPress\LocalIndex;

/**
 * Word tokenization for local inverted index.
 */
final class Tokenizer
{
    /**
     * @return list<string>
     */
    public static function tokenize(string $text, ?int $minLength = null): array
    {
        $minLength = $minLength ?? IndexSettings::minWordLength();
        $text = mb_strtolower($text);
        $text = preg_replace('/[^\p{L}\p{N}\s\-]/u', ' ', $text) ?? '';
        $parts = preg_split('/\s+/u', trim($text)) ?: [];
        $tokens = [];
        foreach ($parts as $part) {
            $part = trim($part, '-');
            if ($part === '' || mb_strlen($part) < $minLength) {
                continue;
            }
            if (mb_strlen($part) > 50) {
                $part = mb_substr($part, 0, 50);
            }
            if (Stopwords::isStopword($part)) {
                continue;
            }
            $tokens[] = $part;
        }

        return array_values(array_unique($tokens));
    }

    public static function reverse(string $term): string
    {
        return strrev($term);
    }
}
