/**
 * Handler for machines_list messages.
 * Forwards the request to the gateway, which queries the database for the
 * user's machines, and returns the response to the client.
 */
import { ErrorCode } from "@pocketcoder/shared/constants";
import { logger } from "../utils/logger.js";
import { gatewayRequest } from "../gateway/gateway-request.js";
/** The gateway client instance, set via setMachinesListGatewayClient(). */
let _gatewayClient = null;
/**
 * Provide the gateway client to the handler.
 * Must be called during daemon startup before messages arrive.
 */
export function setMachinesListGatewayClient(client) {
    _gatewayClient = client;
}
export async function handleMachinesList(message, send) {
    const msg = message;
    const { requestId } = msg;
    if (!_gatewayClient || !_gatewayClient.connected) {
        send({
            type: "machines_list_response",
            requestId,
            error: {
                code: ErrorCode.UNKNOWN_ERROR,
                message: "Not connected to gateway",
            },
        });
        return;
    }
    try {
        const response = (await gatewayRequest(_gatewayClient, {
            type: "machines_list",
            requestId,
            payload: {},
        }, "machines_list_response"));
        send(response);
    }
    catch (err) {
        const errorMessage = err instanceof Error ? err.message : String(err);
        logger.error(`machines_list error: ${errorMessage}`);
        send({
            type: "machines_list_response",
            requestId,
            error: {
                code: ErrorCode.UNKNOWN_ERROR,
                message: errorMessage,
            },
        });
    }
}
//# sourceMappingURL=machines-list.js.map