// Discord install URL helpers for self-hosted and gateway bot modes.
// Gateway URLs include OAuth state with client credentials so additional
// server installs are linked to the same gateway client identity.
import { generateBotInstallUrl } from './utils.js';
export const KIMAKI_SHARED_APP_ID = process.env.KIMAKI_SHARED_APP_ID || '1477605701202481173';
export const KIMAKI_WEBSITE_URL = process.env.KIMAKI_WEBSITE_URL || 'https://kimaki.xyz';
export function generateDiscordInstallUrlForBot({ appId, mode, clientId, clientSecret, }) {
    if (mode !== 'gateway') {
        return generateBotInstallUrl({ clientId: appId });
    }
    if (!KIMAKI_SHARED_APP_ID) {
        return new Error('Gateway mode is not available: shared app ID is missing');
    }
    if (!clientId || !clientSecret) {
        return new Error('Gateway credentials are missing from local database');
    }
    const statePayload = JSON.stringify({
        clientId,
        clientSecret,
    });
    return generateBotInstallUrl({
        clientId: KIMAKI_SHARED_APP_ID,
        state: statePayload,
        redirectUri: `${KIMAKI_WEBSITE_URL}/api/auth/callback/discord`,
        responseType: 'code',
    });
}
