"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 rowController = require("../../api/controllers/row");
const tableController = require("../../api/controllers/table");
const { FieldTypes } = require("../../constants");
const { buildCtx } = require("./utils");
const automationUtils = require("../automationUtils");
const SortOrders = {
    ASCENDING: "ascending",
    DESCENDING: "descending",
};
const SortOrdersPretty = {
    [SortOrders.ASCENDING]: "Ascending",
    [SortOrders.DESCENDING]: "Descending",
};
exports.definition = {
    description: "Query rows from the database",
    icon: "Search",
    name: "Query rows",
    tagline: "Query rows from {{inputs.enriched.table.name}} table",
    type: "ACTION",
    stepId: "QUERY_ROWS",
    internal: true,
    inputs: {},
    schema: {
        inputs: {
            properties: {
                tableId: {
                    type: "string",
                    customType: "table",
                    title: "Table",
                },
                filters: {
                    type: "object",
                    customType: "filters",
                    title: "Filtering",
                },
                sortColumn: {
                    type: "string",
                    title: "Sort Column",
                    customType: "column",
                },
                sortOrder: {
                    type: "string",
                    title: "Sort Order",
                    enum: Object.values(SortOrders),
                    pretty: Object.values(SortOrdersPretty),
                },
                limit: {
                    type: "number",
                    title: "Limit",
                    customType: "queryLimit",
                },
            },
            required: ["tableId"],
        },
        outputs: {
            properties: {
                rows: {
                    type: "array",
                    customType: "rows",
                    description: "The rows that were found",
                },
                success: {
                    type: "boolean",
                    description: "Whether the deletion was successful",
                },
            },
            required: ["rows", "success"],
        },
    },
};
function getTable(appId, tableId) {
    return __awaiter(this, void 0, void 0, function* () {
        const ctx = buildCtx(appId, null, {
            params: {
                tableId,
            },
        });
        yield tableController.find(ctx);
        return ctx.body;
    });
}
exports.run = function ({ inputs, appId }) {
    return __awaiter(this, void 0, void 0, function* () {
        const { tableId, filters, sortColumn, sortOrder, limit } = inputs;
        const table = yield getTable(appId, tableId);
        let sortType = FieldTypes.STRING;
        if (table && table.schema && table.schema[sortColumn] && sortColumn) {
            const fieldType = table.schema[sortColumn].type;
            sortType =
                fieldType === FieldTypes.NUMBER ? FieldTypes.NUMBER : FieldTypes.STRING;
        }
        const ctx = buildCtx(appId, null, {
            params: {
                tableId,
            },
            body: {
                sortType,
                limit,
                sort: sortColumn,
                query: filters || {},
                // default to ascending, like data tab
                sortOrder: sortOrder || SortOrders.ASCENDING,
            },
            version: "1",
        });
        try {
            yield rowController.search(ctx);
            return {
                rows: ctx.body ? ctx.body.rows : [],
                success: ctx.status === 200,
            };
        }
        catch (err) {
            return {
                success: false,
                response: automationUtils.getError(err),
            };
        }
    });
};
