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