import { TwirpServer, TwirpError, TwirpErrorCode, TwirpContentType, chainInterceptors, } from 'twirp-ts';
import { GetGamesDrawerBadgeStatusForAppsRequest, GetGamesDrawerBadgeStatusForAppsResponse, } from './gamesdrawerbadge_msg.js';
export var NotificationsInternalMethod;
(function (NotificationsInternalMethod) {
    NotificationsInternalMethod["GetGamesDrawerBadgeStatusForApps"] = "GetGamesDrawerBadgeStatusForApps";
})(NotificationsInternalMethod || (NotificationsInternalMethod = {}));
export const NotificationsInternalMethodList = [
    NotificationsInternalMethod.GetGamesDrawerBadgeStatusForApps,
];
export function createNotificationsInternalServer(service) {
    return new TwirpServer({
        service,
        packageName: 'devvit.plugin.notifications',
        serviceName: 'NotificationsInternal',
        methodList: NotificationsInternalMethodList,
        matchRoute: matchNotificationsInternalRoute,
    });
}
function matchNotificationsInternalRoute(method, events) {
    switch (method) {
        case 'GetGamesDrawerBadgeStatusForApps':
            return async (ctx, service, data, interceptors) => {
                ctx = { ...ctx, methodName: 'GetGamesDrawerBadgeStatusForApps' };
                await events.onMatch(ctx);
                return handleNotificationsInternalGetGamesDrawerBadgeStatusForAppsRequest(ctx, service, data, interceptors);
            };
        default:
            events.onNotFound();
            const msg = `no handler found`;
            throw new TwirpError(TwirpErrorCode.BadRoute, msg);
    }
}
function handleNotificationsInternalGetGamesDrawerBadgeStatusForAppsRequest(ctx, service, data, interceptors) {
    switch (ctx.contentType) {
        case TwirpContentType.JSON:
            return handleNotificationsInternalGetGamesDrawerBadgeStatusForAppsJSON(ctx, service, data, interceptors);
        case TwirpContentType.Protobuf:
            return handleNotificationsInternalGetGamesDrawerBadgeStatusForAppsProtobuf(ctx, service, data, interceptors);
        default:
            const msg = 'unexpected Content-Type';
            throw new TwirpError(TwirpErrorCode.BadRoute, msg);
    }
}
async function handleNotificationsInternalGetGamesDrawerBadgeStatusForAppsJSON(ctx, service, data, interceptors) {
    let request;
    let response;
    try {
        const body = JSON.parse(data.toString() || '{}');
        request = GetGamesDrawerBadgeStatusForAppsRequest.fromJSON(body);
    }
    catch (e) {
        if (e instanceof Error) {
            const msg = 'the json request could not be decoded';
            throw new TwirpError(TwirpErrorCode.Malformed, msg).withCause(e, true);
        }
    }
    if (interceptors && interceptors.length > 0) {
        const interceptor = chainInterceptors(...interceptors);
        response = await interceptor(ctx, request, (ctx, inputReq) => {
            return service.GetGamesDrawerBadgeStatusForApps(ctx, inputReq);
        });
    }
    else {
        response = await service.GetGamesDrawerBadgeStatusForApps(ctx, request);
    }
    return JSON.stringify(GetGamesDrawerBadgeStatusForAppsResponse.toJSON(response));
}
async function handleNotificationsInternalGetGamesDrawerBadgeStatusForAppsProtobuf(ctx, service, data, interceptors) {
    let request;
    let response;
    try {
        request = GetGamesDrawerBadgeStatusForAppsRequest.decode(data);
    }
    catch (e) {
        if (e instanceof Error) {
            const msg = 'the protobuf request could not be decoded';
            throw new TwirpError(TwirpErrorCode.Malformed, msg).withCause(e, true);
        }
    }
    if (interceptors && interceptors.length > 0) {
        const interceptor = chainInterceptors(...interceptors);
        response = await interceptor(ctx, request, (ctx, inputReq) => {
            return service.GetGamesDrawerBadgeStatusForApps(ctx, inputReq);
        });
    }
    else {
        response = await service.GetGamesDrawerBadgeStatusForApps(ctx, request);
    }
    return Buffer.from(GetGamesDrawerBadgeStatusForAppsResponse.encode(response).finish());
}
