"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SubmitResultResultUC = void 0;
exports.makeSubmitResultUC = makeSubmitResultUC;
exports.submitHitResult = submitHitResult;
exports.submitProgressResult = submitProgressResult;
exports.submitQualityResult = submitQualityResult;
exports.submitScoreResult = submitScoreResult;
exports.submitMultiHitResult = submitMultiHitResult;
const core_1 = require("@vived/core");
const AppDispatcher_1 = require("../Entities/AppDispatcher");
class SubmitResultResultUC extends core_1.AppObjectUC {
    constructor() {
        super(...arguments);
        this.requestType = "SUBMIT_RESULTS";
    }
}
exports.SubmitResultResultUC = SubmitResultResultUC;
SubmitResultResultUC.type = "SubmitResultResultUC";
SubmitResultResultUC.get = (appObjects) => (0, core_1.getSingletonComponent)(SubmitResultResultUC.type, appObjects);
function makeSubmitResultUC(appObject) {
    return new SubmitResultResultUCImp(appObject);
}
function submitHitResult(data, appObjects) {
    const dispatchUC = SubmitResultResultUC.get(appObjects);
    if (dispatchUC) {
        dispatchUC.submitHitResult(data);
    }
    else {
        appObjects.submitWarning("submitHitResult", "Can't find SubmitResultResultUC");
    }
}
function submitProgressResult(data, appObjects) {
    const dispatchUC = SubmitResultResultUC.get(appObjects);
    if (dispatchUC) {
        dispatchUC.submitProgressResult(data);
    }
    else {
        appObjects.submitWarning("submitProgressResult", "Can't find SubmitResultResultUC");
    }
}
function submitQualityResult(data, appObjects) {
    const dispatchUC = SubmitResultResultUC.get(appObjects);
    if (dispatchUC) {
        dispatchUC.submitQualityResult(data);
    }
    else {
        appObjects.submitWarning("submitQualityResult", "Can't find SubmitResultResultUC");
    }
}
function submitScoreResult(data, appObjects) {
    const dispatchUC = SubmitResultResultUC.get(appObjects);
    if (dispatchUC) {
        dispatchUC.submitScoreResult(data);
    }
    else {
        appObjects.submitWarning("submitScoreResult", "Can't find SubmitResultResultUC");
    }
}
function submitMultiHitResult(data, appObjects) {
    const dispatchUC = SubmitResultResultUC.get(appObjects);
    if (dispatchUC) {
        dispatchUC.submitMultiHitResult(data);
    }
    else {
        appObjects.submitWarning("submitMultiHitResult", "Can't find SubmitResultResultUC");
    }
}
class SubmitResultResultUCImp extends SubmitResultResultUC {
    constructor(appObject) {
        super(appObject, SubmitResultResultUC.type);
        this.requestVersion = 3;
        this.submitHitResult = (data) => {
            if (!this.appDispatcher)
                return;
            const { description, success } = data;
            const payload = {
                resultType: "HIT_V1",
                description,
                result: {
                    success,
                },
            };
            this.appDispatcher.formRequestAndDispatch("SUBMIT_RESULTS", 3, payload);
        };
        this.submitProgressResult = (data) => {
            if (!this.appDispatcher)
                return;
            const { description, progress } = data;
            const payload = {
                resultType: "PROGRESS_V1",
                description,
                result: {
                    progress,
                },
            };
            this.appDispatcher.formRequestAndDispatch("SUBMIT_RESULTS", 3, payload);
        };
        this.submitMultiHitResult = (data) => {
            if (!this.appDispatcher)
                return;
            const { description, hits, misses, unanswered } = data;
            const payload = {
                resultType: "MULTIHIT_V1",
                description,
                result: {
                    hits,
                    misses,
                    unanswered,
                },
            };
            this.appDispatcher.formRequestAndDispatch("SUBMIT_RESULTS", 3, payload);
        };
        this.submitQualityResult = (data) => {
            if (!this.appDispatcher)
                return;
            const { description, stars, maxStars } = data;
            const payload = {
                resultType: "QUALITY_V1",
                description,
                result: {
                    stars,
                    maxStars,
                },
            };
            this.appDispatcher.formRequestAndDispatch("SUBMIT_RESULTS", 3, payload);
        };
        this.submitScoreResult = (data) => {
            if (!this.appDispatcher)
                return;
            const { description, score, maxScore } = data;
            const payload = {
                resultType: "SCORE_V1",
                description,
                result: {
                    score,
                    maxScore,
                },
            };
            this.appDispatcher.formRequestAndDispatch("SUBMIT_RESULTS", 3, payload);
        };
        this.appDispatcher = appObject.getComponent(AppDispatcher_1.AppDispatcher.type);
        if (!this.appDispatcher) {
            this.warn("DispatchStateChangeUC has been added to an App Object that does not have a AppDispatcher. Add a dispatcher first");
            this.dispose();
        }
    }
}
//# sourceMappingURL=SubmitResultResultUC.js.map