// ---------------------------------------------------------------------------
// Key utility
// ---------------------------------------------------------------------------
/**
 * Builds the map key for an MCP server from its resource reference.
 *
 * Format: `"org/slug"`. Used as the entry key in
 * {@link McpServerSetupState} and the `key` field on every per-entry
 * {@link McpServerSetupAction}.
 */
export function toServerKey(ref) {
    return `${ref.org}/${ref.slug}`;
}
// ---------------------------------------------------------------------------
// Initial state
// ---------------------------------------------------------------------------
export const INITIAL_MCP_SETUP_STATE = {};
// ---------------------------------------------------------------------------
// Reducer
// ---------------------------------------------------------------------------
export function mcpServerSetupReducer(state, action) {
    switch (action.type) {
        case "ADD_SERVER":
            return { ...state, [action.key]: { status: "loading", error: null } };
        case "RESOLVE_NEEDS_SETUP": {
            const entry = state[action.key];
            if (entry?.status !== "loading")
                return state;
            return {
                ...state,
                [action.key]: {
                    status: "needsSetup",
                    mcpServer: action.mcpServer,
                    missingVariables: action.missingVariables,
                    discoveredTools: action.discoveredTools,
                    toolApprovals: action.toolApprovals,
                    error: null,
                },
            };
        }
        case "RESOLVE_READY": {
            const entry = state[action.key];
            if (entry?.status !== "loading")
                return state;
            return {
                ...state,
                [action.key]: {
                    status: "ready",
                    mcpServer: action.mcpServer,
                    discoveredTools: action.discoveredTools,
                    toolApprovals: action.toolApprovals,
                    enabledTools: action.enabledTools,
                    error: null,
                },
            };
        }
        case "POOL_RESOLVE": {
            const entry = state[action.key];
            if (entry?.status !== "needsSetup")
                return state;
            if (action.missingVariables.length === 0) {
                return {
                    ...state,
                    [action.key]: {
                        status: "ready",
                        mcpServer: entry.mcpServer,
                        discoveredTools: entry.discoveredTools,
                        toolApprovals: entry.toolApprovals,
                        enabledTools: action.enabledTools,
                        error: null,
                    },
                };
            }
            return {
                ...state,
                [action.key]: {
                    ...entry,
                    missingVariables: action.missingVariables,
                },
            };
        }
        case "SUBMIT_START": {
            const entry = state[action.key];
            if (entry?.status !== "needsSetup")
                return state;
            return {
                ...state,
                [action.key]: {
                    status: "submitting",
                    mcpServer: entry.mcpServer,
                    missingVariables: entry.missingVariables,
                    discoveredTools: entry.discoveredTools,
                    toolApprovals: entry.toolApprovals,
                    error: null,
                },
            };
        }
        case "SUBMIT_DONE": {
            const entry = state[action.key];
            if (entry?.status !== "submitting")
                return state;
            return {
                ...state,
                [action.key]: {
                    status: "ready",
                    mcpServer: entry.mcpServer,
                    discoveredTools: entry.discoveredTools,
                    toolApprovals: entry.toolApprovals,
                    enabledTools: action.enabledTools,
                    error: null,
                },
            };
        }
        case "SUBMIT_FAIL": {
            const entry = state[action.key];
            if (entry?.status !== "submitting")
                return state;
            return {
                ...state,
                [action.key]: {
                    status: "needsSetup",
                    mcpServer: entry.mcpServer,
                    missingVariables: entry.missingVariables,
                    discoveredTools: entry.discoveredTools,
                    toolApprovals: entry.toolApprovals,
                    error: action.error,
                },
            };
        }
        case "SET_ENABLED_TOOLS": {
            const entry = state[action.key];
            if (entry?.status !== "ready")
                return state;
            return {
                ...state,
                [action.key]: { ...entry, enabledTools: action.enabledTools },
            };
        }
        case "SET_ERROR": {
            const entry = state[action.key];
            if (!entry)
                return state;
            return { ...state, [action.key]: { ...entry, error: action.error } };
        }
        case "CLEAR_ERROR": {
            const entry = state[action.key];
            if (!entry)
                return state;
            return { ...state, [action.key]: { ...entry, error: null } };
        }
        case "REMOVE_SERVER": {
            if (!(action.key in state))
                return state;
            const { [action.key]: _, ...rest } = state;
            return rest;
        }
        case "RESET":
            return INITIAL_MCP_SETUP_STATE;
        default:
            return state;
    }
}
//# sourceMappingURL=mcpServerSetupReducer.js.map