"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.IsAssetFetchedUC = void 0;
exports.makeIsAssetFetchedUC = makeIsAssetFetchedUC;
exports.isAssetFetched = isAssetFetched;
const core_1 = require("@vived/core");
const AppDispatcher_1 = require("../Entities/AppDispatcher");
class IsAssetFetchedUC extends core_1.AppObjectUC {
    constructor() {
        super(...arguments);
        this.requestType = "IS_ASSET_FILE_FETCHED";
    }
}
exports.IsAssetFetchedUC = IsAssetFetchedUC;
IsAssetFetchedUC.type = "IsAssetFetchedUC";
IsAssetFetchedUC.get = (appObjects) => (0, core_1.getSingletonComponent)(IsAssetFetchedUC.type, appObjects);
function makeIsAssetFetchedUC(appObject) {
    return new GetAppAssetsURLUCImp(appObject);
}
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_1.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