"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 automationUtils = require("../automationUtils");
const { buildCtx } = require("./utils");
exports.definition = {
    name: "Update Row",
    tagline: "Update a {{inputs.enriched.table.name}} row",
    icon: "Refresh",
    description: "Update a row in your database",
    type: "ACTION",
    internal: true,
    stepId: "UPDATE_ROW",
    inputs: {},
    schema: {
        inputs: {
            properties: {
                row: {
                    type: "object",
                    customType: "row",
                    title: "Table",
                },
                rowId: {
                    type: "string",
                    title: "Row ID",
                },
            },
            required: ["row", "rowId"],
        },
        outputs: {
            properties: {
                row: {
                    type: "object",
                    customType: "row",
                    description: "The updated row",
                },
                response: {
                    type: "object",
                    description: "The response from the table",
                },
                success: {
                    type: "boolean",
                    description: "Whether the action was successful",
                },
                id: {
                    type: "string",
                    description: "The identifier of the updated row",
                },
                revision: {
                    type: "string",
                    description: "The revision of the updated row",
                },
            },
            required: ["success", "id", "revision"],
        },
    },
};
exports.run = function ({ inputs, appId, emitter }) {
    return __awaiter(this, void 0, void 0, function* () {
        if (inputs.rowId == null || inputs.row == null) {
            return {
                success: false,
                response: {
                    message: "Invalid inputs",
                },
            };
        }
        const tableId = inputs.row.tableId;
        // clear any falsy properties so that they aren't updated
        for (let propKey of Object.keys(inputs.row)) {
            if (!inputs.row[propKey] || inputs.row[propKey] === "") {
                delete inputs.row[propKey];
            }
        }
        // have to clean up the row, remove the table from it
        const ctx = buildCtx(appId, emitter, {
            body: Object.assign(Object.assign({}, inputs.row), { _id: inputs.rowId }),
            params: {
                rowId: inputs.rowId,
                tableId: tableId,
            },
        });
        try {
            if (tableId) {
                inputs.row = yield automationUtils.cleanUpRow(tableId, inputs.row);
            }
            yield rowController.patch(ctx);
            return {
                row: ctx.body,
                response: ctx.message,
                id: ctx.body._id,
                revision: ctx.body._rev,
                success: ctx.status === 200,
            };
        }
        catch (err) {
            return {
                success: false,
                response: automationUtils.getError(err),
            };
        }
    });
};
