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