// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
import { UsageFlags } from "@azure-tools/typespec-client-generator-core";
import { resolvePath } from "@typespec/compiler";
import { PreserveType, stringifyRefs } from "json-serialize-refs";
import { configurationFileName, tspOutputFileName } from "./constants.js";
export async function writeCodeModel(context, codeModel, outputFolder) {
    await context.program.host.writeFile(resolvePath(outputFolder, tspOutputFileName), prettierOutput(stringifyRefs(codeModel, transformJSONProperties, 1, PreserveType.Objects)));
}
export async function writeConfiguration(context, configurations, outputFolder) {
    await context.program.host.writeFile(resolvePath(outputFolder, configurationFileName), prettierOutput(JSON.stringify(configurations, null, 2)));
}
function transformJSONProperties(key, value) {
    // convertUsageNumbersToStrings
    if (this["kind"] === "model" || this["kind"] === "enum") {
        if (key === "usage" && typeof value === "number") {
            if (value === 0) {
                return "None";
            }
            const result = [];
            for (const prop in UsageFlags) {
                if (!isNaN(Number(prop))) {
                    if ((value & Number(prop)) !== 0) {
                        result.push(UsageFlags[prop]);
                    }
                }
            }
            return result.join(",");
        }
    }
    // skip __raw if there is one
    if (key === "__raw") {
        return undefined;
    }
    return value;
}
function prettierOutput(output) {
    return output + "\n";
}
//# sourceMappingURL=code-model-writer.js.map