import { AppObjectUC, getSingletonComponent, } from "@vived/core";
import { AppDispatcher } from "../Entities/AppDispatcher";
export class GetAssetsForOwnerUC extends AppObjectUC {
    constructor() {
        super(...arguments);
        this.requestType = "GET_ASSET_FOR_OWNER";
    }
}
GetAssetsForOwnerUC.type = "GetAssetsForOwnerUC";
GetAssetsForOwnerUC.get = (appObjects) => getSingletonComponent(GetAssetsForOwnerUC.type, appObjects);
export function makeGetAssetsForOwnerUC(appObject) {
    return new GetLinkedAssetsUCImp(appObject);
}
export 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.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