// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
import { getClientType } from "@azure-tools/typespec-client-generator-core";
import { createDiagnosticCollector, getDoc, getSummary, } from "@typespec/compiler";
import { getExtensions } from "@typespec/openapi";
import { InputParameterScope } from "../type/input-parameter-scope.js";
import { fromSdkType } from "./type-converter.js";
export function resolveServers(sdkContext, servers) {
    // Create a diagnostics collector for internal use
    const diagnostics = createDiagnosticCollector();
    const result = servers.map((server) => {
        const parameters = [];
        let url = server.url;
        const endpoint = url.replace("http://", "").replace("https://", "").split("/")[0];
        for (const [name, prop] of server.parameters) {
            const isEndpoint = endpoint === `{${name}}`;
            let defaultValue = undefined;
            const value = prop.defaultValue ? getDefaultValue(prop.defaultValue) : "";
            const inputType = isEndpoint
                ? {
                    kind: "url",
                    name: "url",
                    crossLanguageDefinitionId: "TypeSpec.url",
                }
                : diagnostics.pipe(fromSdkType(sdkContext, getClientType(sdkContext, prop)));
            if (value) {
                defaultValue = {
                    type: inputType,
                    value: value,
                };
            }
            const variable = {
                kind: "endpoint",
                name: name,
                serializedName: name,
                summary: getSummary(sdkContext.program, prop),
                doc: getDoc(sdkContext.program, prop),
                type: inputType,
                isApiVersion: name.toLowerCase() === "apiversion" || name.toLowerCase() === "api-version",
                isEndpoint: isEndpoint,
                optional: false,
                skipUrlEncoding: 
                // TODO: update this when https://github.com/Azure/typespec-azure/issues/1022 is resolved
                getExtensions(sdkContext.program, prop).get("x-ms-skip-url-encoding") === true,
                scope: InputParameterScope.Client,
                defaultValue: defaultValue,
                readOnly: false,
                crossLanguageDefinitionId: "",
            };
            parameters.push(variable);
        }
        /* add default server. */
        if (server.url && parameters.length === 0) {
            const variable = {
                kind: "endpoint",
                name: "host",
                serializedName: "host",
                doc: server.description,
                type: {
                    kind: "string",
                    name: "string",
                    crossLanguageDefinitionId: "TypeSpec.string",
                },
                isApiVersion: false,
                optional: false,
                isEndpoint: true,
                skipUrlEncoding: false,
                scope: InputParameterScope.Client,
                defaultValue: {
                    type: {
                        kind: "string",
                        name: "string",
                        crossLanguageDefinitionId: "TypeSpec.string",
                    },
                    value: server.url,
                },
                readOnly: false,
                crossLanguageDefinitionId: "",
            };
            url = `{host}`;
            parameters.push(variable);
        }
        return {
            url: url,
            description: server.description,
            parameters,
        };
    });
    return diagnostics.wrap(result);
}
function getDefaultValue(value) {
    switch (value.valueKind) {
        case "StringValue":
            return value.value;
        case "NumericValue":
            return value.value;
        case "BooleanValue":
            return value.value;
        case "ArrayValue":
            return value.values.map(getDefaultValue);
        default:
            return undefined;
    }
}
//# sourceMappingURL=typespec-server.js.map