import app from '@adonisjs/core/services/app';
import AKTestEvent from '../events/events/test_event.js';
import { deviceToken } from './device_token.js';
export const deviceTokenService = (userModel) => {
    const deviceTokenModel = deviceToken(userModel);
    return class AKDeviceTokenService {
        async assign(token, type, toUser) {
            const deviceToken = await deviceTokenModel.query().where('token', token).first();
            if (deviceToken) {
                if (deviceToken.userId !== toUser.id)
                    await deviceTokenModel.query().where('token', token).update('userId', toUser.id);
            }
            else
                await deviceTokenModel.create({
                    userId: toUser.id,
                    token,
                    type,
                });
        }
        async unassign(token, toUser) {
            await deviceTokenModel.query().where('token', token).where('userId', toUser.id).delete();
        }
        async list(forUser, type) {
            const query = deviceTokenModel.query().where('userId', forUser.id);
            if (type)
                query.where('type', type);
            return await query;
        }
        async sendTest(toUser) {
            const event = new AKTestEvent(toUser);
            const eventService = await app.container.make('akago.event.service');
            await eventService.publish(event);
        }
        async deleteTokens(tokens) {
            return await deviceTokenModel.query().whereIn('token', tokens).delete();
        }
    };
};
//# sourceMappingURL=device_token_service.js.map