"use client";
import { useMemo } from "react";
import { DEFAULT_MODEL_ID, DISABLED_PROVIDERS, resolveDefaultModelId, modelKey, } from "./registry.js";
import { useModelRegistryContext } from "./ModelRegistryContext.js";
/**
 * Data hook that exposes the platform model registry with grouping
 * and lookup helpers.
 *
 * Pure data layer — no rendering, no side effects. Platform builders
 * who want full control over rendering import this hook and build
 * their own UI.
 *
 * The model data is fetched from the public model registry API by
 * {@link StigmerProvider} and cached in context. During loading,
 * `isLoading` is `true` and `models` is empty.
 *
 * **Modes:**
 * - `options.harness === "cursor"` — Cursor-harness models only
 * - `options.harness === "native"` — native models, excluding disabled providers
 * - `options.harness` omitted — unified: both harnesses, excluding disabled providers
 *
 * @example
 * ```tsx
 * // Unified mode — flat picker with all models
 * const { featured, models, getByKey, isLoading } = useModelRegistry();
 *
 * // Legacy single-harness mode
 * const { models, defaultModel } = useModelRegistry({ harness: "native" });
 * ```
 */
export function useModelRegistry(options) {
    const harness = options?.harness;
    const { models: allModels, isLoading, error, refetch } = useModelRegistryContext();
    return useMemo(() => {
        const isUnified = harness === undefined;
        const { modelId: defaultId } = harness
            ? resolveDefaultModelId(harness, allModels)
            : { modelId: DEFAULT_MODEL_ID };
        const byProvider = new Map();
        const byId = new Map();
        const byCompoundKey = new Map();
        const enabledModels = [];
        const featuredModels = [];
        let defaultModel;
        for (const model of allModels) {
            if (isUnified) {
                if (DISABLED_PROVIDERS.has(model.provider))
                    continue;
            }
            else {
                if (model.harness !== harness)
                    continue;
                if (DISABLED_PROVIDERS.has(model.provider))
                    continue;
            }
            enabledModels.push(model);
            byCompoundKey.set(modelKey(model.harness, model.modelId), model);
            if (!byId.has(model.modelId)) {
                byId.set(model.modelId, model);
            }
            const group = byProvider.get(model.provider);
            if (group) {
                group.push(model);
            }
            else {
                byProvider.set(model.provider, [model]);
            }
            if (model.modelId === defaultId) {
                defaultModel ??= model;
            }
            if (model.featured) {
                featuredModels.push(model);
            }
        }
        const providers = Array.from(byProvider.keys());
        return {
            models: enabledModels,
            byProvider: byProvider,
            defaultModel: defaultModel ?? enabledModels[0],
            getModel: (modelId) => byId.get(modelId),
            providers,
            featured: featuredModels,
            getByKey: (key) => byCompoundKey.get(key),
            isLoading,
            error,
            refetch,
        };
    }, [harness, allModels, isLoading, error, refetch]);
}
//# sourceMappingURL=useModelRegistry.js.map