"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.GetLinkedAssetsUC = void 0;
exports.makeGetLinkedAssetsUC = makeGetLinkedAssetsUC;
exports.getLinkedAssets = getLinkedAssets;
const core_1 = require("@vived/core");
const AppDispatcher_1 = require("../Entities/AppDispatcher");
class GetLinkedAssetsUC extends core_1.AppObjectUC {
    constructor() {
        super(...arguments);
        this.requestType = "GET_LINKED_ASSETS";
    }
}
exports.GetLinkedAssetsUC = GetLinkedAssetsUC;
GetLinkedAssetsUC.type = "GetLinkedAssetsUC";
GetLinkedAssetsUC.get = (appObjects) => (0, core_1.getSingletonComponent)(GetLinkedAssetsUC.type, appObjects);
function makeGetLinkedAssetsUC(appObject) {
    return new GetLinkedAssetsUCImp(appObject);
}
function getLinkedAssets(assetID, type, appObjects) {
    const dispatchUC = GetLinkedAssetsUC.get(appObjects);
    if (dispatchUC) {
        return dispatchUC.doDispatch(assetID, type);
    }
    else {
        appObjects.submitWarning("getAssetBlobURL", "Can't find GetAssetMetaUC");
        return Promise.reject();
    }
}
class GetLinkedAssetsUCImp extends GetLinkedAssetsUC {
    constructor(appObject) {
        super(appObject, GetLinkedAssetsUC.type);
        this.requestVersion = 1;
        this.doDispatch = (assetID, type) => {
            return new Promise((resolve, reject) => {
                const callback = (linkedAssets) => {
                    if (linkedAssets === undefined) {
                        reject(new Error("Linked asset list is missing"));
                    }
                    else {
                        resolve(linkedAssets);
                    }
                };
                const payload = {
                    assetID,
                    type,
                    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("GetLinkedAssetsUC has been added to an App Object that does not have a AppDispatcher. Add a dispatcher first");
            this.dispose();
        }
    }
}
//# sourceMappingURL=GetLinkedAssetsUC.js.map