import { AppObjectUC, getSingletonComponent, } from "@vived/core";
import { AppDispatcher } from "../Entities/AppDispatcher";
export class GetAssetMetaUC extends AppObjectUC {
    constructor() {
        super(...arguments);
        this.requestType = "GET_ASSET_META";
    }
}
GetAssetMetaUC.type = "GetAssetMetaUC";
GetAssetMetaUC.get = (appObjects) => getSingletonComponent(GetAssetMetaUC.type, appObjects);
export function makeGetAssetMetaUC(appObject) {
    return new GetAppAssetsURLUCImp(appObject);
}
export function getAssetMeta(assetID, appObjects) {
    const dispatchUC = GetAssetMetaUC.get(appObjects);
    if (dispatchUC) {
        return dispatchUC.doDispatch(assetID);
    }
    else {
        appObjects.submitWarning("getAssetBlobURL", "Can't find GetAssetMetaUC");
        return Promise.reject();
    }
}
class GetAppAssetsURLUCImp extends GetAssetMetaUC {
    constructor(appObject) {
        super(appObject, GetAssetMetaUC.type);
        this.requestVersion = 1;
        this.doDispatch = (assetID) => {
            return new Promise((resolve, reject) => {
                const callback = (assetMeta) => {
                    if (assetMeta === undefined) {
                        reject(new Error("Meta is undefined"));
                    }
                    else {
                        resolve(assetMeta);
                    }
                };
                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("GetAssetMetaUC has been added to an App Object that does not have a AppDispatcher. Add a dispatcher first");
            this.dispose();
        }
    }
}
//# sourceMappingURL=GetAssetMetaUC.js.map