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

class RecentlyViewedStoreService extends ReportStoreService {
    constructor(userDataStore) {
        super(userDataStore);
    }
    async getAllReports(userId) {
        const userConfig = await this.getState(userId);
        return userConfig.recentlyViewedReports;
    }
    async getAllReportsById(id, userId) {
        const userConfig = await this.getState(userId);
        return userConfig.recentlyViewedReports.filter((requested) => {
            return (requested.id && requested.id === id) || (requested.variantId && requested.variantId === id);
        });
    }
    async getReportById(id, userId) {
        const userConfig = await this.getState(userId);
        return userConfig.recentlyViewedReports.find((viewed) => {
            return (viewed.id && viewed.id === id) || (viewed.variantId && viewed.variantId === id);
        });
    }
    async getReportByExecutionId(id, userId) {
        const userConfig = await this.getState(userId);
        return userConfig.recentlyViewedReports.find((report) => report.executionId === id);
    }
    async getReportByTableId(id, userId) {
        const userConfig = await this.getState(userId);
        return userConfig.recentlyViewedReports.find((report) => report.tableId === id);
    }
    async setRecentlyViewed(reportData, userId) {
        const userConfig = await this.getState(userId);
        const { executionId } = reportData;
        const index = executionId
            ? this.findIndexByExecutionId(executionId, userConfig.recentlyViewedReports)
            : await this.findIndexByReportId(reportData.id, userConfig.recentlyViewedReports);
        // Remove the old entry before inserting the new at index 0
        if (index > -1) {
            userConfig.recentlyViewedReports.splice(index, 1);
            await this.saveState(userId, userConfig);
        }
        await this.addReport(reportData, userId, userConfig);
    }
    async addReport(reportData, userId, userConfig) {
        userConfig.recentlyViewedReports.unshift(reportData);
        await this.saveState(userId, userConfig);
    }
    async setToExpired(id, userId) {
        const userConfig = await this.getState(userId);
        const index = this.findIndexByExecutionId(id, userConfig.recentlyViewedReports);
        if (index === -1) {
            return;
        }
        await this.saveExpiredState(userConfig, index, userId);
    }
    async setToExpiredByTableId(id, userId) {
        const userConfig = await this.getState(userId);
        const index = this.findIndexByTableId(id, userConfig.recentlyViewedReports);
        if (index !== -1) {
            await this.saveExpiredState(userConfig, index, userId);
        }
        else {
            logger.info(`Unable to expire viewed report - ${id} not found in state`);
        }
    }
    async saveExpiredState(userConfig, index, userId) {
        const report = userConfig.recentlyViewedReports[index];
        if (!report)
            return;
        const updatedReport = {
            ...report,
            status: RequestStatus.EXPIRED,
            timestamp: {
                ...report.timestamp,
                expired: new Date(),
            },
        };
        const updatedUserConfig = {
            ...userConfig,
            recentlyViewedReports: userConfig.recentlyViewedReports.map((r, i) => (i === index ? updatedReport : r)),
        };
        await this.saveState(userId, updatedUserConfig);
    }
    async removeReport(userId, reportId, id, tableId) {
        const userConfig = await this.getState(userId);
        const { recentlyViewedReports } = userConfig;
        let index = -1;
        if (tableId) {
            // is an ASYNC report so can find it with the table ID
            index = this.findIndexByTableId(tableId, recentlyViewedReports);
        }
        else {
            // is a SYNC report - so only accessible via an ID and report ID
            index = this.findIndexByReportAndVariantId(id, reportId, recentlyViewedReports);
        }
        if (index >= 0) {
            userConfig.recentlyViewedReports.splice(index, 1);
            await this.saveState(userId, userConfig);
        }
    }
    async removeSupersededViewedReports(removedExecutionIds, userId) {
        Promise.all(removedExecutionIds.map(async (executionId) => {
            const report = await this.getReportByExecutionId(executionId, userId);
            if (report) {
                const { reportId, id, tableId } = report;
                await this.removeReport(userId, reportId, id, tableId);
            }
        })).then(() => undefined);
    }
}

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