// Copyright Metatype OÜ, licensed under the Mozilla Public License Version 2.0.
// SPDX-License-Identifier: MPL-2.0
import "./_dnt.polyfills.js";
import { inspect } from "util";
// import { createInterface, Interface } from "node:readline";
import process from "process";
/**
 * see: module level documentation `meta-cli/src/deploy/actors/task.rs`
 */
function getOutput(args) {
    return args
        .map((arg) => {
        if (typeof arg === "string")
            return arg;
        return inspect(arg, {
            colors: process.stdout.isTTY,
            depth: 10,
            maxStringLength: 1000,
            maxArrayLength: 20,
        });
    })
        .join(" ");
}
export const log = {
    debug(...args) {
        const output = getOutput(args);
        process.stdout.write(`debug: ${output}\n`);
    },
    info(...args) {
        const output = getOutput(args);
        process.stdout.write(`info: ${output}\n`);
    },
    warn(...args) {
        const output = getOutput(args);
        process.stdout.write(`warning: ${output}\n`);
    },
    error(...args) {
        const output = getOutput(args);
        process.stdout.write(`error: ${output}\n`);
    },
    failure(data) {
        process.stdout.write(`failure: ${JSON.stringify(data)}\n`);
    },
    success(data, noEncode = false) {
        const encoded = noEncode ? data : JSON.stringify(data);
        process.stdout.write(`success: ${encoded}\n`);
    },
};
class RpcResponseReader {
    constructor() {
        Object.defineProperty(this, "buffer", {
            enumerable: true,
            configurable: true,
            writable: true,
            value: ""
        });
        process.stdin.setEncoding("utf-8");
    }
    read(id) {
        return new Promise((resolve, reject) => {
            const handler = () => {
                while (true) {
                    const chunk = process.stdin.read();
                    if (chunk == null) {
                        break;
                    }
                    this.buffer += chunk;
                    const lines = this.buffer.split(/\r\n|\n/);
                    if (lines.length > 2) {
                        reject(new Error("not sequential"));
                    }
                    else if (lines.length <= 1) {
                        continue;
                    }
                    this.buffer = lines.pop();
                    try {
                        const message = JSON.parse(lines[0]);
                        if (message.id === id) {
                            resolve(message.result);
                            break;
                        }
                    }
                    catch (e) {
                        reject("invalid message");
                    }
                }
                process.stdin.off("readable", handler);
            };
            process.stdin.on("readable", handler);
        });
    }
}
const JSONRPC_VERSION = "2.0";
const rpcCall = (() => {
    const responseReader = new RpcResponseReader();
    let latestRpcId = 0;
    return (method, params = null) => {
        const rpcId = latestRpcId++;
        const rpcMessage = JSON.stringify({
            jsonrpc: JSONRPC_VERSION,
            id: rpcId,
            method,
            params,
        });
        process.stdout.write(`jsonrpc: ${rpcMessage}\n`);
        return responseReader.read(rpcId);
    };
})();
export const rpc = {
    getDeployTarget: () => rpcCall("GetDeployTarget"),
    getDeployData: (typegraph) => rpcCall("GetDeployData", { typegraph }),
};
