import * as i0 from '@angular/core';
import { inject, Injectable } from '@angular/core';
import { AXMProjectManagementJsaService } from './acorex-modules-project-management.mjs';
import { AXPLayoutBuilderService } from '@acorex/platform/layout/builder';

class AXPReassignJsaCommand {
    constructor() {
        //#region ----   Services & Dependencies   ----
        this.jsaService = inject(AXMProjectManagementJsaService);
        this.layoutBuilder = inject(AXPLayoutBuilderService);
    }
    //#endregion
    //#region ----   Command Execution   ----
    async execute(input) {
        // Get current JSA data to show current assignee
        const currentJsa = await this.jsaService.getOne(input.id);
        // Show dialog to select new assignee
        const dialogResult = await this.showReassignDialog(currentJsa);
        if (dialogResult.cancelled) {
            return { success: false };
        }
        // Update JSA with new assigned user
        await this.jsaService.updateOne(input.id, {
            assignedToUserId: dialogResult.user?.id,
            assignedToUser: {
                id: dialogResult.user?.id,
                displayName: dialogResult.user?.displayName,
            },
        });
        return { success: true };
    }
    //#endregion
    //#region ----   Dialog Logic   ----
    async showReassignDialog(currentJsa) {
        const dialogRef = await this.layoutBuilder
            .create()
            .dialog(dialog => {
            dialog
                .setTitle('@project-management:jsa.reassign-dialog.title')
                .setMessage('@project-management:jsa.reassign-dialog.message')
                .setContext({
                currentAssignee: currentJsa.assignedToUser?.displayName || 'Unassigned',
                assignedToUserId: '',
                user: null
            })
                .content(flex => {
                flex
                    .setDirection('column')
                    .formField('@project-management:jsa.reassign-dialog.current-assignee-label', field => {
                    field.path('currentAssignee');
                    field.textBox().disabled(true);
                })
                    .formField('@project-management:jsa.reassign-dialog.assignee-label', field => {
                    field.path('assignedToUserId');
                    field.lookupBox({
                        entity: 'SecurityManagement.User',
                        textField: 'displayName',
                        valueField: 'id',
                        look: 'select',
                        columns: ['displayName'],
                        expose: [
                            { source: 'id', target: 'user.id' },
                            { source: 'displayName', target: 'user.displayName' },
                        ],
                        validations: [
                            {
                                rule: 'required',
                                options: { message: 'Please select a new assignee' }
                            }
                        ]
                    });
                });
            })
                .setActions(actions => {
                actions.cancel('@general:actions.cancel.title').submit('@project-management:jsa.reassign-dialog.submit-text');
            });
        })
            .show();
        const formData = dialogRef.context();
        const action = dialogRef.action();
        dialogRef.close();
        return {
            user: formData['user'],
            cancelled: action === 'cancel'
        };
    }
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.3.4", ngImport: i0, type: AXPReassignJsaCommand, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); }
    static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "20.3.4", ngImport: i0, type: AXPReassignJsaCommand, providedIn: 'root' }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.3.4", ngImport: i0, type: AXPReassignJsaCommand, decorators: [{
            type: Injectable,
            args: [{ providedIn: 'root' }]
        }] });

export { AXPReassignJsaCommand };
//# sourceMappingURL=acorex-modules-project-management-reassign-jsa.command-BYYLT92Y.mjs.map
