"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());
    });
};
var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
const queryController = require("../../api/controllers/query");
const { buildCtx } = require("./utils");
const automationUtils = require("../automationUtils");
exports.definition = {
    name: "External Data Connector",
    tagline: "Execute Data Connector",
    icon: "Data",
    description: "Execute a query in an external data connector",
    type: "ACTION",
    stepId: "EXECUTE_QUERY",
    internal: true,
    inputs: {},
    schema: {
        inputs: {
            properties: {
                query: {
                    type: "object",
                    properties: {
                        queryId: {
                            type: "string",
                            customType: "query",
                        },
                    },
                    customType: "queryParams",
                    title: "Parameters",
                    required: ["queryId"],
                },
            },
            required: ["query"],
        },
        outputs: {
            properties: {
                response: {
                    type: "object",
                    description: "The response from the datasource execution",
                },
                info: {
                    type: "object",
                    description: "Some query types may return extra data, like headers from a REST query",
                },
                success: {
                    type: "boolean",
                    description: "Whether the action was successful",
                },
            },
        },
        required: ["response", "success"],
    },
};
exports.run = function ({ inputs, appId, emitter }) {
    return __awaiter(this, void 0, void 0, function* () {
        if (inputs.query == null) {
            return {
                success: false,
                response: {
                    message: "Invalid inputs",
                },
            };
        }
        const _a = inputs.query, { queryId } = _a, rest = __rest(_a, ["queryId"]);
        const ctx = buildCtx(appId, emitter, {
            body: {
                parameters: rest,
            },
            params: {
                queryId,
            },
        });
        try {
            yield queryController.executeV2(ctx, { isAutomation: true });
            const _b = ctx.body, { data } = _b, rest = __rest(_b, ["data"]);
            return {
                response: data,
                info: rest,
                success: true,
            };
        }
        catch (err) {
            return {
                success: false,
                info: {},
                response: automationUtils.getError(err),
            };
        }
    });
};
