import { AXPTokenDefinitionService } from '@acorex/platform/common';
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';

//#region ----   Imports   ----
//#endregion
//#region ----   Execute Token Command   ----
/**
 * Command that executes a token and displays the result in a dialog.
 */
class ExecuteTokenCommand {
    constructor() {
        //#region ----   Services & Dependencies   ----
        this.tokenService = inject(AXPTokenDefinitionService);
        this.layoutBuilder = inject(AXPLayoutBuilderService);
        this.toastService = inject(AXToastService);
        this.translationService = inject(AXTranslationService);
    }
    //#endregion
    //#region ----   Command Implementation   ----
    async execute(input) {
        try {
            // Get token value
            const tokenValue = await this.tokenService.getValue(input.tokenName);
            if (tokenValue === undefined || tokenValue === null) {
                const failedTitle = await this.translationService.translateAsync('@platform-management:tokens.messages.execute-failed.title');
                const failedContent = await this.translationService.translateAsync('@platform-management:tokens.messages.execute-failed.content', { params: { tokenName: input.tokenName } });
                this.toastService.show({
                    color: 'warning',
                    title: failedTitle,
                    content: failedContent,
                    closeButton: true,
                    timeOut: 5000,
                });
                return {
                    success: false,
                    message: {
                        text: failedContent,
                    },
                };
            }
            // Check if value is an object or array (for JsonViewer) or string (for largeTextBox)
            const isObjectValue = tokenValue !== null && typeof tokenValue === 'object';
            const displayValue = isObjectValue ? tokenValue : this.formatValueAsString(tokenValue);
            // Get translated strings
            const dialogTitleKey = '@platform-management:tokens.components.execute-token-dialog.title';
            const dialogTitleBase = await this.translationService.translateAsync(dialogTitleKey);
            const dialogTitle = `${dialogTitleBase}: ${input.tokenName}`;
            // Show dialog with token value
            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
            // The promise resolves when user clicks an action
            if (dialogRef.action() === 'cancel') {
                dialogRef.close();
            }
            return {
                success: true,
            };
        }
        catch (error) {
            // Get translated error messages
            const errorTitle = await this.translationService.translateAsync('@platform-management:tokens.messages.execute-error.title');
            const errorContent = await this.translationService.translateAsync('@platform-management:tokens.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 a value as string for display.
     */
    formatValueAsString(value) {
        if (value === null || value === undefined) {
            return '';
        }
        if (typeof value === 'string') {
            return value;
        }
        if (typeof value === 'object') {
            try {
                return JSON.stringify(value, null, 2);
            }
            catch {
                return String(value);
            }
        }
        return String(value);
    }
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.3.19", ngImport: i0, type: ExecuteTokenCommand, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); }
    static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "20.3.19", ngImport: i0, type: ExecuteTokenCommand }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.3.19", ngImport: i0, type: ExecuteTokenCommand, decorators: [{
            type: Injectable
        }] });

export { ExecuteTokenCommand };
//# sourceMappingURL=acorex-modules-platform-management-execute-token.command-C1ZoX8lQ.mjs.map
