import logger from '../../../../utils/logger.js';
import { ReportStoreService } from '../../../../services/reportStoreService.js';

class DownloadPermissionService extends ReportStoreService {
    enabled;
    feedbackEnabled;
    constructor(userDataStore, serviceFeatureConfig) {
        super(userDataStore);
        this.enabled = Boolean(serviceFeatureConfig.download);
        this.feedbackEnabled = Boolean(serviceFeatureConfig.feedbackOnDownload);
        if (!this.enabled)
            logger.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.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 || [];
    }
}

export { DownloadPermissionService, DownloadPermissionService as default };
//# sourceMappingURL=service.js.map
