var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
import HTTPErrorResponse from "../../../../../Types/API/HTTPErrorResponse";
import URL from "../../../../../Types/API/URL";
import BadDataException from "../../../../../Types/Exception/BadDataException";
import JSONFunctions from "../../../../../Types/JSONFunctions";
import CaptureSpan from "../../../../Utils/Telemetry/CaptureSpan";
export class ApiComponentUtils {
    static getReturnValues(response) {
        if (response instanceof HTTPErrorResponse) {
            return {
                "response-status": response.statusCode,
                "response-body": response.jsonData,
                "response-headers": response.headers,
                error: response.message || "Server Error.",
            };
        }
        return {
            "response-status": response.statusCode,
            "response-body": response.jsonData,
            "response-headers": response.headers,
            error: null,
        };
    }
    static sanitizeArgs(metadata, args, options) {
        const successPort = metadata.outPorts.find((p) => {
            return p.id === "success";
        });
        if (!successPort) {
            throw options.onError(new BadDataException("Success port not found"));
        }
        const errorPort = metadata.outPorts.find((p) => {
            return p.id === "error";
        });
        if (!errorPort) {
            throw options.onError(new BadDataException("Error port not found"));
        }
        if (args["request-body"] && typeof args["request-body"] === "string") {
            args["request-body"] = JSONFunctions.parse(`${args["request-body"]}`);
        }
        if (args["request-headers"] &&
            typeof args["request-headers"] === "string") {
            args["request-headers"] = JSONFunctions.parse(args["request-headers"]);
        }
        if (!args["url"]) {
            throw options.onError(new BadDataException("URL not found"));
        }
        if (args["url"] && typeof args["url"] !== "string") {
            throw options.onError(new BadDataException("URL is not type of string"));
        }
        args["url"] = URL.fromString(args["url"]);
        return { args, successPort, errorPort };
    }
}
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Object)
], ApiComponentUtils, "getReturnValues", null);
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object, Object, Object]),
    __metadata("design:returntype", Object)
], ApiComponentUtils, "sanitizeArgs", null);
//# sourceMappingURL=Utils.js.map