"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());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Curl = void 0;
const base_1 = require("./base");
const url_1 = require("url");
const curlconverter = require("curlconverter");
const parseCurl = (data) => {
    const curlJson = curlconverter.toJsonString(data);
    return JSON.parse(curlJson);
};
/**
 * The curl converter parses the request body into the key field of an object
 * e.g. --d '{"key":"val"}' produces an object { "{"key":"val"}" : "" }
 * This is not what we want, so we need to parse out the key from the object
 */
const parseBody = (curl) => {
    if (curl.data) {
        const keys = Object.keys(curl.data);
        if (keys.length) {
            let key = keys[0];
            try {
                // filter out the dollar syntax used by curl for shell support
                if (key.startsWith("$")) {
                    key = key.substring(1);
                }
                return JSON.parse(key);
            }
            catch (e) {
                // do nothing
            }
        }
    }
    return undefined;
};
const parseCookie = (curl) => {
    if (curl.cookies) {
        return Object.entries(curl.cookies).reduce((acc, entry) => {
            const [key, value] = entry;
            return acc + `${key}=${value}; `;
        }, "");
    }
    return null;
};
/**
 * Curl
 * https://curl.se/docs/manpage.html
 */
class Curl extends base_1.ImportSource {
    constructor() {
        super(...arguments);
        this.isSupported = (data) => __awaiter(this, void 0, void 0, function* () {
            try {
                const curl = parseCurl(data);
                this.curl = curl;
            }
            catch (err) {
                return false;
            }
            return true;
        });
        this.getUrl = () => {
            return new url_1.URL(this.curl.raw_url);
        };
        this.getInfo = () => __awaiter(this, void 0, void 0, function* () {
            const url = this.getUrl();
            return {
                name: url.hostname,
            };
        });
        this.getQueries = (datasourceId) => __awaiter(this, void 0, void 0, function* () {
            const url = this.getUrl();
            const name = url.pathname;
            const path = url.origin + url.pathname;
            const method = this.curl.method;
            const queryString = url.search;
            const headers = this.curl.headers;
            const requestBody = parseBody(this.curl);
            const cookieHeader = parseCookie(this.curl);
            if (cookieHeader) {
                headers["Cookie"] = cookieHeader;
            }
            const query = this.constructQuery(datasourceId, name, method, path, undefined, queryString, headers, [], requestBody);
            return [query];
        });
    }
    getImportSource() {
        return "curl";
    }
}
exports.Curl = Curl;
