"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.GetAssetBlobURLUC = void 0;
exports.makeGetAssetBlobURLUC = makeGetAssetBlobURLUC;
exports.getAssetBlobURL = getAssetBlobURL;
const core_1 = require("@vived/core");
const AppDispatcher_1 = require("../Entities/AppDispatcher");
class GetAssetBlobURLUC extends core_1.AppObjectUC {
    constructor() {
        super(...arguments);
        this.requestType = "GET_ASSET_BLOB";
    }
}
exports.GetAssetBlobURLUC = GetAssetBlobURLUC;
GetAssetBlobURLUC.type = "GetAppAssetsURLUC";
GetAssetBlobURLUC.get = (appObjects) => (0, core_1.getSingletonComponent)(GetAssetBlobURLUC.type, appObjects);
function makeGetAssetBlobURLUC(appObject) {
    return new GetAppAssetsURLUCImp(appObject);
}
function getAssetBlobURL(assetID, appObjects) {
    const dispatchUC = GetAssetBlobURLUC.get(appObjects);
    if (dispatchUC) {
        return dispatchUC.doDispatch(assetID);
    }
    else {
        appObjects.submitWarning("getAssetBlobURL", "Can't find GetAssetBlobURLUC");
        return Promise.reject();
    }
}
class GetAppAssetsURLUCImp extends GetAssetBlobURLUC {
    constructor(appObject) {
        super(appObject, GetAssetBlobURLUC.type);
        this.requestVersion = 1;
        this.doDispatch = (assetID) => {
            return new Promise((resolve, reject) => {
                const callback = (blobURL) => {
                    if (blobURL === undefined) {
                        reject(new Error("Blob URL is undefined"));
                    }
                    else {
                        resolve(blobURL);
                    }
                };
                const payload = {
                    assetID,
                    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(AppDispatcher_1.AppDispatcher.type);
        if (!this.appDispatcher) {
            this.warn("GetAssetBlobURLUC has been added to an App Object that does not have a AppDispatcher. Add a dispatcher first");
            this.dispose();
        }
    }
}
//# sourceMappingURL=GetAssetBlobURLUC.js.map