// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
import { getDoc } from "@typespec/compiler";
import { getExtensions } from "@typespec/openapi";
import { InputOperationParameterKind } from "../type/input-operation-parameter-kind.js";
import { RequestLocation } from "../type/request-location.js";
import { getDefaultValue, getInputType } from "./model.js";
export function resolveServers(context, servers, typeMap) {
    return 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.default ? getDefaultValue(prop.default) : "";
            const inputType = isEndpoint
                ? {
                    kind: "url",
                    name: "url",
                    crossLanguageDefinitionId: "TypeSpec.url",
                }
                : getInputType(context, prop, typeMap);
            if (value) {
                defaultValue = {
                    Type: inputType,
                    Value: value,
                };
            }
            const variable = {
                Name: name,
                NameInRequest: name,
                Description: getDoc(context.program, prop),
                Type: inputType,
                Location: RequestLocation.Uri,
                IsApiVersion: name.toLowerCase() === "apiversion" || name.toLowerCase() === "api-version",
                IsResourceParameter: false,
                IsContentType: false,
                IsRequired: true,
                IsEndpoint: isEndpoint,
                SkipUrlEncoding: 
                // TODO: update this when https://github.com/Azure/typespec-azure/issues/1022 is resolved
                getExtensions(context.program, prop).get("x-ms-skip-url-encoding") === true,
                Explode: false,
                Kind: InputOperationParameterKind.Client,
                DefaultValue: defaultValue,
            };
            parameters.push(variable);
        }
        /* add default server. */
        if (server.url && parameters.length === 0) {
            const variable = {
                Name: "host",
                NameInRequest: "host",
                Description: server.description,
                Type: {
                    kind: "string",
                    name: "string",
                    crossLanguageDefinitionId: "TypeSpec.string",
                },
                Location: RequestLocation.Uri,
                IsApiVersion: false,
                IsResourceParameter: false,
                IsContentType: false,
                IsRequired: true,
                IsEndpoint: true,
                SkipUrlEncoding: false,
                Explode: false,
                Kind: InputOperationParameterKind.Client,
                DefaultValue: {
                    Type: {
                        kind: "string",
                        name: "string",
                        crossLanguageDefinitionId: "TypeSpec.string",
                    },
                    Value: server.url,
                },
            };
            url = `{host}`;
            parameters.push(variable);
        }
        return {
            url: url,
            description: server.description,
            parameters,
        };
    });
}
//# sourceMappingURL=typespec-server.js.map