"use strict";
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);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
    return function (target, key) { decorator(target, key, paramIndex); }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SyncController = void 0;
const openapi = require("@nestjs/swagger");
const common_1 = require("@nestjs/common");
const swagger_1 = require("@nestjs/swagger");
const decorators_1 = require("../decorators");
const sync_dto_1 = require("../dtos/sync.dto");
const enum_1 = require("../enum");
const auth_guard_1 = require("../middleware/auth.guard");
const global_exception_filter_1 = require("../middleware/global-exception.filter");
const sync_service_1 = require("../services/sync.service");
let SyncController = class SyncController {
    service;
    errorService;
    constructor(service, errorService) {
        this.service = service;
        this.errorService = errorService;
    }
    async getSyncStream(auth, req, res, dto) {
        try {
            await this.service.stream(auth, res, dto);
        }
        catch (error) {
            res.setHeader('Content-Type', 'application/json');
            this.errorService.handleError(req, res, error);
        }
    }
    getSyncAck(auth) {
        return this.service.getAcks(auth);
    }
    sendSyncAck(auth, dto) {
        return this.service.setAcks(auth, dto);
    }
    deleteSyncAck(auth, dto) {
        return this.service.deleteAcks(auth, dto);
    }
};
exports.SyncController = SyncController;
__decorate([
    (0, common_1.Post)('stream'),
    (0, auth_guard_1.Authenticated)({ permission: enum_1.Permission.SyncStream }),
    (0, common_1.Header)('Content-Type', 'application/jsonlines+json'),
    (0, common_1.HttpCode)(common_1.HttpStatus.OK),
    (0, decorators_1.Endpoint)({
        summary: 'Stream sync changes',
        description: 'Retrieve a JSON lines streamed response of changes for synchronization. This endpoint is used by the mobile app to efficiently stay up to date with changes.',
        history: new decorators_1.HistoryBuilder().added('v1').beta('v1').stable('v2'),
    }),
    openapi.ApiResponse({ status: common_1.HttpStatus.OK }),
    __param(0, (0, auth_guard_1.Auth)()),
    __param(1, (0, common_1.Req)()),
    __param(2, (0, common_1.Res)()),
    __param(3, (0, common_1.Body)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object, Object, Object, sync_dto_1.SyncStreamDto]),
    __metadata("design:returntype", Promise)
], SyncController.prototype, "getSyncStream", null);
__decorate([
    (0, common_1.Get)('ack'),
    (0, auth_guard_1.Authenticated)({ permission: enum_1.Permission.SyncCheckpointRead }),
    (0, decorators_1.Endpoint)({
        summary: 'Retrieve acknowledgements',
        description: 'Retrieve the synchronization acknowledgments for the current session.',
        history: new decorators_1.HistoryBuilder().added('v1').beta('v1').stable('v2'),
    }),
    openapi.ApiResponse({ status: 200, type: [require("../dtos/sync.dto").SyncAckDto] }),
    __param(0, (0, auth_guard_1.Auth)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], SyncController.prototype, "getSyncAck", null);
__decorate([
    (0, common_1.Post)('ack'),
    (0, auth_guard_1.Authenticated)({ permission: enum_1.Permission.SyncCheckpointUpdate }),
    (0, common_1.HttpCode)(common_1.HttpStatus.NO_CONTENT),
    (0, decorators_1.Endpoint)({
        summary: 'Acknowledge changes',
        description: 'Send a list of synchronization acknowledgements to confirm that the latest changes have been received.',
        history: new decorators_1.HistoryBuilder().added('v1').beta('v1').stable('v2'),
    }),
    openapi.ApiResponse({ status: common_1.HttpStatus.NO_CONTENT }),
    __param(0, (0, auth_guard_1.Auth)()),
    __param(1, (0, common_1.Body)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object, sync_dto_1.SyncAckSetDto]),
    __metadata("design:returntype", void 0)
], SyncController.prototype, "sendSyncAck", null);
__decorate([
    (0, common_1.Delete)('ack'),
    (0, auth_guard_1.Authenticated)({ permission: enum_1.Permission.SyncCheckpointDelete }),
    (0, common_1.HttpCode)(common_1.HttpStatus.NO_CONTENT),
    (0, decorators_1.Endpoint)({
        summary: 'Delete acknowledgements',
        description: 'Delete specific synchronization acknowledgments.',
        history: new decorators_1.HistoryBuilder().added('v1').beta('v1').stable('v2'),
    }),
    openapi.ApiResponse({ status: common_1.HttpStatus.NO_CONTENT }),
    __param(0, (0, auth_guard_1.Auth)()),
    __param(1, (0, common_1.Body)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object, sync_dto_1.SyncAckDeleteDto]),
    __metadata("design:returntype", Promise)
], SyncController.prototype, "deleteSyncAck", null);
exports.SyncController = SyncController = __decorate([
    (0, swagger_1.ApiTags)(enum_1.ApiTag.Sync),
    (0, common_1.Controller)('sync'),
    __metadata("design:paramtypes", [sync_service_1.SyncService,
        global_exception_filter_1.GlobalExceptionFilter])
], SyncController);
//# sourceMappingURL=sync.controller.js.map