import { InvalidValueError, MalformedBodyError } from '@crosslab/service-common';
import { isCallback, isEventCallback } from '../../generated/types.js';
import { handleEventCallback } from './event/index.js';
export * from '../../methods/callbacks.js';
/**
 * This function adds the endpoint for incoming callbacks registered by the device service.
 * @param app The express application the callback endpoint should be added to.
 */
export function callbackHandling(app) {
    app.post('/callbacks/device', async (req, res, next) => {
        try {
            const callback = req.body;
            if (!isCallback(callback))
                throw new MalformedBodyError('Body of request is not a valid callback', 400);
            switch (callback.callbackType) {
                case 'event':
                    if (!isEventCallback(callback))
                        throw new MalformedBodyError('Body of request is not a valid event callback', 400);
                    return res.status(await handleEventCallback(callback)).send();
                default:
                    throw new InvalidValueError(`Callbacks of type '${req.body.callbackType}' are not supported`, 400);
            }
        }
        catch (error) {
            return next(error);
        }
    });
}
//# sourceMappingURL=index.js.map