<?php

namespace App\Support;

/**
 * Safe keyword matching for user-entered labels (avoids "pet" matching "carpet", etc.).
 */
final class FinancialKeywordMatch
{
    /**
     * @param  list<string>  $needles
     */
    public static function containsAny(string $haystack, array $needles): bool
    {
        $h = BrandResolver::normalize($haystack);
        if ($h === '') {
            return false;
        }
        foreach ($needles as $needle) {
            $n = BrandResolver::normalize($needle);
            if ($n === '') {
                continue;
            }
            if (str_contains($n, ' ')) {
                if (str_contains($h, $n)) {
                    return true;
                }

                continue;
            }
            if ((bool) preg_match('/\b'.preg_quote($n, '/').'\b/u', $h)) {
                return true;
            }
        }

        return false;
    }
}
