var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
import { inject } from '@adonisjs/core';
import '@akago/vine-lucid';
import vine from '@vinejs/vine';
import { AKVineRequest, AKVineResponse } from '../crud/vine_handler_decorators.js';
import { AKVineRequestData } from '../crud/vine_validate_middleware.js';
import { AKAPIException } from '../exceptions/api_exception.js';
import { OpenApiDescription } from '../openapi/openapi_router_macros.js';
import { AKDeviceTokenType } from './device_token.js';
export const deviceTokenController = (deviceTokenServiceClass) => {
    const deviceTokenService = new deviceTokenServiceClass();
    class AKDeviceTokenController {
        async assign({ auth }, { type, token }) {
            if (!auth.user)
                throw AKAPIException.notAllowed;
            else
                await deviceTokenService.assign(token, type, auth.user);
        }
        async unassign({ auth }, { token }) {
            if (!auth.user)
                throw AKAPIException.notAllowed;
            else
                await deviceTokenService.unassign(token, auth.user);
        }
        async list({ auth }) {
            if (!auth.user)
                throw AKAPIException.notAllowed;
            else {
                const tokens = await deviceTokenService.list(auth.user);
                return tokens.map((t) => t.serialize({ fields: ['type', 'token'] }));
            }
        }
        async test({ auth }) {
            if (!auth.user)
                throw AKAPIException.notAllowed;
            else {
                await deviceTokenService.sendTest(auth.user);
            }
        }
    }
    __decorate([
        OpenApiDescription(`Assign given push identifier to current user. Un-assign it to previous assigned user.`),
        AKVineRequest(vine.object({
            type: vine.enumWithValues(AKDeviceTokenType),
            token: vine.string(),
        })),
        __metadata("design:type", Function),
        __metadata("design:paramtypes", [Function, AKVineRequestData]),
        __metadata("design:returntype", Promise)
    ], AKDeviceTokenController.prototype, "assign", null);
    __decorate([
        OpenApiDescription(`Unssign given push identifier off current user. Useful in when signing out.`),
        AKVineRequest(vine.object({
            token: vine.string(),
        })),
        __metadata("design:type", Function),
        __metadata("design:paramtypes", [Function, AKVineRequestData]),
        __metadata("design:returntype", Promise)
    ], AKDeviceTokenController.prototype, "unassign", null);
    __decorate([
        OpenApiDescription(`Get my tokens. Mainly for tests purpose.`),
        AKVineResponse(vine.array(vine.object({
            type: vine.enumWithValues(AKDeviceTokenType),
            token: vine.string(),
        }))),
        __metadata("design:type", Function),
        __metadata("design:paramtypes", [Function]),
        __metadata("design:returntype", Promise)
    ], AKDeviceTokenController.prototype, "list", null);
    __decorate([
        OpenApiDescription(`Sends a test push notification to all tokens of current user.`),
        inject(),
        __metadata("design:type", Function),
        __metadata("design:paramtypes", [Function]),
        __metadata("design:returntype", Promise)
    ], AKDeviceTokenController.prototype, "test", null);
    return AKDeviceTokenController;
};
//# sourceMappingURL=device_token_controller.js.map