import { loadTokens } from "../../auth/token-storage.js";
import { listProjects } from "../../db/repositories/projects.js";
import { getMachineMetadata } from "../../utils/machine-info.js";
/**
 * Build the daemon_register message from current auth, machine info, and projects.
 * Returns null if not authenticated (no tokens).
 */
export async function buildDaemonRegisterMessage(label = null) {
    const tokens = await loadTokens();
    if (!tokens)
        return null;
    const machine = getMachineMetadata(label);
    const dbProjects = listProjects();
    const projects = dbProjects.map((p) => ({
        projectId: p.project_id,
        name: p.name,
    }));
    return {
        type: "daemon_register",
        payload: {
            machineKey: tokens.machine_key,
            token: tokens.access_token,
            machine,
            projects,
        },
    };
}
/**
 * Send daemon_register message to the gateway.
 * Returns true if the message was sent, false if not authenticated.
 */
export async function sendDaemonRegister(client, label = null) {
    const message = await buildDaemonRegisterMessage(label);
    if (!message)
        return false;
    client.send(message);
    return true;
}
//# sourceMappingURL=daemon-register.js.map