"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");
class VotesCountRule extends Rule_1.Rule {
    constructor(mode, value) {
        super();
        this.rule = Rule_1.Rule.Type.VotesCount;
        this.mode = mode;
        this.value = value;
    }
    type() {
        return Rule_1.Rule.Type.VotesCount;
    }
    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 voteCount = post.active_votes.length;
            if (this.mode == VotesCountRule.Mode.EQUAL) {
                if (voteCount !== this.value)
                    throw new ValidationException_1.ValidationException("Post votes count (" + voteCount + ") does not equal " + this.value);
            }
            else if (this.mode == VotesCountRule.Mode.MORE_THAN) {
                if (voteCount <= this.value)
                    throw new ValidationException_1.ValidationException("Post votes count (" + voteCount + ") is not more than " + this.value);
            }
            else if (this.mode == VotesCountRule.Mode.LESS_THAN) {
                if (voteCount >= this.value)
                    throw new ValidationException_1.ValidationException("Post votes count (" + voteCount + ") is not less than " + this.value);
            }
            else {
                throw new Error("Unknown mode of votes count rule: " + this.mode);
            }
        });
    }
    validateRuleObject(unprototypedObj) {
        ["value", "mode"].forEach(prop => {
            if (!_.has(unprototypedObj, prop))
                throw new ValidationException_1.ValidationException("VotesCountRule: property " + prop + " is missing");
        });
        if (!_.includes([VotesCountRule.Mode.MORE_THAN, VotesCountRule.Mode.LESS_THAN, VotesCountRule.Mode.EQUAL], unprototypedObj.mode))
            throw new ValidationException_1.ValidationException("VotesCountRule: unknown mode " + unprototypedObj.mode);
    }
    getDescription() {
        let out = "The post has ";
        switch (this.mode) {
            case VotesCountRule.Mode.MORE_THAN:
                out += "more than";
                break;
            case VotesCountRule.Mode.LESS_THAN:
                out += "less than";
                break;
            case VotesCountRule.Mode.EQUAL:
                out += "";
                break;
            default: out += this.mode;
        }
        out += " " + this.value + " votes";
        return out;
    }
}
exports.VotesCountRule = VotesCountRule;
(function (VotesCountRule) {
    let Mode;
    (function (Mode) {
        Mode["MORE_THAN"] = "more_than";
        Mode["LESS_THAN"] = "less_than";
        Mode["EQUAL"] = "equal";
    })(Mode = VotesCountRule.Mode || (VotesCountRule.Mode = {}));
})(VotesCountRule = exports.VotesCountRule || (exports.VotesCountRule = {}));
//# sourceMappingURL=VotesCountRule.js.map