
import InteractionFactory from "../domain/InteractionFactory";
import SequenceEvaluation from "../domain/SequenceEvaluation";
import SequenceEvaluator from "../domain/SequenceEvaluator";
import SequenceEvaluatorRepository from "../domain/SequenceEvaluatorRepository";

export default class SequenceEvaluationApplication {

    public sequenceEvaluatorRepository: SequenceEvaluatorRepository;

    constructor(sequenceEvaluatorRepository: SequenceEvaluatorRepository) {
        this.sequenceEvaluatorRepository = sequenceEvaluatorRepository;
    }

    public createSequenceEvaluator(name: string, webSiteId: string, stepList: Array<{name: string, expression: string}>): Promise<void> {
        return this.sequenceEvaluatorRepository.createSequenceEvaluator(name, webSiteId, stepList)
            .then((_sequenceEvaluator) => {return; });
    }

    public updateSequenceEvaluator(name: string, webSiteId: string, stepList: Array<{name: string, expression: string}>): Promise<void> {
        return this.sequenceEvaluatorRepository.updateSequenceEvaluator(name, webSiteId, stepList)
        .then(() => {return; });
    }

    public removeSequenceEvaluator(webSiteId: string): Promise<void> {
        return this.sequenceEvaluatorRepository.removeSequenceEvaluator(webSiteId)
        .then(() => {return; });
    }

    public getSequenceEvaluator(webSiteId: string): Promise<SequenceEvaluator> {
        return this.sequenceEvaluatorRepository.getSequenceEvaluatorByWebSiteId(webSiteId).then((evaluator) => {
            return evaluator;
        });
    }

    public evaluateSequence(webSiteId: string, sequence: string[]): Promise<SequenceEvaluation | null> {
        const interactionList = sequence.map((interactionText) => InteractionFactory.parseInteraction(interactionText));
        return this.sequenceEvaluatorRepository.getSequenceEvaluatorByWebSiteId(webSiteId)
        .then((evaluator) => {
            if (!evaluator) {
                return null;
            }
            return evaluator.evaluate(interactionList);
        });
    }

}
