import { listAllServiceNamespaces } from "@azure-tools/typespec-client-generator-core";
import { getNamespaceFullName, NoTarget } from "@typespec/compiler";
import { spawn } from "child_process";
export async function execCSharpGenerator(context, options) {
    const command = "dotnet";
    const args = [
        "--roll-forward",
        "Major",
        options.generatorPath,
        options.outputFolder,
        "-g",
        options.generatorName,
    ];
    if (options.newProject) {
        args.push("--new-project");
    }
    if (options.debug) {
        args.push("--debug");
    }
    context.logger.info(`${command} ${args.join(" ")}`);
    const child = spawn(command, args, { stdio: "pipe" });
    const stderr = [];
    return new Promise((resolve, reject) => {
        let buffer = "";
        child.stdout?.on("data", (data) => {
            buffer += data.toString();
            let index;
            while ((index = buffer.indexOf("\n")) !== -1) {
                const message = buffer.slice(0, index);
                buffer = buffer.slice(index + 1);
                processJsonRpc(context, message);
            }
        });
        child.stderr?.on("data", (data) => {
            stderr.push(data);
        });
        child.on("error", (error) => {
            reject(error);
        });
        child.on("exit", (exitCode) => {
            resolve({
                exitCode: exitCode ?? -1,
                stderr: Buffer.concat(stderr).toString(),
                proc: child,
            });
        });
    });
}
function processJsonRpc(context, message) {
    const response = JSON.parse(message);
    const method = response.method;
    const params = response.params;
    switch (method) {
        case "trace":
            context.logger.trace(params.level, params.message);
            break;
        case "diagnostic":
            let crossLanguageDefinitionId;
            if ("crossLanguageDefinitionId" in params) {
                crossLanguageDefinitionId = params.crossLanguageDefinitionId;
            }
            context.logger.reportDiagnostic({
                code: params.code,
                format: {
                    message: params.message,
                },
                target: findTarget(crossLanguageDefinitionId) ?? NoTarget,
            });
            break;
    }
    function findTarget(crossLanguageDefinitionId) {
        if (crossLanguageDefinitionId === undefined) {
            return undefined;
        }
        return context.__typeCache.crossLanguageDefinitionIds.get(crossLanguageDefinitionId);
    }
}
export async function execAsync(command, args = [], options = {}) {
    const child = spawn(command, args, options);
    return new Promise((resolve, reject) => {
        child.on("error", (error) => {
            reject(error);
        });
        const stdio = [];
        const stdout = [];
        const stderr = [];
        child.stdout?.on("data", (data) => {
            stdout.push(data);
            stdio.push(data);
        });
        child.stderr?.on("data", (data) => {
            stderr.push(data);
            stdio.push(data);
        });
        child.on("exit", (exitCode) => {
            resolve({
                exitCode: exitCode ?? -1,
                stdio: Buffer.concat(stdio).toString(),
                stdout: Buffer.concat(stdout).toString(),
                stderr: Buffer.concat(stderr).toString(),
                proc: child,
            });
        });
    });
}
export function getClientNamespaceString(context) {
    return getClientNamespaceStringHelper(context.emitContext.options["package-name"], listAllServiceNamespaces(context)[0]);
}
export function getClientNamespaceStringHelper(packageName, namespace) {
    if (packageName) {
        packageName = packageName
            .replace(/-/g, ".")
            .replace(/\.([a-z])?/g, (match) => match.toUpperCase());
        return packageName.charAt(0).toUpperCase() + packageName.slice(1);
    }
    if (namespace) {
        return getNamespaceFullName(namespace);
    }
    return undefined;
}
//# sourceMappingURL=utils.js.map