"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.run = exports.definition = void 0;
const row_1 = require("../../api/controllers/row");
const utils_1 = require("./utils");
const automationUtils_1 = require("../automationUtils");
exports.definition = {
    description: "Delete a row from your database",
    icon: "TableRowRemoveCenter",
    name: "Delete Row",
    tagline: "Delete a {{inputs.enriched.table.name}} row",
    type: "ACTION",
    stepId: "DELETE_ROW",
    internal: true,
    inputs: {},
    schema: {
        inputs: {
            properties: {
                tableId: {
                    type: "string",
                    customType: "table",
                    title: "Table",
                },
                id: {
                    type: "string",
                    title: "Row ID",
                },
            },
            required: ["tableId", "id"],
        },
        outputs: {
            properties: {
                row: {
                    type: "object",
                    customType: "row",
                    description: "The deleted row",
                },
                response: {
                    type: "object",
                    description: "The response from the table",
                },
                success: {
                    type: "boolean",
                    description: "Whether the deletion was successful",
                },
            },
            required: ["row", "success"],
        },
    },
};
function run({ inputs, appId, emitter }) {
    return __awaiter(this, void 0, void 0, function* () {
        if (inputs.id == null) {
            return {
                success: false,
                response: {
                    message: "Invalid inputs",
                },
            };
        }
        let ctx = (0, utils_1.buildCtx)(appId, emitter, {
            body: {
                _id: inputs.id,
                _rev: inputs.revision,
            },
            params: {
                tableId: inputs.tableId,
            },
        });
        try {
            yield (0, row_1.destroy)(ctx);
            return {
                response: ctx.body,
                row: ctx.row,
                success: ctx.status === 200,
            };
        }
        catch (err) {
            return {
                success: false,
                response: (0, automationUtils_1.getError)(err),
            };
        }
    });
}
exports.run = run;
