module.exports = Self => {
    Self.remoteMethod('getList', {
        description: 'Get list of the available and active notification subscriptions',
        accessType: 'READ',
        accepts: [
            {
                arg: 'id',
                type: 'number',
                description: 'User to modify',
                http: {source: 'path'}
            }
        ],
        returns: {
            type: 'object',
            root: true
        },
        http: {
            path: `/:id/getList`,
            verb: 'GET'
        }
    });

    Self.getList = async(id, options) => {
        const activeNotificationsMap = new Map();

        const myOptions = {};

        if (typeof options == 'object')
            Object.assign(myOptions, options);

        const availableNotificationsMap = await Self.getAvailable(id, myOptions);
        const activeNotifications = await Self.app.models.NotificationSubscription.find({
            fields: ['id', 'notificationFk'],
            include: {relation: 'notification'},
            where: {userFk: id}
        }, myOptions);

        for (active of activeNotifications) {
            activeNotificationsMap.set(active.notificationFk, {
                id: active.id,
                notificationFk: active.notificationFk,
                name: active.notification().name,
                description: active.notification().description,
                active: true
            });
            availableNotificationsMap.delete(active.notificationFk);
        }

        return {
            active: [...activeNotificationsMap.entries()],
            available: [...availableNotificationsMap.entries()]
        };
    };
};