"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 maximal or minimal weight of single vote.
 * Negative values means flag, while positive means upvote.
 */
class WeightRule extends Rule_1.Rule {
    constructor(min, max) {
        super();
        this.rule = Rule_1.Rule.Type.Weight;
        this.min = min;
        this.max = max;
    }
    type() {
        return Rule_1.Rule.Type.Weight;
    }
    validate(voteorder, context) {
        return __awaiter(this, void 0, void 0, function* () {
            this.validateRuleObject(this);
            if (voteorder.weight < this.min)
                throw new ValidationException_1.ValidationException("Weight of vote (" + this.weightToPercent(voteorder.weight) + ") is lower than  " + this.weightToPercent(this.min));
            else if (voteorder.weight > this.max)
                throw new ValidationException_1.ValidationException("Weight of vote (" + this.weightToPercent(voteorder.weight) + ") is higher than (" + this.weightToPercent(this.max));
        });
    }
    validateRuleObject(unprototypedObj) {
        ["min", "max"].forEach(prop => {
            if (!_.has(unprototypedObj, prop))
                throw new ValidationException_1.ValidationException("WeightRule: property " + prop + " is missing");
        });
        if (Math.abs(unprototypedObj.min) > 10000)
            throw new ValidationException_1.ValidationException("WeightRule: absolute value of .min ( " + unprototypedObj.min + " ) is > 10000");
        if (Math.abs(unprototypedObj.max) > 10000)
            throw new ValidationException_1.ValidationException("WeightRule: absolute value of .max ( " + unprototypedObj.max + " ) is > 10000");
    }
    getDescription() {
        return (this.min < 0 ? "Can flag: "
            + Math.abs(Math.min(0, this.max)) / 100 + " - " + Math.abs(this.min) / 100 + " %"
            : "(cannot flag)")
            + "; " + // separator
            (this.max > 0 ? "Can upvote: "
                + Math.max(0, this.min) / 100 + " - " + this.max / 100 + " %"
                : "(cannot upvote)");
    }
    weightToPercent(weight) {
        return (weight / 100).toFixed(2) + "%";
    }
}
exports.WeightRule = WeightRule;
//# sourceMappingURL=WeightRule.js.map