<?php

namespace ZenoChatbot\Services\Chat;

use ZenoChatbot\Services\Chat\Contracts\LanguageResolverInterface;

class DefaultLanguageResolver implements LanguageResolverInterface
{
    /**
     * Resolve language to array of language codes to search
     * Supports fallback (e.g., 'en-GB' falls back to 'en')
     *
     * @param string $language Language code
     * @return array Array of language codes to search
     */
    public function resolve(string $language): array
    {
        $languages = [$language];

        // Add base language as fallback (e.g., 'en-GB' -> 'en')
        if (str_contains($language, '-')) {
            $baseLang = explode('-', $language)[0];
            $languages[] = $baseLang;
        }

        // Always include 'en' as final fallback if not already present
        if (!in_array('en', $languages)) {
            $languages[] = 'en';
        }

        return array_unique($languages);
    }
}
