"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.NO_ASSET_ID_ERROR = exports.SubmitActivityAssetUC = void 0;
exports.makeSubmitActivityAssetUC = makeSubmitActivityAssetUC;
exports.submitActivityAsset = submitActivityAsset;
const core_1 = require("@vived/core");
const Entities_1 = require("../Entities");
class SubmitActivityAssetUC extends core_1.AppObjectUC {
    constructor() {
        super(...arguments);
        this.requestType = "SUBMIT_ACTIVITY_ASSET";
    }
}
exports.SubmitActivityAssetUC = SubmitActivityAssetUC;
SubmitActivityAssetUC.type = "SubmitActivityAssetUC";
SubmitActivityAssetUC.get = (appObjects) => (0, core_1.getSingletonComponent)(SubmitActivityAssetUC.type, appObjects);
function makeSubmitActivityAssetUC(appObject) {
    return new SubmitActivityAssetUCImp(appObject);
}
function submitActivityAsset(assetFile, appObjects) {
    const dispatchUC = SubmitActivityAssetUC.get(appObjects);
    if (dispatchUC) {
        return dispatchUC.doDispatch(assetFile);
    }
    else {
        appObjects.submitWarning("submitActivityAsset", "Can't find SubmitActivityAssetUC");
        return Promise.reject();
    }
}
class SubmitActivityAssetUCImp extends SubmitActivityAssetUC {
    constructor(appObject) {
        super(appObject, SubmitActivityAssetUC.type);
        this.requestVersion = 1;
        this.doDispatch = (assetFile) => {
            return new Promise((resolve, reject) => {
                const callback = (assetID) => {
                    if (assetID === undefined) {
                        reject(exports.NO_ASSET_ID_ERROR);
                    }
                    else {
                        resolve(assetID);
                    }
                };
                const payload = {
                    assetFile,
                    callback,
                };
                if (!this.appDispatcher)
                    return reject(new Error("No host handler has been registered"));
                else {
                    this.appDispatcher.formRequestAndDispatch(this.requestType, this.requestVersion, payload);
                }
            });
        };
        this.appDispatcher = appObject.getComponent(Entities_1.AppDispatcher.type);
        if (!this.appDispatcher) {
            this.warn("SubmitActivityAssetUC has been added to an App Object that does not have a AppDispatcher. Add a dispatcher first");
            this.dispose();
        }
        this.appObjects.registerSingleton(this);
    }
}
exports.NO_ASSET_ID_ERROR = new Error("Asset ID is undefined");
//# sourceMappingURL=SubmitActivityAssetUC.js.map