import { getLibraryName, getSdkModel, } from "@azure-tools/typespec-client-generator-core";
import { spawn } from "child_process";
import { InputOperationParameterKind } from "../type/input-operation-parameter-kind.js";
import { RequestLocation } from "../type/request-location.js";
export function capitalize(str) {
    return str.charAt(0).toUpperCase() + str.slice(1);
}
export function getNameForTemplate(model) {
    if (model.name !== "" && model.templateMapper && model.templateMapper.args) {
        return model.name + model.templateMapper.args.map((it) => it.name).join("");
    }
    return model.name;
}
export function getTypeName(context, type) {
    const name = getLibraryName(context, type);
    if (type.kind !== "Model")
        return name;
    if (type.name === name) {
        const templateName = getNameForTemplate(type);
        if (templateName === "") {
            const sdkModel = getSdkModel(context, type);
            return sdkModel.name;
        }
        return templateName;
    }
    return name;
}
export function createContentTypeOrAcceptParameter(mediaTypes, name, nameInRequest) {
    const isContentType = nameInRequest.toLowerCase() === "content-type";
    const inputType = {
        kind: "string",
        name: "string",
        crossLanguageDefinitionId: "TypeSpec.string",
    };
    return {
        Name: name,
        NameInRequest: nameInRequest,
        Type: inputType,
        Location: RequestLocation.Header,
        IsApiVersion: false,
        IsResourceParameter: false,
        IsContentType: isContentType,
        IsRequired: true,
        IsEndpoint: false,
        SkipUrlEncoding: false,
        Explode: false,
        Kind: InputOperationParameterKind.Constant,
        DefaultValue: mediaTypes.length === 1
            ? {
                Type: inputType,
                Value: mediaTypes[0],
            }
            : undefined,
    };
}
export function isSdkPathParameter(parameter) {
    return parameter.kind === "path";
}
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,
            });
        });
    });
}
//# sourceMappingURL=utils.js.map