import Action from "./Action";

// Value Object
export default class Ngram {
    private readonly _ngram: Action[];
    private _occurence: number;
    private _successorAction: Map<string, number>;

    constructor(ngram: Action[], occurence: number) {
        if (ngram === null || ngram === undefined) {
            throw new Error("Cannot create Ngram with no array of action");
        }
        if (! Array.isArray(ngram)) {
            throw new Error("Cannot create Ngram with no array of action");
        }
        this._ngram = ngram;

        if (occurence === null || occurence === undefined) {
            throw new Error("Cannot create Ngram with no occurence");
        }
        this._occurence = occurence;
        this._successorAction = new Map();
    }

    get n(): number {
        return this._ngram.length;
    }

    get key(): string {
        return this._ngram.map((action) => action.key).join(" -> ");
    }

    get ngram(): Action[] {
        return [...this._ngram];
    }

    get occurence(): number {
        return this._occurence;
    }

    get successorAction(): Map<string, number> {
        return new Map(this._successorAction);
    }

    public addSuccessorAction(action: Action, occurence: number): void {
        this._successorAction.set(action.key, occurence);
    }
}
