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