import { AppObjectEntity } from "@vived/core";
export class AppHandlerEntity extends AppObjectEntity {
}
AppHandlerEntity.type = "APP_HANDLER_ENTITY";
export function makeAppHandler(appObject) {
    return new RequestHandlerImp(appObject);
}
class RequestHandlerImp extends AppHandlerEntity {
    handlePayloadVersionRequest(request) {
        if (request.version !== 2) {
            throw new UnsupportedRequestVersion(request.type, request.version);
        }
        const payload = request.payload;
        if (payload.callback === undefined) {
            throw new UnableToParsePayload(request.type, request.version, JSON.stringify(payload));
        }
        const versionMap = new Map();
        this.handlers.forEach((handler) => {
            versionMap.set(handler.requestType, handler.payloadVersion);
        });
        payload.callback(versionMap);
    }
    handleAppHandlerVersionRequest(request) {
        if (request.version !== 1) {
            throw new UnsupportedRequestVersion(request.type, request.version);
        }
        const payload = request.payload;
        if (payload.callback === undefined) {
            throw new UnableToParsePayload(request.type, request.version, JSON.stringify(payload));
        }
        payload.callback(this.appHandlerVersion);
    }
    constructor(appObject) {
        super(appObject, AppHandlerEntity.type);
        this.handlers = new Map();
        this.getAppPayloadRequest = "GET_APP_PAYLOAD_VERSION";
        this.getAppHandlerVersionRequest = "GET_APP_HANDLER_VERSION";
        this.appHandlerVersion = 1;
        this.handler = (request) => {
            if (request.type === this.getAppPayloadRequest) {
                this.handlePayloadVersionRequest(request);
                return;
            }
            if (request.type === this.getAppHandlerVersionRequest) {
                this.handleAppHandlerVersionRequest(request);
                return;
            }
            const handler = this.handlers.get(request.type);
            if (!handler) {
                console.warn(`[AppHandler] Unsupported request type received: ${request.type}`);
                return;
            }
            try {
                handler.handleRequest(request.version, request.payload);
            }
            catch (e) {
                console.warn(`[AppHandler] ${e}`);
            }
        };
        this.registerRequestHandler = (handler) => {
            if (this.handlers.has(handler.requestType)) {
                console.warn(`[AppHandler] More than one handler is being registered for ${handler.requestType}. Previous handler is being overwritten`);
            }
            this.handlers.set(handler.requestType, handler);
        };
    }
}
export class UnsupportedRequestVersion extends Error {
    constructor(requestType, version) {
        const msg = `Version ${version} of request type ${requestType} is not supported`;
        super(msg);
    }
}
export class UnableToParsePayload extends Error {
    constructor(requestType, version, payload) {
        const msg = `Unable to parse payload for version ${version} of request type ${requestType}: ${payload}`;
        super(msg);
    }
}
//# sourceMappingURL=AppHandler.js.map