/**
 * Provider-dropdown option resolution.
 *
 * Extracted from components/settings.js so the fallback logic can be
 * unit-tested (the settings.js IIFE is otherwise unreachable from tests).
 *
 * Exposes window.LdrProviderOptions. Consumers destructure at the top of
 * their IIFE, e.g.
 *     const { resolveProviderOptions } = window.LdrProviderOptions;
 */
(function() {
    'use strict';

    /**
     * Resolve the model-provider dropdown options with a single, explicit
     * fallback chain:
     *   1. the backend's auto-discovered list (/settings/api/available-models),
     *   2. the llm.provider setting's own options, then
     *   3. the static fallback list (offline).
     * Pure function — callers pass the current state so it stays testable and
     * can be re-evaluated lazily (on dropdown open) regardless of fetch/setup
     * ordering.
     *
     * @param {Array<{value: string, label: string}>} discoveredProviderOptions
     *   Options from the auto-discovery endpoint (preferred).
     * @param {Array<{key: string, options?: Array}>} allSettings
     *   The loaded settings list; the llm.provider entry's options are the
     *   secondary fallback.
     * @param {Array<{value: string, label: string}>} staticProviders
     *   Hardcoded last-resort list used when the page is offline.
     * @returns {Array<{value: string, label: string}>}
     */
    function resolveProviderOptions(
        discoveredProviderOptions,
        allSettings,
        staticProviders,
    ) {
        if (
            Array.isArray(discoveredProviderOptions) &&
            discoveredProviderOptions.length > 0
        ) {
            return discoveredProviderOptions;
        }
        if (Array.isArray(allSettings)) {
            const providerSetting = allSettings.find(
                s => s && s.key === 'llm.provider',
            );
            if (
                providerSetting &&
                Array.isArray(providerSetting.options) &&
                providerSetting.options.length > 0
            ) {
                return providerSetting.options.map(opt => ({
                    value: opt.value,
                    label: opt.label,
                }));
            }
        }
        return staticProviders;
    }

    window.LdrProviderOptions = {
        resolveProviderOptions,
    };
})();
