import { ensureConfigDir, saveOwnerConfig } from "../config/index.js";
import { DEFAULT_HEARTBEAT_INTERVAL_MS } from "../config/types.js";
import { getCurrentVersion } from "../utils/version-check.js";
import { logger } from "../logger.js";
import { generateVerificationCode } from "./set-owner.js";
const POLL_INTERVAL_MS = 10_000;
/**
 * Poll the onboarding service for an owner email.
 * Blocks until the service returns a non-null ownerEmail, then writes owner.json.
 * Returns the created OwnerConfig.
 */
export async function waitForRemoteOwner(client, config) {
    logger.system("Polling onboarding service for owner email...");
    // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
    while (true) {
        const result = await client.heartbeat({
            version: getCurrentVersion(),
            model: config.model,
            provider: config.provider,
            uptime: Math.round(process.uptime()),
            busy: false,
            onboardingComplete: false,
        });
        if (result?.ownerEmail) {
            logger.system(`Owner email received from onboarding service: ${result.ownerEmail}`);
            ensureConfigDir();
            const code = generateVerificationCode();
            const ownerConfig = {
                ownerName: result.ownerName ?? "",
                ownerEmail: result.ownerEmail,
                allowedDiscordChannelIds: [],
                heartbeatIntervalMs: DEFAULT_HEARTBEAT_INTERVAL_MS,
                dualSessionEnabled: result.dualSessionEnabled,
                androidUseEnabled: false,
                chatMonitorEnabled: true,
                onboardingComplete: false,
                onboardingCodeVerified: false,
                onboardingCode: code,
                silentCompaction: true,
                language: "en",
            };
            saveOwnerConfig(ownerConfig);
            logger.system("owner.json written with verification code.");
            return ownerConfig;
        }
        await new Promise((resolve) => setTimeout(resolve, POLL_INTERVAL_MS));
    }
}
//# sourceMappingURL=remote-set-owner.js.map