import { w as withAdminBody, k as jsonResponse } from './helpers-kzwksNq1.js';
import { o as opencodeFetch } from './http-D8YVIqJa.js';
import { a as asStringOrEmpty } 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/finish/+server.ts
/**
* POST /admin/providers/oauth/finish — Complete an OAuth code-mode
* sign-in by exchanging the operator-pasted authorization code with the
* assistant OpenCode instance.
*/
var POST = (event) => withAdminBody(event, async ({ requestId, body }) => {
	try {
		const providerId = asStringOrEmpty(body.providerId);
		const methodIndex = Number(asStringOrEmpty(body.methodIndex));
		const code = asStringOrEmpty(body.code);
		if (!providerId || Number.isNaN(methodIndex) || !code) return jsonResponse(200, {
			ok: false,
			message: "Paste the authorization code before finishing sign-in.",
			selectedProviderId: providerId
		}, requestId);
		await opencodeFetch(`/provider/${encodeURIComponent(providerId)}/oauth/callback`, {
			method: "POST",
			body: JSON.stringify({
				method: methodIndex,
				code
			})
		});
		return jsonResponse(200, {
			ok: true,
			message: "OAuth connection completed.",
			selectedProviderId: providerId
		}, 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-DFpIzR83.js.map
