import * as i0 from '@angular/core';
import { inject, Injectable } from '@angular/core';
import { AXMProjectManagementJsaService } from './acorex-modules-project-management.mjs';
import { AXPTemplateViewerService } from '@acorex/platform/layout/components';
import { AXTranslationService } from '@acorex/core/translation';
import { AXMFormTemplateManagementTemplateEntityService } from '@acorex/modules/form-template-management';

class AXPSetupJsaCommand {
    constructor() {
        //#region ----   Services & Dependencies   ----
        this.jsaService = inject(AXMProjectManagementJsaService);
        this.templateViewerService = inject(AXPTemplateViewerService);
        this.translationService = inject(AXTranslationService);
        this.templateService = inject(AXMFormTemplateManagementTemplateEntityService);
    }
    //#endregion
    //#region ----   Command Execution   ----
    async execute(input) {
        // Get JSA data to access template
        const jsa = await this.jsaService.getOne(input.id);
        if (!jsa) {
            return { success: false };
        }
        // Show template viewer to collect setup data
        const dialogResult = await this.showSetupDialog(jsa);
        if (dialogResult.cancelled) {
            return { success: false };
        }
        // Update JSA with template data
        await this.jsaService.updateOne(input.id, {
            status: 'Setup',
            templateData: dialogResult.context,
            // Add template data fields when they exist in the entity
            // templateData: dialogResult.context,
            // setupAt: new Date().toISOString(),
        });
        return { success: true };
    }
    //#endregion
    //#region ----   Dialog Logic   ----
    async showSetupDialog(jsa) {
        const title = `${await this.translationService.translateAsync('@project-management:jsa.setup-dialog.title')} - ${jsa.title}`;
        // Get actual JSA template from template service
        let template = null;
        if (jsa.templateId) {
            try {
                const templateData = await this.templateService.getOne(jsa.templateId);
                if (templateData && templateData.template) {
                    // Parse the template JSON if it's a string
                    template = typeof templateData.template === 'string'
                        ? JSON.parse(templateData.template)
                        : templateData.template;
                }
            }
            catch (error) {
                console.error('Failed to load template:', error);
            }
        }
        // Fallback to placeholder template if no template found
        if (!template) {
            template = {
                type: 'document',
                children: [{
                        type: 'page-layout',
                        children: [{
                                type: 'text-block-layout',
                                options: {
                                    content: 'Empty Template'
                                }
                            }]
                    }]
            };
        }
        const config = {
            template,
            title,
            size: 'lg',
            context: jsa.templateData,
            readOnly: false,
            showActions: true
        };
        const result = await this.templateViewerService.showTemplate(config);
        return { context: result.context, cancelled: result.cancelled };
    }
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.3.4", ngImport: i0, type: AXPSetupJsaCommand, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); }
    static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "20.3.4", ngImport: i0, type: AXPSetupJsaCommand, providedIn: 'root' }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.3.4", ngImport: i0, type: AXPSetupJsaCommand, decorators: [{
            type: Injectable,
            args: [{ providedIn: 'root' }]
        }] });

export { AXPSetupJsaCommand };
//# sourceMappingURL=acorex-modules-project-management-setup-jsa.command-aA_WYmob.mjs.map
