"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
const fetch = require("node-fetch");
const { getFetchResponse } = require("./utils");
const automationUtils = require("../automationUtils");
const RequestType = {
    POST: "POST",
    GET: "GET",
    PUT: "PUT",
    DELETE: "DELETE",
    PATCH: "PATCH",
};
const BODY_REQUESTS = [RequestType.POST, RequestType.PUT, RequestType.PATCH];
/**
 * NOTE: this functionality is deprecated - it no longer should be used.
 */
exports.definition = {
    deprecated: true,
    name: "Outgoing webhook",
    tagline: "Send a {{inputs.requestMethod}} request",
    icon: "Send",
    description: "Send a request of specified method to a URL",
    type: "ACTION",
    internal: true,
    stepId: "OUTGOING_WEBHOOK",
    inputs: {
        requestMethod: "POST",
        url: "http://",
        requestBody: "{}",
        headers: "{}",
    },
    schema: {
        inputs: {
            properties: {
                requestMethod: {
                    type: "string",
                    enum: Object.values(RequestType),
                    title: "Request method",
                },
                url: {
                    type: "string",
                    title: "URL",
                },
                requestBody: {
                    type: "string",
                    title: "JSON Body",
                    customType: "wide",
                },
                headers: {
                    type: "string",
                    title: "Headers",
                    customType: "wide",
                },
            },
            required: ["requestMethod", "url"],
        },
        outputs: {
            properties: {
                response: {
                    type: "object",
                    description: "The response from the webhook",
                },
                httpStatus: {
                    type: "number",
                    description: "The HTTP status code returned",
                },
                success: {
                    type: "boolean",
                    description: "Whether the action was successful",
                },
            },
            required: ["response", "success"],
        },
    },
};
exports.run = function ({ inputs }) {
    return __awaiter(this, void 0, void 0, function* () {
        let { requestMethod, url, requestBody, headers } = inputs;
        if (!url.startsWith("http")) {
            url = `http://${url}`;
        }
        const request = {
            method: requestMethod,
        };
        if (headers) {
            try {
                const customHeaders = typeof headers === "string" ? JSON.parse(headers) : headers;
                request.headers = Object.assign(Object.assign({}, request.headers), customHeaders);
            }
            catch (err) {
                return {
                    success: false,
                    response: "Unable to process headers, must be a JSON object.",
                };
            }
        }
        if (requestBody &&
            requestBody.length !== 0 &&
            BODY_REQUESTS.indexOf(requestMethod) !== -1) {
            request.body =
                typeof requestBody === "string"
                    ? requestBody
                    : JSON.stringify(requestBody);
            request.headers = Object.assign(Object.assign({}, request.headers), { "Content-Type": "application/json" });
        }
        try {
            // do a quick JSON parse if there is a body, to generate an error if its invalid
            if (request.body) {
                JSON.parse(request.body);
            }
            const response = yield fetch(url, request);
            const { status, message } = yield getFetchResponse(response);
            return {
                httpStatus: status,
                response: message,
                success: status >= 200 && status <= 206,
            };
        }
        catch (err) {
            /* istanbul ignore next */
            return {
                success: false,
                response: automationUtils.getError(err),
            };
        }
    });
};
