'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var logger = require('../../../../utils/logger.js');
var reportStoreService = require('../../../../services/reportStoreService.js');

class DownloadPermissionService extends reportStoreService.ReportStoreService {
    enabled;
    feedbackEnabled;
    constructor(userDataStore, serviceFeatureConfig) {
        super(userDataStore);
        this.enabled = Boolean(serviceFeatureConfig.download);
        this.feedbackEnabled = Boolean(serviceFeatureConfig.feedbackOnDownload);
        if (!this.enabled)
            logger.default.info(`Download: disabled `);
    }
    async saveDownloadPermissionData(userId, reportId, id) {
        if (!this.enabled)
            return;
        const userConfig = await this.getState(userId);
        // Init downloads if not present
        if (!userConfig.downloadPermissions) {
            userConfig.downloadPermissions = [];
        }
        const permissionExists = await this.downloadEnabledForReport(userId, reportId, id);
        if (!permissionExists) {
            userConfig.downloadPermissions.push({ reportId, id });
            logger.default.info(`Download permission granted for ${userId}: ${reportId} - ${id}`);
            await this.saveState(userId, userConfig);
        }
    }
    async removeDownloadPermissionData(userId, id) {
        if (!this.enabled)
            return;
        const userConfig = await this.getState(userId);
        const { downloadPermissions } = userConfig;
        if (downloadPermissions) {
            const index = downloadPermissions.findIndex((downloadConfig) => {
                return downloadConfig.id === id;
            });
            if (index >= 0) {
                downloadPermissions.splice(index, 1);
            }
            await this.saveState(userId, userConfig);
        }
    }
    async downloadEnabledForReport(userId, reportId, id) {
        if (!this.feedbackEnabled)
            return true;
        if (!this.enabled)
            return false;
        const userConfig = await this.getState(userId);
        if (!userConfig.downloadPermissions)
            return false;
        const config = userConfig.downloadPermissions.find((downloadConfig) => {
            return downloadConfig.id === id && downloadConfig.reportId === reportId;
        });
        return !!config;
    }
    async getAllDownloadPermissions(userId) {
        if (!this.enabled)
            return [];
        const userConfig = await this.getState(userId);
        return userConfig.downloadPermissions || [];
    }
}

exports.DownloadPermissionService = DownloadPermissionService;
exports.default = DownloadPermissionService;
//# sourceMappingURL=service.js.map
