"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 PayoutRule extends Rule_1.Rule {
    constructor(mode, value) {
        super();
        this.rule = Rule_1.Rule.Type.Payout;
        this.mode = mode;
        this.value = value;
    }
    type() {
        return Rule_1.Rule.Type.Payout;
    }
    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 payout = PayoutRule._parsePayout(post.total_payout_value);
            if (this.mode == PayoutRule.Mode.EQUAL) {
                if (payout !== this.value)
                    throw new ValidationException_1.ValidationException("Payout rule: payout (" + payout + ") does not equal " + this.value);
            }
            else if (this.mode == PayoutRule.Mode.MORE_THAN) {
                if (payout <= this.value)
                    throw new ValidationException_1.ValidationException("Payout rule: payout (" + payout + ") is not more than " + this.value);
            }
            else if (this.mode == PayoutRule.Mode.LESS_THAN) {
                if (payout >= this.value)
                    throw new ValidationException_1.ValidationException("Payout rule: payout (" + payout + ") is not less than " + this.value);
            }
            else {
                throw new Error("Unknown mode of payout rule: " + this.mode);
            }
        });
    }
    validateRuleObject(unprototypedObj) {
        ["value", "mode"].forEach(prop => {
            if (!_.has(unprototypedObj, prop))
                throw new ValidationException_1.ValidationException("PayoutRule: property " + prop + " is missing");
        });
        if (!_.includes([PayoutRule.Mode.MORE_THAN, PayoutRule.Mode.LESS_THAN, PayoutRule.Mode.EQUAL], unprototypedObj.mode))
            throw new ValidationException_1.ValidationException("PayoutRule: unknown mode " + unprototypedObj.mode);
    }
    static _parsePayout(payoutStr) {
        const regex = /^([0-9]+\.?[0-9]*) SBD$/gm;
        const matches = regex.exec(payoutStr);
        if (matches && matches.length > 1) {
            return parseFloat(matches[1]);
        }
        else
            throw new Error("PayoutRule: cannot parse payout (" + payoutStr + ")");
    }
    getDescription() {
        let out = "Payout of the post ";
        switch (this.mode) {
            case PayoutRule.Mode.MORE_THAN:
                out += "is more than";
                break;
            case PayoutRule.Mode.LESS_THAN:
                out += "is less than";
                break;
            case PayoutRule.Mode.EQUAL:
                out += "equals";
                break;
            default: out += this.mode;
        }
        out += " " + this.value + " SBD";
        return out;
    }
}
exports.PayoutRule = PayoutRule;
(function (PayoutRule) {
    let Mode;
    (function (Mode) {
        Mode["MORE_THAN"] = "more_than";
        Mode["LESS_THAN"] = "less_than";
        Mode["EQUAL"] = "equal";
    })(Mode = PayoutRule.Mode || (PayoutRule.Mode = {}));
})(PayoutRule = exports.PayoutRule || (exports.PayoutRule = {}));
//# sourceMappingURL=PayoutRule.js.map