import { ReportStoreService } from './reportStoreService.js';
import { FiltersType } from '../components/_filters/filtersTypeEnum.js';
import logger from '../utils/logger.js';

/* eslint-disable no-param-reassign */
class DefaultFilterValuesService extends ReportStoreService {
    enabled;
    constructor(reportDataStore, serviceFeatureConfig) {
        super(reportDataStore);
        this.enabled = Boolean(serviceFeatureConfig.saveDefaults);
        if (!this.enabled)
            logger.info(`saveDefaultsFilters: disabled`);
    }
    async init(userConfig, userId) {
        if (!this.enabled)
            return;
        if (!userConfig.defaultFilters) {
            userConfig.defaultFilters = [];
            await this.saveState(userId, userConfig);
        }
    }
    async save(userId, reportId, id, values) {
        if (!this.enabled)
            return;
        const userConfig = await this.getState(userId);
        await this.init(userConfig, userId);
        const defaults = {
            reportId,
            id,
            values,
        };
        const defaultValuesIndex = await this.getIndex(userId, reportId, id);
        if (userConfig.defaultFilters !== undefined) {
            if (defaultValuesIndex === -1) {
                userConfig.defaultFilters.push(defaults);
            }
            else if (typeof defaultValuesIndex === 'number') {
                userConfig.defaultFilters[defaultValuesIndex] = defaults;
            }
            await this.saveState(userId, userConfig);
        }
    }
    async get(userId, reportId, id, filtersType) {
        if (!this.enabled)
            return [];
        const userConfig = await this.getState(userId);
        const defaultConfig = userConfig.defaultFilters?.find((defaultFilter) => {
            return defaultFilter.id === id && defaultFilter.reportId === reportId;
        });
        if (!defaultConfig) {
            return undefined;
        }
        return defaultConfig.values.filter((v) => {
            const type = !v.type ? FiltersType.REQUEST : v.type;
            return filtersType === type;
        });
    }
    async hasDefaults(userId, reportId, id, type = FiltersType.REQUEST) {
        if (!this.enabled)
            return false;
        const userConfig = await this.getState(userId);
        const defaults = userConfig.defaultFilters?.find((df) => df.reportId === reportId && df.id === id);
        if (!defaults || !Array.isArray(defaults.values) || defaults.values.length === 0) {
            return false;
        }
        return defaults.values.some((value) => {
            const filtersType = !value.type ? FiltersType.REQUEST : value.type;
            return filtersType === type;
        });
    }
    async getIndex(userId, reportId, id) {
        const userConfig = await this.getState(userId);
        return userConfig.defaultFilters?.findIndex((defaultFilter) => {
            return defaultFilter.id === id && defaultFilter.reportId === reportId;
        });
    }
    async delete(userId, reportId, id, type) {
        if (!this.enabled)
            return;
        const userConfig = await this.getState(userId);
        const index = await this.getIndex(userId, reportId, id);
        if (index !== undefined && index !== -1 && userConfig.defaultFilters) {
            const defaults = userConfig.defaultFilters[index];
            if (defaults.values.length > 0) {
                const updatedValues = defaults.values.filter((value) => {
                    const filtersType = !value.type ? FiltersType.REQUEST : value.type;
                    return filtersType !== type;
                });
                userConfig.defaultFilters[index].values = updatedValues;
            }
            else {
                userConfig.defaultFilters.splice(index, 1);
            }
            await this.saveState(userId, userConfig);
        }
    }
}

export { DefaultFilterValuesService, DefaultFilterValuesService as default };
//# sourceMappingURL=defaultFilterValuesService.js.map
