import * as i0 from '@angular/core';
import { inject, Injectable } from '@angular/core';
import { GetProductListQuery } from './acorex-connectivity-mock-get-product-list.query-DzkssYRw.mjs';

//#endregion
class GetProductPdfDataQuery {
    constructor() {
        this.productListQuery = inject(GetProductListQuery);
    }
    async fetch(input) {
        // Extract product-specific filters from AXPFilterClause format
        const extractedFilters = this.extractProductFilters(input.filters);
        // Get the product list data to generate PDF content
        const productListInput = {
            skip: 0,
            take: 10000, // Get all products for PDF
            filters: extractedFilters,
            logic: input.logic,
        };
        const productData = await this.productListQuery.fetch(productListInput);
        // Generate a mock PDF base64 string
        // In production, this would call an actual PDF generation service
        const pdfBase64 = this.generateMockPdfBase64(productData);
        return {
            base64: pdfBase64,
            filename: `Product_Report_${new Date().toISOString().split('T')[0]}.pdf`,
        };
    }
    //#region ----   Filter Extraction   ----
    /**
     * Extract product-specific filters from AXPFilterClause format
     * Maps report parameter paths to product entity field names
     */
    extractProductFilters(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 product-specific field mappings
            if (field === 'brand') {
                // Brand filter - can be array of IDs or single ID
                if (Array.isArray(value) && value.length > 0) {
                    filters.push({
                        field: 'brandId',
                        operator: { type: 'in' },
                        value: value,
                    });
                }
                else if (value) {
                    filters.push({
                        field: 'brandId',
                        operator: { type: 'equal' },
                        value: value,
                    });
                }
            }
            else if (field === 'category') {
                // Category filter - can be array of IDs or single ID
                if (Array.isArray(value) && value.length > 0) {
                    filters.push({
                        field: 'categoryIds',
                        operator: { type: 'contains' },
                        value: value[0], // Use first category for contains check
                    });
                }
                else if (value) {
                    filters.push({
                        field: 'categoryIds',
                        operator: { type: 'contains' },
                        value: value,
                    });
                }
            }
            else if (field === 'status') {
                // Status filter - skip 'all' value
                if (value && value !== 'all') {
                    filters.push({
                        field: 'statusId',
                        operator: { type: 'equal' },
                        value: value,
                    });
                }
            }
            else if (field === 'priceRange') {
                // Price range filter - custom logic handled in query
                filters.push({
                    field: 'priceRange',
                    operator: { type: 'equal' },
                    value: value,
                });
            }
            else if (field === 'startDate' || field === 'endDate') {
                // Date filters - can be handled if needed
                // For now, skip as they might not be directly on product entity
            }
            else {
                // Direct field mapping
                filters.push({
                    field: field,
                    operator: operator,
                    value: value,
                });
            }
        }
        return filters;
    }
    //#endregion
    //#region ----   PDF Generation Helpers   ----
    /**
     * Generate a mock PDF base64 string
     * In production, this would call an actual PDF generation service
     */
    generateMockPdfBase64(productData) {
        // Create a minimal valid PDF with some content
        // This is a very basic PDF structure for demonstration
        // In production, use a proper PDF library like jsPDF or call a backend service
        const pdfContent = this.createSimplePdf(productData);
        // Convert to base64
        const base64 = btoa(pdfContent);
        return base64;
    }
    /**
     * Create a simple PDF document structure
     * This is a minimal valid PDF for demonstration - in production use a proper PDF library
     */
    createSimplePdf(productData) {
        // Use a minimal valid PDF structure that browsers can render
        // This is a very basic single-page PDF
        const pdf = `%PDF-1.4
1 0 obj
<< /Type /Catalog /Pages 2 0 R >>
endobj
2 0 obj
<< /Type /Pages /Kids [3 0 R] /Count 1 >>
endobj
3 0 obj
<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] /Contents 4 0 R /Resources << /Font << /F1 5 0 R >> >> >>
endobj
4 0 obj
<< /Length 200 >>
stream
BT
/F1 12 Tf
100 700 Td
(Product Report) Tj
0 -20 Td
(Total Brands: ${productData.total}) Tj
0 -20 Td
(Generated: ${new Date().toLocaleDateString()}) Tj
0 -20 Td
(Items: ${productData.items.length}) Tj
ET
endstream
endobj
5 0 obj
<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>
endobj
xref
0 6
0000000000 65535 f 
0000000009 00000 n 
0000000058 00000 n 
0000000115 00000 n 
0000000317 00000 n 
0000000444 00000 n 
trailer
<< /Size 6 /Root 1 0 R >>
startxref
527
%%EOF`;
        return pdf;
    }
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.3.19", ngImport: i0, type: GetProductPdfDataQuery, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); }
    static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "20.3.19", ngImport: i0, type: GetProductPdfDataQuery, providedIn: 'root' }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.3.19", ngImport: i0, type: GetProductPdfDataQuery, decorators: [{
            type: Injectable,
            args: [{
                    providedIn: 'root',
                }]
        }] });

export { GetProductPdfDataQuery };
//# sourceMappingURL=acorex-connectivity-mock-get-product-pdf-data.query-wMD65pwR.mjs.map
