"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.statsRouter = void 0;
const tslib_1 = require("tslib");
const express_1 = tslib_1.__importDefault(require("express"));
const local_video_viewer_1 = require("../../../models/view/local-video-viewer");
const middlewares_1 = require("../../../middlewares");
const statsRouter = express_1.default.Router();
exports.statsRouter = statsRouter;
statsRouter.get('/:videoId/stats/overall', middlewares_1.authenticate, (0, middlewares_1.asyncMiddleware)(middlewares_1.videoOverallStatsValidator), (0, middlewares_1.asyncMiddleware)(getOverallStats));
statsRouter.get('/:videoId/stats/timeseries/:metric', middlewares_1.authenticate, (0, middlewares_1.asyncMiddleware)(middlewares_1.videoTimeserieStatsValidator), (0, middlewares_1.asyncMiddleware)(getTimeserieStats));
statsRouter.get('/:videoId/stats/retention', middlewares_1.authenticate, (0, middlewares_1.asyncMiddleware)(middlewares_1.videoRetentionStatsValidator), (0, middlewares_1.asyncMiddleware)(getRetentionStats));
function getOverallStats(req, res) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        const video = res.locals.videoAll;
        const query = req.query;
        const stats = yield local_video_viewer_1.LocalVideoViewerModel.getOverallStats({
            video,
            startDate: query.startDate,
            endDate: query.endDate
        });
        return res.json(stats);
    });
}
function getRetentionStats(req, res) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        const video = res.locals.videoAll;
        const stats = yield local_video_viewer_1.LocalVideoViewerModel.getRetentionStats(video);
        return res.json(stats);
    });
}
function getTimeserieStats(req, res) {
    var _a, _b;
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        const video = res.locals.videoAll;
        const metric = req.params.metric;
        const query = req.query;
        const stats = yield local_video_viewer_1.LocalVideoViewerModel.getTimeserieStats({
            video,
            metric,
            startDate: (_a = query.startDate) !== null && _a !== void 0 ? _a : video.createdAt.toISOString(),
            endDate: (_b = query.endDate) !== null && _b !== void 0 ? _b : new Date().toISOString()
        });
        return res.json(stats);
    });
}
//# sourceMappingURL=stats.js.map