import * as fs from "fs";
import * as path from "path";
import ITraceRepository from "../domain/traceRepository";
import Word from "../domain/word";

const tracesDirectory = path.join(__dirname, "..", "..", "input");

export default class TraceRepositoryJSON implements ITraceRepository {

    public getTraces(filename: string): Word[][] {
        const data = JSON.parse(fs.readFileSync(path.join(tracesDirectory, filename), "utf8"));
        const tracesData = data;
        const traces = [];
        for (const traceData of tracesData) {
            traces.push(traceData.map((action) => {
                const split = action.split("$");
                if (split.length === 2) {
                    return new Word(split[0], split[1]);
                } else {
                    return new Word(split[0]);
                }
            }));
        }
        return traces;
    }

}
