"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 TagsRule extends Rule_1.Rule {
    constructor(mode, tags) {
        super();
        this.rule = Rule_1.Rule.Type.Tags;
        this.mode = mode;
        this.tags = tags;
    }
    type() {
        return Rule_1.Rule.Type.Tags;
    }
    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 postMetadata = JSON.parse(post.json_metadata);
            if (this.mode === TagsRule.Mode.ALLOW) { // allow mode (every post tag must be within this list)
                for (let i = 0; i < postMetadata.tags.length; i++) {
                    const tag = postMetadata.tags[i];
                    if (this.tags.indexOf(tag) === -1)
                        throw new ValidationException_1.ValidationException("Tag " + tag + " is not on the allowed tags list [" + this.tags.join() + "].");
                }
            }
            else if (this.mode === TagsRule.Mode.DENY) { // deny mode (none of post tags can be on this list)
                for (let i = 0; i < postMetadata.tags.length; i++) {
                    const tag = postMetadata.tags[i];
                    if (this.tags.indexOf(tag) !== -1)
                        throw new ValidationException_1.ValidationException("Tag " + tag + " is on the denied tags list [" + this.tags.join() + "].");
                }
            }
            else if (this.mode === TagsRule.Mode.REQUIRE) { // the post should have all of the specified tags
                for (let i = 0; i < this.tags.length; i++) {
                    const tag = this.tags[i];
                    if (postMetadata.tags.indexOf(tag) === -1)
                        throw new ValidationException_1.ValidationException("The post tags [" + postMetadata.tags.join() + "] does not include " + tag + ".");
                }
            }
            else if (this.mode === TagsRule.Mode.ANY) { // the post should have at least one of the specified tags
                for (let i = 0; i < this.tags.length; i++) {
                    const tag = this.tags[i];
                    if (postMetadata.tags.indexOf(tag) !== -1) {
                        return;
                    }
                }
                throw new ValidationException_1.ValidationException("None of the tags [" + postMetadata.tags.join() + "] is on the \"any\" tags list [" + this.tags.join() + "].");
            }
            else
                throw new ValidationException_1.ValidationException("Unknown mode in tags.");
        });
    }
    validateRuleObject(unprototypedObj) {
        ["tags", "mode"].forEach(prop => {
            if (!_.has(unprototypedObj, prop))
                throw new ValidationException_1.ValidationException("TagsRule: property " + prop + " is missing");
        });
        if (!_.includes([TagsRule.Mode.ALLOW, TagsRule.Mode.DENY, TagsRule.Mode.ANY, TagsRule.Mode.REQUIRE], unprototypedObj.mode))
            throw new ValidationException_1.ValidationException("TagsRule: unknown mode " + unprototypedObj.mode);
    }
    getDescription() {
        if (this.mode === TagsRule.Mode.ALLOW) {
            return "Allow only tags: " + this.tags.join(", ");
        }
        else if (this.mode === TagsRule.Mode.DENY) {
            return "Deny tags: " + this.tags.join(", ");
        }
        else if (this.mode === TagsRule.Mode.REQUIRE) {
            return "Require all of tags: " + this.tags.join(", ");
        }
        else if (this.mode === TagsRule.Mode.ANY) {
            return "Require at least one of tags: " + this.tags.join(", ");
        }
        else {
            return "[Unknown mode " + this.mode + "] tags: " + this.tags.join(", ");
        }
    }
}
exports.TagsRule = TagsRule;
(function (TagsRule) {
    let Mode;
    (function (Mode) {
        Mode["ALLOW"] = "allow";
        Mode["DENY"] = "deny";
        Mode["ANY"] = "any";
        Mode["REQUIRE"] = "require";
    })(Mode = TagsRule.Mode || (TagsRule.Mode = {}));
})(TagsRule = exports.TagsRule || (exports.TagsRule = {}));
//# sourceMappingURL=TagsRule.js.map