/**
 * request_oauth tool — parks via ToolExecutor, waits for the client
 * to complete an OAuth popup flow via SFAE, then resumes with status.
 *
 * The agent never sees the actual tokens.
 */
import { Type } from '@sinclair/typebox';
import { REQUEST_OAUTH } from '../../tool-names.js';
const schema = Type.Object({
    domain: Type.String({
        description: "The API domain requiring OAuth (e.g. 'googleapis.com')"
    }),
    scope: Type.String({
        description: "Space-separated OAuth scopes (e.g. 'https://www.googleapis.com/auth/gmail.readonly')"
    }),
    message: Type.Optional(Type.String({
        description: 'Explanation shown to the user about why OAuth access is needed'
    }))
});
export function createRequestOAuthTool(toolExecutor) {
    return {
        name: REQUEST_OAUTH,
        label: 'Request OAuth',
        isReadOnly: true,
        description: 'Initiate an OAuth 2.0 authorization flow with the user. ' +
            'Use this when a service requires OAuth consent (e.g. Google APIs) ' +
            'instead of a static API key or access token. ' +
            'The user will be redirected to the provider consent page in a popup. ' +
            'After authorization, tokens are stored securely. ' +
            'You will never see the actual tokens.',
        parameters: schema,
        async execute(toolCallId, { domain, scope, message }) {
            const { promise, notify } = toolExecutor.submit(toolCallId, REQUEST_OAUTH, {
                domain,
                scope,
                ...(message ? { message } : {})
            });
            notify();
            const result = await promise;
            if (result.status === 'error') {
                return {
                    content: [{ type: 'text', text: result.error ?? 'OAuth request failed' }],
                    details: { status: 'error' }
                };
            }
            const data = (result.data ?? {});
            const status = data.status ?? 'unknown';
            if (status === 'success') {
                const resDomain = data.domain ?? domain;
                return {
                    content: [{ type: 'text', text: `OAuth credentials stored: ${resDomain}` }],
                    details: { status: 'success', domain: resDomain }
                };
            }
            if (status === 'cancelled') {
                return {
                    content: [{ type: 'text', text: 'User cancelled the OAuth request.' }],
                    details: { status: 'cancelled' }
                };
            }
            const error = data.error ?? status;
            return {
                content: [{ type: 'text', text: `OAuth request failed: ${error}` }],
                details: { status }
            };
        }
    };
}
//# sourceMappingURL=request-oauth.js.map