import { Check, ChevronLeft, ChevronRight, Loader2, Pin, RotateCw, Search, Star } from 'lucide-react';
import React, { useEffect, useMemo, useState } from 'react';
import { useI18n } from '../../application/i18n/I18nProvider';
import {
canonicalizeEffortEncodedModelId,
filterComposerModels,
resolveComposerEnterModelId,
resolvePinnedAndRecentModels,
type ComposerModelPrefEntry,
type ComposerModelPrefs,
type ComposerPickerModel,
} from '../../infrastructure/ai/composerPicker';
import type { AgentModelPreset, ProviderConfig } from '../../infrastructure/ai/types';
import { formatRelativeTime } from '../AIChatSessionHistoryDrawer';
import { ProviderIconBadge } from '../settings/tabs/ai/ProviderIconBadge';
import { useProviderModelCatalog } from './useProviderModelCatalog';
export const COMPOSER_PROVIDER_PICKER_WIDTH = 260;
export const COMPOSER_MODEL_PICKER_WIDTH = 260;
export interface ComposerModelPickerProps {
providers?: ProviderConfig[];
selectedProviderId?: string;
selectedModelId?: string;
modelPresets?: AgentModelPreset[];
/**
* The host's live model catalog (external SDK agents like OpenCode) is still
* loading. Show the loading row instead of an empty/placeholder list.
*/
loading?: boolean;
/**
* Provenance of the model list behind this picker: a live SDK fetch, the
* persisted last-known-good catalog, or the build-time preset fallback.
* Rendered as a small muted status footer; omit (provider switcher mode)
* to hide the chip.
*/
catalogSource?: 'live' | 'last-known-good' | 'presets';
/** ISO timestamp of the successful fetch backing `catalogSource`. */
catalogFetchedAt?: string;
/**
* The catalog warning text behind the host's retry banner, when present.
* Together with `catalogSource` it decides whether the manual refresh
* button renders: any fetched catalog (live or last-known-good) or a
* degraded preset fallback offers a refresh; a plain preset list does not.
*/
catalogWarning?: string | null;
/**
* Manual refresh action (LKG-first policy: the picker hydrates from disk
* and the user refreshes explicitly). Triggers the host's force refresh.
*/
onRefreshCatalog?: () => void;
prefs: ComposerModelPrefs;
/**
* Offer the "use custom model" action. Disable when the host locks the
* model (e.g. a managed Codex config's `model` field overrides every
* selection), so the action never becomes a silent no-op.
*/
allowCustomEntry?: boolean;
onSelectProviderModel?: (providerId: string, modelId: string, contextWindow?: number) => void;
onSelectModel?: (modelId: string, options?: { custom?: boolean }) => void;
onTogglePinned: (entry: ComposerModelPrefEntry) => void;
}
const rowClassName =
'flex h-8 w-full items-center gap-2 px-2.5 text-left text-[12px] hover:bg-muted/30 transition-colors cursor-pointer';
const SectionLabel: React.FC<{ children: React.ReactNode }> = ({ children }) => (