
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(webSiteId: string, stepList: Array<{name: string, expression: string}>): Promise<void> {
        return this.sequenceEvaluatorRepository.createSequenceEvaluator(webSiteId, stepList)
            .then((_sequenceEvaluator) => {return; });
    }

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

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

    public getSequenceEvaluator(webSiteId: string): Promise<SequenceEvaluator> {
        if (webSiteId === undefined || webSiteId === null) {
            return Promise.resolve(null);
        }
        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);
        });
    }

}
