import * as fs from "fs";
import * as path from "path";
import { Rule } from "../domain/rule";
import IRuleRepository from "../domain/ruleRepository";

export default class RuleRepositoryJSON implements IRuleRepository {

    public ruleDirectory: string;

    constructor(ruleDirectory) {
        this.ruleDirectory = ruleDirectory;
    }

    public getRulesByName(name: string): Rule[] {
        const filename = name + ".json";
        const rulesdata = JSON.parse(fs.readFileSync(path.join(this.ruleDirectory, filename), "utf8"));
        const rules = rulesdata.map((data) =>
            new Rule(data.match.event, data.output.prefix, data.output.suffix, data.match.selector, data.match.code));
        rules.push(new Rule(null, "start", null));
        rules.push(new Rule(null, "end",  null));

        return rules;
    }

}
