import Comment from "./Comment";
import Action from "./Action";

export default class Exploration {

    interactions: (Action | Comment)[];

    constructor() {
        this.interactions = [];
    }

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

    get isEmpty(): boolean {
        return this.interactions.length <= 1;
    }

    get actions(): Action[] {
        return this.interactions.filter(interaction => interaction instanceof Action)
    }

    addAction(kind: string, value?: string): void {
        this.interactions.push(new Action(kind, value, this.interactions.length));
    }

    addComment(kind: string, value: string) : void{
        this.interactions.push(new Comment(kind, value, this.interactions.length));
    }

    reset(): void {
        this.interactions = [];
    }

    start(): void {
        if (this.actions.length === 0) {
            this.addAction("start");
        }
    }

    toJSON() {
        return this.interactions.map(interaction => {
            if (interaction instanceof Comment) {
                return (
                    {
                        concreteType: "Comment",
                        kind: interaction.kind,
                        value: interaction.value
                    }
                )
            } else if (interaction instanceof Action) {
                return (
                    {
                        concreteType: "Action",
                        kind: interaction.kind,
                        value: interaction.value
                    }
                )
            }
        })
    }

}