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