var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
import ComponentCode from "../ComponentCode";
import BadDataException from "../../../../Types/Exception/BadDataException";
import ComponentID from "../../../../Types/Workflow/ComponentID";
import SleepComponents from "../../../../Types/Workflow/Components/Sleep";
import CaptureSpan from "../../../Utils/Telemetry/CaptureSpan";
/**
 * Maximum duration a single Sleep component may suspend for. Bounds how far in
 * the future a delayed resume job can be parked.
 */
export const MAX_SLEEP_IN_MS = 30 * 24 * 60 * 60 * 1000; // 30 days
export default class Sleep extends ComponentCode {
    constructor() {
        super();
        const SleepComponent = SleepComponents.find((i) => {
            return i.id === ComponentID.Sleep;
        });
        if (!SleepComponent) {
            throw new BadDataException("Component not found.");
        }
        this.setMetadata(SleepComponent);
    }
    /**
     * Coerce a workflow argument (which may arrive as a string from the form, a
     * number, or null) into a non-negative integer. Invalid / negative values
     * are treated as 0.
     */
    toNonNegativeNumber(value) {
        const parsed = typeof value === "number" ? value : parseFloat(value);
        if (isNaN(parsed) || !isFinite(parsed) || parsed < 0) {
            return 0;
        }
        return parsed;
    }
    async run(args, options) {
        const outPort = this.getMetadata().outPorts.find((p) => {
            return p.id === "out";
        });
        if (!outPort) {
            throw options.onError(new BadDataException("Out port not found"));
        }
        const days = this.toNonNegativeNumber(args["days"]);
        const hours = this.toNonNegativeNumber(args["hours"]);
        const minutes = this.toNonNegativeNumber(args["minutes"]);
        const seconds = this.toNonNegativeNumber(args["seconds"]);
        let sleepForMs = days * 24 * 60 * 60 * 1000 +
            hours * 60 * 60 * 1000 +
            minutes * 60 * 1000 +
            seconds * 1000;
        sleepForMs = Math.round(sleepForMs);
        if (sleepForMs > MAX_SLEEP_IN_MS) {
            options.log(`Requested sleep of ${sleepForMs}ms exceeds the maximum of ${MAX_SLEEP_IN_MS}ms. Clamping to the maximum.`);
            sleepForMs = MAX_SLEEP_IN_MS;
        }
        if (sleepForMs <= 0) {
            // Nothing to sleep for — behave as a pass-through.
            options.log("Sleep duration is zero. Continuing to the next step immediately.");
            return Promise.resolve({
                returnValues: {},
                executePort: outPort,
            });
        }
        options.log(`Workflow will sleep for ${sleepForMs}ms and then continue to the next step.`);
        return Promise.resolve({
            returnValues: {},
            executePort: outPort,
            suspendForMs: sleepForMs,
        });
    }
}
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object, Object]),
    __metadata("design:returntype", Promise)
], Sleep.prototype, "run", null);
//# sourceMappingURL=Sleep.js.map