"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ImportSource = void 0;
var MethodToVerb;
(function (MethodToVerb) {
    MethodToVerb["get"] = "read";
    MethodToVerb["post"] = "create";
    MethodToVerb["put"] = "update";
    MethodToVerb["patch"] = "patch";
    MethodToVerb["delete"] = "delete";
})(MethodToVerb || (MethodToVerb = {}));
class ImportSource {
    constructor() {
        this.constructQuery = (datasourceId, name, method, path, url, queryString, headers = {}, parameters = [], body = undefined) => {
            const readable = true;
            const queryVerb = this.verbFromMethod(method);
            const transformer = "return data";
            const schema = {};
            path = this.processPath(path);
            if (url) {
                if (typeof url === "string") {
                    path = `${url}/${path}`;
                }
                else {
                    let href = url.href;
                    if (href.endsWith("/")) {
                        href = href.slice(0, -1);
                    }
                    path = `${href}/${path}`;
                }
            }
            queryString = this.processQuery(queryString);
            const requestBody = JSON.stringify(body, null, 2);
            const query = {
                datasourceId,
                name,
                parameters,
                fields: {
                    headers,
                    queryString,
                    path,
                    requestBody,
                },
                transformer,
                schema,
                readable,
                queryVerb,
            };
            return query;
        };
        this.verbFromMethod = (method) => {
            const verb = MethodToVerb[method];
            if (!verb) {
                throw new Error(`Unsupported method: ${method}`);
            }
            return verb;
        };
        this.processPath = (path) => {
            if (path === null || path === void 0 ? void 0 : path.startsWith("/")) {
                path = path.substring(1);
            }
            // add extra braces around params for binding
            path = path.replace(/[{]/g, "{{");
            path = path.replace(/[}]/g, "}}");
            return path;
        };
        this.processQuery = (queryString) => {
            if (queryString === null || queryString === void 0 ? void 0 : queryString.startsWith("?")) {
                return queryString.substring(1);
            }
            return queryString;
        };
    }
}
exports.ImportSource = ImportSource;
