import { AppObjectUC, getSingletonComponent, } from "@vived/core";
import { AppDispatcher } from "../Entities/AppDispatcher";
export class GetAssetBlobURLUC extends AppObjectUC {
    constructor() {
        super(...arguments);
        this.requestType = "GET_ASSET_BLOB";
    }
}
GetAssetBlobURLUC.type = "GetAppAssetsURLUC";
GetAssetBlobURLUC.get = (appObjects) => getSingletonComponent(GetAssetBlobURLUC.type, appObjects);
export function makeGetAssetBlobURLUC(appObject) {
    return new GetAppAssetsURLUCImp(appObject);
}
export 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.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