import * as i0 from '@angular/core';
import { inject, Injectable } from '@angular/core';
import { AXPLayoutBuilderService } from '@acorex/platform/layout/builder';
import { AXTranslationService } from '@acorex/core/translation';
import { AXToastService } from '@acorex/components/toast';

class AXPEmailJsaCommand {
    constructor() {
        //#region ----   Services & Dependencies   ----
        this.layoutBuilder = inject(AXPLayoutBuilderService);
        this.translationService = inject(AXTranslationService);
        this.toastService = inject(AXToastService);
    }
    //#endregion
    //#region ----   Command Execution   ----
    async execute(input) {
        // Show dialog and handle the entire flow
        return await this.showEmailDialog();
    }
    //#endregion
    //#region ----   Dialog Logic   ----
    async showEmailDialog() {
        const dialogRef = await this.layoutBuilder
            .create()
            .dialog(dialog => {
            dialog
                .setTitle('@project-management:jsa.email-dialog.title')
                .setMessage('@project-management:jsa.email-dialog.message')
                .setContext({ selectedUsers: [] })
                .content(flex => {
                flex
                    .setDirection('column')
                    .formField('@project-management:jsa.email-dialog.recipients-label', field => {
                    field.path('selectedUsers');
                    field.lookupBox({
                        entity: 'SecurityManagement.User',
                        textField: 'displayName',
                        valueField: 'id',
                        look: 'select',
                        columns: ['displayName', 'email'],
                        multiple: true,
                        allowClear: true,
                        validations: [
                            {
                                rule: 'required',
                                options: { message: 'Please select at least one recipient' }
                            }
                        ]
                    });
                });
            })
                .setActions(actions => {
                actions.cancel('@general:actions.cancel.title').submit('@project-management:jsa.email-dialog.submit-text');
            });
        })
            .show();
        // Wait for user interaction
        const formData = dialogRef.context();
        const action = dialogRef.action();
        if (action === 'cancel') {
            return { success: false };
        }
        dialogRef.setLoading(true);
        try {
            await this.showEmailSentNotification(formData['selectedUsers']);
            dialogRef.close();
            return { success: true };
        }
        catch (error) {
            // Show error notification
            await this.showEmailErrorNotification(error);
            return { success: false };
        }
    }
    //#endregion
    //#region ----   Notification Logic   ----
    async showEmailSentNotification(selectedUsers) {
        const message = await this.translationService.translateAsync('@project-management:jsa.email-sent.message', {
            params: { count: selectedUsers?.length || 0 }
        });
        const title = await this.translationService.translateAsync('@project-management:jsa.email-sent.title');
        this.toastService.success(`${title}: ${message}`);
    }
    async showEmailErrorNotification(error) {
        const title = await this.translationService.translateAsync('@project-management:jsa.email-error.title');
        const message = error?.message || await this.translationService.translateAsync('@project-management:jsa.email-error.message');
        this.toastService.danger(`${title}: ${message}`);
    }
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.3.4", ngImport: i0, type: AXPEmailJsaCommand, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); }
    static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "20.3.4", ngImport: i0, type: AXPEmailJsaCommand, providedIn: 'root' }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.3.4", ngImport: i0, type: AXPEmailJsaCommand, decorators: [{
            type: Injectable,
            args: [{ providedIn: 'root' }]
        }] });

export { AXPEmailJsaCommand };
//# sourceMappingURL=acorex-modules-project-management-email-jsa.command-BaUjgMlj.mjs.map
