import * as i0 from '@angular/core';
import { inject, Injectable } from '@angular/core';
import { AXPEntityService } from '@acorex/platform/layout/entity';
import { RootConfig } from '@acorex/modules/application-management';

//#endregion
class GetApplicationVersionsChartDataQuery {
    constructor() {
        this.entityService = inject(AXPEntityService);
        this.appVersionService = this.entityService
            .withEntity(`${RootConfig.module.name}.${RootConfig.entities.appVersion.name}`)
            .data();
    }
    async fetch(input) {
        // Get all app versions from storage (no pagination for charts)
        const allVersionsResult = await this.appVersionService.query({
            skip: 0,
            take: 10000, // Get all versions for aggregation
            filter: input.filters && input.filters.length > 0
                ? {
                    logic: input.logic || 'and',
                    filters: input.filters,
                }
                : undefined,
        });
        let allVersions = allVersionsResult.items;
        // Apply custom filters after fetching
        if (input.filters) {
            for (const filter of input.filters) {
                if (filter.field === 'dateRange' && filter.value) {
                    allVersions = this.applyDateRangeFilter(allVersions, filter.value);
                }
                if (filter.field === 'versionNumber' && filter.value) {
                    allVersions = this.applyVersionNumberFilter(allVersions, filter.value);
                }
            }
        }
        // Group versions by month for chart aggregation
        const monthGroups = new Map();
        for (const version of allVersions) {
            const publishDate = new Date(version.publishDate);
            const year = publishDate.getFullYear();
            const month = publishDate.getMonth() + 1;
            const monthKey = `${year}-${String(month).padStart(2, '0')}`;
            if (!monthGroups.has(monthKey)) {
                monthGroups.set(monthKey, []);
            }
            monthGroups.get(monthKey).push(version);
        }
        // Aggregate data by month for chart
        const chartResults = [];
        for (const [monthKey, versions] of monthGroups.entries()) {
            const [year, month] = monthKey.split('-');
            const publishDate = new Date(parseInt(year), parseInt(month) - 1, 1);
            const monthName = publishDate.toLocaleString('en-US', { month: 'long' });
            const quarter = Math.ceil(parseInt(month) / 3);
            const quarterName = `Q${quarter}`;
            chartResults.push({
                publishDate: publishDate.toISOString(),
                month: monthName,
                year: parseInt(year),
                quarter: quarterName,
                versionCount: versions.length,
            });
        }
        // Sort by date ascending for timeline
        chartResults.sort((a, b) => {
            const dateA = new Date(a.publishDate).getTime();
            const dateB = new Date(b.publishDate).getTime();
            return dateA - dateB;
        });
        return {
            items: chartResults,
        };
    }
    //#region ----   Custom Filter Handlers   ----
    /**
     * Apply date range filter to versions
     */
    applyDateRangeFilter(versions, dateRange) {
        if (!dateRange || !dateRange.start || !dateRange.end) {
            return versions;
        }
        const startDate = new Date(dateRange.start);
        const endDate = new Date(dateRange.end);
        endDate.setHours(23, 59, 59, 999); // Include the entire end date
        return versions.filter((version) => {
            const publishDate = new Date(version.publishDate);
            return publishDate >= startDate && publishDate <= endDate;
        });
    }
    /**
     * Apply version number filter to versions
     */
    applyVersionNumberFilter(versions, versionNumber) {
        if (!versionNumber) {
            return versions;
        }
        const searchTerm = versionNumber.toLowerCase().trim();
        return versions.filter((version) => {
            return version.versionNumber.toLowerCase().includes(searchTerm);
        });
    }
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.3.19", ngImport: i0, type: GetApplicationVersionsChartDataQuery, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); }
    static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "20.3.19", ngImport: i0, type: GetApplicationVersionsChartDataQuery, providedIn: 'root' }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.3.19", ngImport: i0, type: GetApplicationVersionsChartDataQuery, decorators: [{
            type: Injectable,
            args: [{
                    providedIn: 'root',
                }]
        }] });

export { GetApplicationVersionsChartDataQuery };
//# sourceMappingURL=acorex-connectivity-mock-get-application-versions-chart-data.query-uwDUrbFJ.mjs.map
