import Rule from './Rule';

class RuleRepository {

    rulePrefixToRuleMap: Map<string, Rule[]>;

    constructor() {
        this.rulePrefixToRuleMap = new Map();
    }

    addRule(data) {
        const rule = new Rule(
            data.output.prefix,
            data.output.suffix,
            data.match.event,
            data.match.css,
            data.match.xpath,
            data.match.code,
            data.match.key);
        if (!this.rulePrefixToRuleMap.has(rule.prefix)) {
            this.rulePrefixToRuleMap.set(rule.prefix, [])
        }
        this.rulePrefixToRuleMap.get(rule.prefix).push(rule);
    }

    getRulesByPrefix(prefix) {
        return this.rulePrefixToRuleMap.get(prefix)
    }

    getRules() {
        const rulesArrayList = Array.from(this.rulePrefixToRuleMap.values());
        let rules = [];
        rulesArrayList.forEach(ruleList => {
            rules = rules.concat([...ruleList]);
        })
        return rules;
    }

}

export default RuleRepository;