import { i as listChannelPlugins } from "./registry-Cm9hh4A0.js";
import "./plugins-Bgbyd76W.js";
import { in as errorShape, rn as ErrorCodes } from "./schema-SR2yQE9K.js";
import { ir as validateWebLoginWaitParams, rr as validateWebLoginStartParams } from "./src-D97XO84e.js";
import { t as formatForLog } from "./ws-log-5Yy_e1zU.js";
import { t as assertValidParams } from "./validation-A2QHa-1d.js";
//#region src/gateway/server-methods/web.ts
const WEB_LOGIN_METHODS = new Set(["web.login.start", "web.login.wait"]);
/** Resolves the channel plugin that currently owns web QR-login methods. */
const resolveWebLoginProvider = () => listChannelPlugins().find((plugin) => [...plugin.gatewayMethods ?? [], ...(plugin.gatewayMethodDescriptors ?? []).map((descriptor) => descriptor.name)].some((method) => WEB_LOGIN_METHODS.has(method))) ?? null;
function resolveAccountId(params) {
	return typeof params.accountId === "string" ? params.accountId : void 0;
}
function respondProviderUnavailable(respond) {
	respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, "web login provider is not available"));
}
function respondProviderUnsupported(respond, providerId) {
	respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, `web login is not supported by provider ${providerId}`));
}
function respondWebLoginUnavailable(respond, err) {
	respond(false, void 0, errorShape(ErrorCodes.UNAVAILABLE, formatForLog(err)));
}
/** Resolves a concrete provider gateway login method or sends the public error. */
function resolveWebLoginRequest(params) {
	const accountId = resolveAccountId(params.rawParams);
	const provider = resolveWebLoginProvider();
	if (!provider) {
		respondProviderUnavailable(params.respond);
		return null;
	}
	const gateway = provider.gateway;
	const run = gateway?.[params.gatewayMethod];
	if (!run) {
		respondProviderUnsupported(params.respond, provider.id);
		return null;
	}
	return {
		accountId,
		provider,
		run: run.bind(gateway)
	};
}
/** Checks whether the matching channel/account should be restored after login start. */
function wasChannelRunning(params) {
	const runtime = params.context.getRuntimeSnapshot();
	if (params.accountId) {
		const accountRuntime = runtime.channelAccounts[params.channelId]?.[params.accountId];
		if (accountRuntime) return accountRuntime.running === true;
	}
	if (!params.accountId) return runtime.channels[params.channelId]?.running === true;
	const defaultRuntime = runtime.channels[params.channelId];
	return defaultRuntime?.accountId === params.accountId && defaultRuntime.running === true;
}
/** Gateway handlers for plugin-owned web QR-login flows. */
const webHandlers = {
	"web.login.start": async ({ params, respond, context }) => {
		if (!assertValidParams(params, validateWebLoginStartParams, "web.login.start", respond)) return;
		try {
			const request = resolveWebLoginRequest({
				rawParams: params,
				respond,
				gatewayMethod: "loginWithQrStart"
			});
			if (!request) return;
			const { accountId, provider, run } = request;
			const wasRunning = wasChannelRunning({
				context,
				channelId: provider.id,
				accountId
			});
			await context.stopChannel(provider.id, accountId);
			const result = await run({
				force: Boolean(params.force),
				timeoutMs: typeof params.timeoutMs === "number" ? params.timeoutMs : void 0,
				verbose: Boolean(params.verbose),
				accountId
			});
			if (result.connected) await context.startChannel(provider.id, accountId);
			else if (wasRunning && !result.qrDataUrl) await context.startChannel(provider.id, accountId);
			respond(true, result, void 0);
		} catch (err) {
			respondWebLoginUnavailable(respond, err);
		}
	},
	"web.login.wait": async ({ params, respond, context }) => {
		if (!assertValidParams(params, validateWebLoginWaitParams, "web.login.wait", respond)) return;
		try {
			const request = resolveWebLoginRequest({
				rawParams: params,
				respond,
				gatewayMethod: "loginWithQrWait"
			});
			if (!request) return;
			const { accountId, provider, run } = request;
			const result = await run({
				timeoutMs: typeof params.timeoutMs === "number" ? params.timeoutMs : void 0,
				accountId,
				currentQrDataUrl: typeof params.currentQrDataUrl === "string" ? params.currentQrDataUrl : void 0
			});
			if (result.connected) await context.startChannel(provider.id, accountId);
			respond(true, result, void 0);
		} catch (err) {
			respondWebLoginUnavailable(respond, err);
		}
	}
};
//#endregion
export { webHandlers };
