import { AXPLayoutBuilderService } from '@acorex/platform/layout/builder';
import { AXToastService } from '@acorex/components/toast';
import { AXTranslationService } from '@acorex/core/translation';
import * as i0 from '@angular/core';
import { inject, Injectable } from '@angular/core';
import { AXMQueryService } from './acorex-modules-data-management.mjs';

//#endregion
//#region ----   Execute Query Command   ----
/**
 * Command that executes a query and displays the result in a dialog.
 */
class ExecuteQueryCommand {
    constructor() {
        //#region ----   Services & Dependencies   ----
        this.queryService = inject(AXMQueryService);
        this.layoutBuilder = inject(AXPLayoutBuilderService);
        this.toastService = inject(AXToastService);
        this.translationService = inject(AXTranslationService);
    }
    //#endregion
    //#region ----   Command Implementation   ----
    async execute(input) {
        try {
            // Execute query
            const result = await this.queryService.execute(input.queryId);
            if (!result) {
                const failedTitle = await this.translationService.translateAsync('@data-management:metadata-definitions.components.query.messages.execute-failed.title');
                const failedContent = await this.translationService.translateAsync('@data-management:metadata-definitions.components.query.messages.execute-failed.content', { params: { queryName: input.queryName || input.queryId } });
                this.toastService.show({
                    color: 'warning',
                    title: failedTitle,
                    content: failedContent,
                    closeButton: true,
                    timeOut: 5000,
                });
                return {
                    success: false,
                    message: {
                        text: failedContent,
                    },
                };
            }
            // Format result for display
            const displayValue = this.formatResult(result);
            // Get translated strings
            const dialogTitleBase = await this.translationService.translateAsync('@data-management:metadata-definitions.components.query.components.execute-query-dialog.title');
            const dialogTitle = `${dialogTitleBase}: ${input.queryTitle || input.queryName || input.queryId}`;
            // Show dialog with query result
            const dialogRef = await this.layoutBuilder
                .create()
                .dialog((dialog) => {
                dialog
                    .setTitle(dialogTitle)
                    .setSize('lg')
                    .content((content) => {
                    content.customWidget('json-viewer', (widget) => {
                        widget.path('value');
                    });
                })
                    .setContext({
                    value: displayValue
                })
                    .setActions((actions) => {
                    actions.cancel('@general:actions.close.title');
                });
            })
                .show();
            // Dialog closes automatically when user clicks close
            if (dialogRef.action() === 'cancel') {
                dialogRef.close();
            }
            return {
                success: true,
            };
        }
        catch (error) {
            // Get translated error messages
            const errorTitle = await this.translationService.translateAsync('@data-management:metadata-definitions.components.query.messages.execute-error.title');
            const errorContent = await this.translationService.translateAsync('@data-management:metadata-definitions.components.query.messages.execute-error.content', { params: { error: error.message || 'Unknown error' } });
            // Show error toast
            this.toastService.show({
                color: 'danger',
                title: errorTitle,
                content: errorContent,
                closeButton: true,
                timeOut: 5000,
            });
            return {
                success: false,
                message: {
                    text: errorContent,
                },
            };
        }
    }
    //#endregion
    //#region ----   Utility Methods   ----
    /**
     * Formats query result for display.
     */
    formatResult(result) {
        if (result === null || result === undefined) {
            return null;
        }
        // If result has items and total (standard query result format)
        if (result.items && Array.isArray(result.items)) {
            return {
                total: result.total || result.items.length,
                items: result.items,
                ...(result.metadata ? { metadata: result.metadata } : {}),
            };
        }
        // If result is already an object or array, return as is
        if (typeof result === 'object') {
            return result;
        }
        // For primitive values, wrap in object
        return {
            value: result,
        };
    }
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.3.19", ngImport: i0, type: ExecuteQueryCommand, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); }
    static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "20.3.19", ngImport: i0, type: ExecuteQueryCommand }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.3.19", ngImport: i0, type: ExecuteQueryCommand, decorators: [{
            type: Injectable
        }] });

export { ExecuteQueryCommand };
//# sourceMappingURL=acorex-modules-data-management-execute-query.command-CUKrpbHG.mjs.map
