"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    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) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const _ = require("lodash");
const Rule_1 = require("./Rule");
const ValidationException_1 = require("../validation/ValidationException");
/**
 * This rule limits age of the post.
 */
class AgeOfPostRule extends Rule_1.Rule {
    constructor(mode, value, unit) {
        super();
        this.rule = Rule_1.Rule.Type.AgeOfPost;
        this.mode = mode;
        this.value = value;
        this.unit = unit;
    }
    type() {
        return Rule_1.Rule.Type.AgeOfPost;
    }
    validate(voteorder, context) {
        return __awaiter(this, void 0, void 0, function* () {
            this.validateRuleObject(this);
            let post;
            try {
                post = yield context.getPost();
            }
            catch (e) {
                if (e.notFoundException)
                    throw new ValidationException_1.ValidationException(e.message);
                else
                    throw e;
            }
            const unitMultiplier = (this.unit === AgeOfPostRule.TimeUnit.DAY ? 24 * 60 * 60 :
                (this.unit === AgeOfPostRule.TimeUnit.HOUR ? 60 * 60 :
                    (this.unit === AgeOfPostRule.TimeUnit.MINUTE ? 60 :
                        1)));
            const numberOfSeconds = this.value * unitMultiplier;
            const thresholdTime = new Date(Date.now() - numberOfSeconds * 1000);
            const postTime = new Date(post.created + "Z" /* this is UTC date */);
            if (this.mode === AgeOfPostRule.Mode.OLDER_THAN) {
                if (postTime.getTime() >= thresholdTime.getTime())
                    throw new ValidationException_1.ValidationException("AgeOfPostRule: Post (" + postTime + ") is older than " + thresholdTime);
            }
            else if (this.mode === AgeOfPostRule.Mode.YOUNGER_THAN) {
                if (postTime.getTime() <= thresholdTime.getTime())
                    throw new ValidationException_1.ValidationException("AgeOfPostRule: Post (" + postTime + ") is younger than " + thresholdTime);
            }
            else
                throw new ValidationException_1.ValidationException("AgeOfPostRule: Unknown mode " + this.mode);
        });
    }
    validateRuleObject(unprototypedObj) {
        ["mode", "value", "unit"].forEach(prop => {
            if (!_.has(unprototypedObj, prop))
                throw new ValidationException_1.ValidationException("AgeOfPostRule: property " + prop + " is missing");
        });
        if (!_.includes([
            AgeOfPostRule.TimeUnit.DAY, AgeOfPostRule.TimeUnit.HOUR,
            AgeOfPostRule.TimeUnit.MINUTE, AgeOfPostRule.TimeUnit.SECOND
        ], unprototypedObj.unit))
            throw new ValidationException_1.ValidationException("AgeOfPostRule: unknown unit " + unprototypedObj.unit);
        if (!_.includes([
            AgeOfPostRule.Mode.OLDER_THAN, AgeOfPostRule.Mode.YOUNGER_THAN,
        ], unprototypedObj.mode))
            throw new ValidationException_1.ValidationException("AgeOfPostRule: unknown mode " + unprototypedObj.mode);
    }
    getDescription() {
        return "Post "
            + (this.mode === "older_than" ? "older than" : "younger than")
            + this.value + " " + this.unit + "s";
    }
}
exports.AgeOfPostRule = AgeOfPostRule;
(function (AgeOfPostRule) {
    let TimeUnit;
    (function (TimeUnit) {
        TimeUnit["DAY"] = "day";
        TimeUnit["HOUR"] = "hour";
        TimeUnit["MINUTE"] = "minute";
        TimeUnit["SECOND"] = "second";
    })(TimeUnit = AgeOfPostRule.TimeUnit || (AgeOfPostRule.TimeUnit = {}));
    let Mode;
    (function (Mode) {
        Mode["OLDER_THAN"] = "older_than";
        Mode["YOUNGER_THAN"] = "younger_than";
    })(Mode = AgeOfPostRule.Mode || (AgeOfPostRule.Mode = {}));
})(AgeOfPostRule = exports.AgeOfPostRule || (exports.AgeOfPostRule = {}));
//# sourceMappingURL=AgeOfPostRule.js.map