import { w as withAdminBody, k as jsonResponse } from './helpers-kzwksNq1.js';
import { o as opencodeFetch } from './http-D8YVIqJa.js';
import { a as asStringOrEmpty, e as extractInputs } from './_helpers-B_lb4-jB.js';
import './src-CidJbygh.js';
import './chunk-CLZ62Ad-.js';
import 'node:module';
import './provider-constants-DPrj3yBX.js';
import './dist-DOFcG0Zp.js';
import 'node:fs';
import 'node:path';
import 'buffer';
import 'fs';
import 'path';
import 'node:child_process';
import 'node:url';
import 'node:crypto';
import 'events';
import 'node:events';
import 'node:stream';
import 'node:string_decoder';
import 'assert';
import 'zlib';
import 'node:assert';
import 'node:fs/promises';
import 'node:os';
import './endpoints-UdQGgKsR.js';
import './state-fxN45gBp.js';
import './coercion-TNFJisCC.js';

//#region src/routes/admin/providers/oauth/start/+server.ts
/**
* POST /admin/providers/oauth/start — Begin an OpenCode-mediated OAuth
* sign-in for a provider. Returns the authorization URL and any extra
* inputs the operator needs to confirm in the UI.
*
* Forwards directly to the running assistant's OpenCode at
* OP_OPENCODE_URL, which holds the OAuth methods map needed to issue
* the authorize request.
*/
var POST = (event) => withAdminBody(event, async ({ requestId, body }) => {
	try {
		const providerId = asStringOrEmpty(body.providerId);
		const methodIndex = Number(asStringOrEmpty(body.methodIndex));
		if (!providerId || Number.isNaN(methodIndex)) return jsonResponse(200, {
			ok: false,
			message: "Choose a provider sign-in method first.",
			selectedProviderId: void 0
		}, requestId);
		const inputs = extractInputs(body);
		const oauth = await opencodeFetch(`/provider/${encodeURIComponent(providerId)}/oauth/authorize`, {
			method: "POST",
			body: JSON.stringify({
				method: methodIndex,
				...inputs ? { inputs } : {}
			})
		});
		return jsonResponse(200, {
			ok: true,
			message: "OAuth flow prepared. Open the link below to continue.",
			selectedProviderId: providerId,
			oauth: {
				providerId,
				methodIndex,
				url: oauth.url,
				mode: oauth.method,
				instructions: oauth.instructions,
				inputs
			}
		}, requestId);
	} catch (error) {
		return jsonResponse(200, {
			ok: false,
			message: error instanceof Error ? error.message : "Internal error",
			selectedProviderId: void 0
		}, requestId);
	}
});

export { POST };
//# sourceMappingURL=_server.ts-B01UNM7i.js.map
