/**
 * @license
 * Copyright 2026 HopCode Team Team
 * SPDX-License-Identifier: Apache-2.0
 */
import { useCallback, useEffect, useState } from 'react';
import { AuthType, isCodingPlanConfig, getCodingPlanConfig, CodingPlanRegion, CODING_PLAN_ENV_KEY, } from '@hoptrendy/hopcode-core';
import { getPersistScopeForModelSelection } from '../../config/modelProvidersScope.js';
import { t } from '../../i18n/index.js';
/**
 * Hook for detecting and handling Coding Plan template updates.
 * Compares the persisted version with the current template version
 * and prompts the user to update if they differ.
 */
export function useCodingPlanUpdates(settings, config, addItem) {
    const [updateRequest, setUpdateRequest] = useState();
    /**
     * Execute the Coding Plan configuration update.
     * Removes old Coding Plan configs and replaces them with new ones from the template.
     * Preserves the user's current model selection if it still exists in the new template.
     * Uses the region from settings.codingPlan.region (defaults to CHINA).
     */
    const executeUpdate = useCallback(async (region = CodingPlanRegion.CHINA) => {
        try {
            const persistScope = getPersistScopeForModelSelection(settings);
            // Get current configs
            const currentConfigs = settings.merged.modelProviders?.[AuthType.USE_OPENAI] || [];
            // Filter out all Coding Plan configs (since they are mutually exclusive)
            // Keep only non-Coding-Plan user custom configs
            const nonCodingPlanConfigs = currentConfigs.filter((cfg) => !isCodingPlanConfig(cfg['baseUrl'], cfg['envKey']));
            // Get the configuration for the current region
            const { template, version } = getCodingPlanConfig(region);
            // Generate new configs from template
            const newConfigs = template.map((templateConfig) => ({
                ...templateConfig,
                envKey: CODING_PLAN_ENV_KEY,
            }));
            // Combine: new Coding Plan configs at the front, user configs preserved
            const updatedConfigs = [
                ...newConfigs,
                ...nonCodingPlanConfigs,
            ];
            // Record the user's current model before the update
            const previousModel = config.getModel();
            const previousModelStillAvailable = newConfigs.some((cfg) => cfg.id === previousModel);
            // Hot-reload model providers configuration first (in-memory only)
            const updatedModelProviders = {
                ...settings.merged.modelProviders,
                [AuthType.USE_OPENAI]: updatedConfigs,
            };
            config.reloadModelProvidersConfig(updatedModelProviders);
            // Refresh auth with the new configuration
            // This validates the configuration before persisting
            await config.refreshAuth(AuthType.USE_OPENAI);
            // Persist to settings only after successful auth refresh
            settings.setValue(persistScope, `modelProviders.${AuthType.USE_OPENAI}`, updatedConfigs);
            // Update the version (single version field for backward compatibility)
            settings.setValue(persistScope, 'codingPlan.version', version);
            // Update the region
            settings.setValue(persistScope, 'codingPlan.region', region);
            const activeModel = config.getModel();
            if (previousModelStillAvailable && activeModel === previousModel) {
                addItem({
                    type: 'info',
                    text: t('{{region}} configuration updated successfully.', {
                        region: t('Alibaba Cloud Coding Plan'),
                    }),
                }, Date.now());
            }
            else {
                addItem({
                    type: 'info',
                    text: t('{{region}} configuration updated successfully. Model switched to "{{model}}".', { region: t('Alibaba Cloud Coding Plan'), model: activeModel }),
                }, Date.now());
            }
            addItem({
                type: 'info',
                text: t('Tip: Use /model to switch between available Coding Plan models.'),
            }, Date.now());
            return true;
        }
        catch (error) {
            const errorMessage = error instanceof Error ? error.message : String(error);
            addItem({
                type: 'error',
                text: t('Failed to update Coding Plan configuration: {{message}}', {
                    message: errorMessage,
                }),
            }, Date.now());
            return false;
        }
    }, [settings, config, addItem]);
    /**
     * Check for version mismatch and prompt user for update if needed.
     * Uses the region from settings.codingPlan.region (defaults to CHINA if not set).
     */
    const checkForUpdates = useCallback(() => {
        const mergedSettings = settings.merged;
        // Get the region (default to CHINA if not set)
        const region = mergedSettings.codingPlan?.region ?? CodingPlanRegion.CHINA;
        // Get the saved version for the current region
        const savedVersion = mergedSettings.codingPlan?.version;
        // If no version is stored, user hasn't used Coding Plan yet - skip check
        if (!savedVersion) {
            return;
        }
        // Get current version for the region
        const currentVersion = getCodingPlanConfig(region).version;
        // Check if version matches
        if (savedVersion !== currentVersion) {
            setUpdateRequest({
                prompt: t('New model configurations are available for {{region}}. Update now?', { region: t('Alibaba Cloud Coding Plan') }),
                onConfirm: async (confirmed) => {
                    setUpdateRequest(undefined);
                    if (confirmed) {
                        await executeUpdate(region);
                    }
                },
            });
        }
    }, [settings, executeUpdate]);
    // Check for updates on mount
    useEffect(() => {
        checkForUpdates();
    }, [checkForUpdates]);
    const dismissCodingPlanUpdate = useCallback(() => {
        setUpdateRequest(undefined);
    }, []);
    return {
        codingPlanUpdateRequest: updateRequest,
        dismissCodingPlanUpdate,
    };
}
//# sourceMappingURL=useCodingPlanUpdates.js.map