import { AppObjectUC, getSingletonComponent } from "@vived/core";
import { AppDispatcher } from "../Entities/AppDispatcher";
export class GetAssetFolderURLUC extends AppObjectUC {
    constructor() {
        super(...arguments);
        this.requestType = "GET_APP_ASSET_URL";
    }
}
GetAssetFolderURLUC.type = "GetAssetFolderURLUC";
GetAssetFolderURLUC.get = (appObjects) => getSingletonComponent(GetAssetFolderURLUC.type, appObjects);
export function makeGetAssetFolderURLUC(appObject) {
    return new GetAppAssetsURLUCImp(appObject);
}
export function getAssetFolderURL(appObjects) {
    const dispatchUC = GetAssetFolderURLUC.get(appObjects);
    if (dispatchUC) {
        return dispatchUC.doDispatch();
    }
    else {
        appObjects.submitWarning("getAssetFolderURL", "Can't find GetAppAssetsURLUC");
        return Promise.reject();
    }
}
class GetAppAssetsURLUCImp extends GetAssetFolderURLUC {
    constructor(appObject) {
        super(appObject, GetAssetFolderURLUC.type);
        this.requestVersion = 1;
        this.doDispatch = () => {
            return new Promise((resolve, reject) => {
                const callback = (url) => {
                    if (!url) {
                        reject(new Error("URL to asset folder is empty"));
                    }
                    else {
                        resolve(url);
                    }
                };
                const payload = {
                    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("GetAssetFolderURLUC has been added to an App Object that does not have a AppDispatcher. Add a dispatcher first");
            this.dispose();
        }
    }
}
//# sourceMappingURL=GetAssetFolderURLUC.js.map