import ComponentCode from "../../ComponentCode";
import { LIMIT_PER_PROJECT } from "../../../../../Types/Database/LimitMax";
import BadDataException from "../../../../../Types/Exception/BadDataException";
import JSONFunctions from "../../../../../Types/JSONFunctions";
import PositiveNumber from "../../../../../Types/PositiveNumber";
import Text from "../../../../../Types/Text";
import BaseModelComponents from "../../../../../Types/Workflow/Components/BaseModel";
export default class DeleteManyBaseModel extends ComponentCode {
    constructor(modelService) {
        super();
        this.modelService = null;
        const BaseModelComponent = BaseModelComponents.getComponents(modelService.getModel()).find((i) => {
            return (i.id ===
                `${Text.pascalCaseToDashes(modelService.getModel().tableName)}-delete-many`);
        });
        if (!BaseModelComponent) {
            throw new BadDataException("Delete many component for " +
                modelService.getModel().tableName +
                " not found.");
        }
        this.setMetadata(BaseModelComponent);
        this.modelService = modelService;
    }
    async run(args, options) {
        const successPort = this.getMetadata().outPorts.find((p) => {
            return p.id === "success";
        });
        if (!successPort) {
            throw options.onError(new BadDataException("Success port not found"));
        }
        const errorPort = this.getMetadata().outPorts.find((p) => {
            return p.id === "error";
        });
        if (!errorPort) {
            throw options.onError(new BadDataException("Error port not found"));
        }
        try {
            if (!this.modelService) {
                throw options.onError(new BadDataException("modelService is undefined."));
            }
            if (!args["query"]) {
                throw options.onError(new BadDataException("Query is undefined."));
            }
            if (typeof args["query"] === "string") {
                args["query"] = JSONFunctions.parse(args["query"]);
            }
            if (typeof args["query"] !== "object") {
                throw options.onError(new BadDataException("Query is should be of type object."));
            }
            if (args["skip"] && typeof args["skip"] === "string") {
                args["skip"] = parseInt(args["skip"]);
            }
            if (args["limit"] && typeof args["limit"] === "string") {
                args["limit"] = parseInt(args["limit"]);
            }
            if (typeof args["skip"] !== "number") {
                args["skip"] = 0;
            }
            if (typeof args["limit"] !== "number") {
                args["limit"] = 10;
            }
            if (typeof args["limit"] === "number" &&
                args["limit"] > LIMIT_PER_PROJECT) {
                options.log("Limit cannot be " + args["limit"]);
                options.log("Setting the limit to " + LIMIT_PER_PROJECT);
                args["limit"] = LIMIT_PER_PROJECT;
            }
            if (this.modelService.getModel().getTenantColumn()) {
                args["query"][this.modelService.getModel().getTenantColumn()] = options.projectId;
            }
            let query = args["query"];
            if (query) {
                query = JSONFunctions.deserialize(args["query"]);
            }
            await this.modelService.deleteBy({
                query: query || {},
                limit: new PositiveNumber(args["limit"]),
                skip: new PositiveNumber(args["skip"]),
                props: {
                    isRoot: true,
                    tenantId: options.projectId,
                },
            });
            return {
                returnValues: {},
                executePort: successPort,
            };
        }
        catch (err) {
            options.log("Error running component");
            options.log(err.message ? err.message : JSON.stringify(err, null, 2));
            return {
                returnValues: {},
                executePort: errorPort,
            };
        }
    }
}
//# sourceMappingURL=DeleteManyBaseModel.js.map