import * as i0 from '@angular/core';
import { inject, Injectable } from '@angular/core';
import { AXPEntityService } from '@acorex/platform/layout/entity';
import { AXPSystemStatusType } from '@acorex/platform/common';
import { RootConfig } from '@acorex/modules/tenant-management';
import { RootConfig as RootConfig$1 } from '@acorex/modules/subscription-management';
import { RootConfig as RootConfig$2 } from '@acorex/modules/application-management';

//#endregion
class GetTenantApplicationAccessQuery {
    constructor() {
        this.entityService = inject(AXPEntityService);
        this.tenantService = this.entityService
            .withEntity(`${RootConfig.module.name}.${RootConfig.entities.tenant.name}`)
            .data();
        this.subscriptionService = this.entityService
            .withEntity(`${RootConfig$1.module.name}.${RootConfig$1.entities.subscription.name}`)
            .data();
        this.planService = this.entityService
            .withEntity(`${RootConfig$1.module.name}.${RootConfig$1.entities.subscriptionPlan.name}`)
            .data();
        this.planItemService = this.entityService
            .withEntity(`${RootConfig$1.module.name}.${RootConfig$1.entities.subscriptionPlanItem.name}`)
            .data();
        this.applicationService = this.entityService
            .withEntity(`${RootConfig$2.module.name}.${RootConfig$2.entities.application.name}`)
            .data();
    }
    async fetch(input) {
        // Extract filters from AXPFilterClause format
        const extractedFilters = this.extractTenantFilters(input.filters);
        const filterLogic = input.filters?.logic || input.logic || 'and';
        // Separate custom filters from entity filters
        const entityFilters = [];
        let dateRangeFilter;
        if (extractedFilters && extractedFilters.length > 0) {
            for (const filter of extractedFilters) {
                if (filter.field === 'dateRange') {
                    dateRangeFilter = filter.value;
                }
                else {
                    entityFilters.push(filter);
                }
            }
        }
        // Get all tenants
        const tenantsResult = await this.tenantService.query({
            skip: 0,
            take: 10000,
            filter: entityFilters.length > 0
                ? {
                    logic: filterLogic,
                    filters: entityFilters,
                }
                : undefined,
        });
        let tenants = tenantsResult.items;
        // Apply date range filter if provided
        if (dateRangeFilter) {
            tenants = this.applyDateRangeFilter(tenants, dateRangeFilter);
        }
        // Get all subscriptions
        const subscriptionsResult = await this.subscriptionService.query({
            skip: 0,
            take: 10000,
        });
        const subscriptions = subscriptionsResult.items.filter((s) => s.subscriberType === 'Tenant');
        // Get all plans
        const plansResult = await this.planService.query({
            skip: 0,
            take: 10000,
        });
        const plans = plansResult.items;
        // Get all plan items
        const planItemsResult = await this.planItemService.query({
            skip: 0,
            take: 10000,
        });
        const planItems = planItemsResult.items;
        // Get all applications
        const applicationsResult = await this.applicationService.query({
            skip: 0,
            take: 10000,
        });
        const applications = applicationsResult.items;
        // Build results for each tenant
        const results = [];
        for (const tenant of tenants) {
            // Get tenant's subscriptions
            const tenantSubscriptions = subscriptions.filter((s) => s.subscriberId === tenant.id);
            // Get unique applications through subscriptions → plans → plan items → editions → applications
            const applicationSet = new Set();
            const applicationNames = [];
            for (const subscription of tenantSubscriptions) {
                if (!subscription.planId)
                    continue;
                // Get plan items for this plan
                const planPlanItems = planItems.filter((pi) => pi.planId === subscription.planId);
                for (const planItem of planPlanItems) {
                    if (!planItem.editionId)
                        continue;
                    // Find applications that have this edition
                    for (const application of applications) {
                        if (application.editionIds && application.editionIds.includes(planItem.editionId)) {
                            if (!applicationSet.has(application.id)) {
                                applicationSet.add(application.id);
                                applicationNames.push(application.title);
                            }
                        }
                    }
                }
            }
            // Count active subscriptions
            const activeSubscriptions = tenantSubscriptions.filter((s) => s.statusId === AXPSystemStatusType.Active);
            results.push({
                tenantId: tenant.id,
                tenantName: tenant.title,
                tenantStatus: tenant.statusId || 'Unknown',
                subscriptionCount: tenantSubscriptions.length,
                applicationsCount: applicationSet.size,
                applicationsList: applicationNames.join(', '),
                activeSubscriptionsCount: activeSubscriptions.length,
                registrationDate: tenant.registrationDate,
                registrationDateStr: tenant.registrationDate?.toISOString(),
            });
        }
        // Apply sorting if provided
        if (input.sort && input.sort.length > 0) {
            results.sort((a, b) => {
                for (const sortField of input.sort) {
                    const field = sortField.field;
                    const aValue = a[field];
                    const bValue = b[field];
                    const dir = sortField.dir === 'asc' ? 1 : -1;
                    if (aValue === undefined && bValue === undefined)
                        continue;
                    if (aValue === undefined)
                        return 1 * dir;
                    if (bValue === undefined)
                        return -1 * dir;
                    if (aValue < bValue)
                        return -1 * dir;
                    if (aValue > bValue)
                        return 1 * dir;
                }
                return 0;
            });
        }
        else {
            // Default sort by tenant name
            results.sort((a, b) => a.tenantName.localeCompare(b.tenantName));
        }
        // Apply pagination
        const skip = input.skip || 0;
        const take = input.take || 10;
        const total = results.length;
        const items = results.slice(skip, skip + take);
        return {
            items,
            total,
        };
    }
    //#region ----   Filter Extraction   ----
    /**
     * Extract tenant-specific filters from AXPFilterClause format
     * Maps report parameter paths to tenant entity field names
     */
    extractTenantFilters(parameters) {
        const filters = [];
        const paramsAny = parameters;
        if (!paramsAny?.filters || !Array.isArray(paramsAny.filters)) {
            return filters;
        }
        for (const filter of paramsAny.filters) {
            if (!filter?.field || filter?.value === undefined || filter?.value === null) {
                continue;
            }
            const field = filter.field;
            const value = filter.value;
            const operator = filter.operator || { type: 'equal' };
            // Handle tenant-specific field mappings
            if (field === 'tenantId') {
                // Tenant ID filter - can be array of IDs or single ID
                if (Array.isArray(value) && value.length > 0) {
                    filters.push({
                        field: 'id',
                        operator: { type: 'in' },
                        value: value,
                    });
                }
                else if (value) {
                    filters.push({
                        field: 'id',
                        operator: { type: 'equal' },
                        value: value,
                    });
                }
            }
            else if (field === 'statusId') {
                // Status filter - skip 'all' value
                if (value && value !== 'all') {
                    filters.push({
                        field: 'statusId',
                        operator: { type: 'equal' },
                        value: value,
                    });
                }
            }
            else if (field === 'dateRange') {
                // Date range filter - custom logic handled in query
                filters.push({
                    field: 'dateRange',
                    operator: { type: 'equal' },
                    value: value,
                });
            }
            else {
                // Direct field mapping
                filters.push({
                    field: field,
                    operator: operator,
                    value: value,
                });
            }
        }
        return filters;
    }
    //#endregion
    //#region ----   Custom Filter Handlers   ----
    /**
     * Apply date range filter to tenants
     */
    applyDateRangeFilter(tenants, dateRange) {
        if (!dateRange || !dateRange.start || !dateRange.end) {
            return tenants;
        }
        const startDate = new Date(dateRange.start);
        const endDate = new Date(dateRange.end);
        endDate.setHours(23, 59, 59, 999);
        return tenants.filter((tenant) => {
            if (!tenant.registrationDate)
                return false;
            const regDate = new Date(tenant.registrationDate);
            return regDate >= startDate && regDate <= endDate;
        });
    }
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.3.19", ngImport: i0, type: GetTenantApplicationAccessQuery, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); }
    static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "20.3.19", ngImport: i0, type: GetTenantApplicationAccessQuery, providedIn: 'root' }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.3.19", ngImport: i0, type: GetTenantApplicationAccessQuery, decorators: [{
            type: Injectable,
            args: [{
                    providedIn: 'root',
                }]
        }] });

export { GetTenantApplicationAccessQuery };
//# sourceMappingURL=acorex-connectivity-mock-get-tenant-application-access.query-BM9toJ45.mjs.map
