<?php

declare(strict_types=1);

namespace App\Config;

/**
 * YAML-backed stop-word configuration.
 *
 * This class intentionally has no PHP fallback list. The complete list lives in
 * config/retriex/language.yaml.
 */
final class StopWordsConfig
{
    /**
     * @param array<string, mixed> $config
     */
    public function __construct(private readonly array $config)
    {
    }

    /**
     * @return string[]
     */
    public function getStopWords(): array
    {
        return $this->requiredStringList('words');
    }

    /**
     * @return string[]
     */
    private function requiredStringList(string $key): array
    {
        if (!array_key_exists($key, $this->config)) {
            throw new \InvalidArgumentException(sprintf('Missing required RetrieX stopwords config key "%s".', $key));
        }

        $value = $this->config[$key];

        if (!is_array($value)) {
            throw new \InvalidArgumentException(sprintf('RetrieX stopwords config key "%s" must be a list.', $key));
        }

        $out = [];
        foreach ($value as $item) {
            if (!is_scalar($item)) {
                continue;
            }

            $item = trim((string) $item);
            if ($item === '') {
                continue;
            }

            if (!in_array($item, $out, true)) {
                $out[] = $item;
            }
        }

        if ($out === []) {
            throw new \InvalidArgumentException(sprintf('RetrieX stopwords config key "%s" must not be empty.', $key));
        }

        return $out;
    }
}
