export const DEFAULT_GATEWAY_HOST = "127.0.0.1";
export const DEFAULT_GATEWAY_PORT = 4789;
function isJsonValue(value) {
    if (value === null ||
        typeof value === "string" ||
        typeof value === "boolean" ||
        (typeof value === "number" && Number.isFinite(value))) {
        return true;
    }
    if (Array.isArray(value)) {
        return value.every(isJsonValue);
    }
    if (typeof value === "object") {
        return Object.values(value).every(isJsonValue);
    }
    return false;
}
export function isGatewayRequestFrame(value) {
    if (!value || typeof value !== "object")
        return false;
    const frame = value;
    if (frame.type !== "req" || typeof frame.id !== "string")
        return false;
    if (!frame.event || typeof frame.event !== "object")
        return false;
    const event = frame.event;
    if (typeof event.piboSessionId !== "string" || event.piboSessionId.length === 0)
        return false;
    if (event.type === "message")
        return typeof event.text === "string";
    if (event.type === "execution") {
        return typeof event.action === "string" && (event.params === undefined || isJsonValue(event.params));
    }
    return false;
}
export function encodeFrame(frame) {
    return `${JSON.stringify(frame)}\n`;
}
export function errorResponse(id, error) {
    return {
        type: "res",
        id,
        ok: false,
        error: { message: error instanceof Error ? error.message : String(error) },
    };
}
