import { AXPReportViewerService } from '@acorex/modules/report-management';
import { AXP_ROOT_CONFIG_TOKEN } from '@acorex/platform/common';
import { HttpClient } from '@angular/common/http';
import * as i0 from '@angular/core';
import { inject, Injectable } from '@angular/core';
import { firstValueFrom } from 'rxjs';

class AXCReportExecuteCommand {
    constructor() {
        this.http = inject(HttpClient);
        this.reportDefinitionService = inject(AXPReportViewerService);
        this.configs = inject(AXP_ROOT_CONFIG_TOKEN);
        this.baseUrl = this.configs.baseUrl;
    }
    //#region ----   Command Implementation   ----
    async execute(context) {
        try {
            const { reportId, layoutId, filters: parameters } = context;
            // Get the report definition to find the layout
            const reportDefinition = await this.reportDefinitionService.getReportById(reportId);
            if (!reportDefinition) {
                throw new Error(`Report with ID ${reportId} not found`);
            }
            // Find the specific layout
            const layoutDefinition = reportDefinition.layouts.find((l) => l.id === layoutId);
            if (!layoutDefinition) {
                throw new Error(`Layout with ID ${layoutId} not found in report ${reportId}`);
            }
            // Call the Execute API endpoint
            const url = `${this.baseUrl}/v1/global/Report-Management/report/Execute`;
            const body = {
                reportId,
                parameters,
            };
            const executeResult = await firstValueFrom(this.http.post(url, body));
            // Convert ExecuteReportDto to AXPExecutionReportResult format
            let executionResult;
            if (executeResult.type === 'table') {
                executionResult = {
                    type: 'table',
                    items: executeResult.items,
                    total: executeResult.totalCount,
                };
            }
            else if (executeResult.type === 'chart') {
                executionResult = {
                    type: 'chart',
                    data: executeResult.items[0] || {},
                };
            }
            else if (executeResult.type === 'cards') {
                executionResult = {
                    type: 'cards',
                    items: executeResult.items,
                };
            }
            else {
                executionResult = {
                    type: 'custom',
                    data: executeResult.items,
                };
            }
            // Return the combined layout configuration and data
            return {
                success: true,
                data: {
                    ...layoutDefinition.layout,
                    data: executionResult,
                },
            };
        }
        catch (error) {
            console.error('Error executing report:', error);
            const errorMessage = error instanceof Error ? error.message : 'Unknown error';
            return {
                success: false,
                message: {
                    type: 'error',
                    text: `Failed to execute report: ${errorMessage}`,
                },
            };
        }
    }
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.3.19", ngImport: i0, type: AXCReportExecuteCommand, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); }
    static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "20.3.19", ngImport: i0, type: AXCReportExecuteCommand, providedIn: 'root' }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.3.19", ngImport: i0, type: AXCReportExecuteCommand, decorators: [{
            type: Injectable,
            args: [{
                    providedIn: 'root',
                }]
        }] });

export { AXCReportExecuteCommand };
//# sourceMappingURL=acorex-connectivity-api-execute.command-D2Q0ahw4.mjs.map
