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 { A as AXMQuestionnaireViewerService } from './acorex-modules-assessment-management-questionnaire-viewer.service-BwxkdgXr.mjs';
import './acorex-modules-assessment-management-questionnaire.entity-B8eP0kxN.mjs';
import { A as AXMQuestionnaireService, t as toPrePostConfig } from './acorex-modules-assessment-management-acorex-modules-assessment-management-W8hHssaa.mjs';

//#endregion
//#region ----   Preview Questionnaire Command   ----
/**
 * Command that opens a preview of a questionnaire in a dialog.
 */
class PreviewQuestionnaireCommand {
    constructor() {
        //#region ----   Services & Dependencies   ----
        this.questionnaireService = inject(AXMQuestionnaireService);
        this.questionnaireViewerService = inject(AXMQuestionnaireViewerService);
        this.translationService = inject(AXTranslationService);
        this.toastService = inject(AXToastService);
    }
    //#endregion
    //#region ----   Command Implementation   ----
    async execute(input) {
        try {
            // Get questionnaire data
            const questionnaire = await this.questionnaireService.getOne(input.questionnaireId);
            if (!questionnaire) {
                return {
                    success: false,
                    message: {
                        text: `Questionnaire with ID ${input.questionnaireId} not found`,
                    },
                };
            }
            // Check if questionnaire has structure
            if (!questionnaire.structure || !questionnaire.structure.sections || questionnaire.structure.sections.length === 0) {
                return {
                    success: false,
                    message: {
                        text: 'Questionnaire has no structure to preview',
                    },
                };
            }
            // Get preview title
            const previewTitleKey = '@assessment-management:questionnaires.entities.questionnaire.actions.preview.title';
            const previewTitleBase = await this.translationService.translateAsync(previewTitleKey) || 'Preview';
            const previewTitle = input.questionnaireTitle
                ? `${previewTitleBase}: ${input.questionnaireTitle}`
                : previewTitleBase;
            const cfg = toPrePostConfig(questionnaire);
            const structure = questionnaire.structure;
            const totalQuestions = structure?.sections?.reduce((s, sec) => s + (sec.items?.length ?? 0), 0) ?? 0;
            const passingScore = cfg?.passingScore;
            await this.questionnaireViewerService.open({
                title: previewTitle,
                structure,
                prePostConfig: cfg,
                size: 'lg',
                config: {},
                submitHandler: async (answers) => {
                    const answeredCount = Object.keys(answers).filter((k) => {
                        const v = answers[k];
                        return v != null && v !== '' && !(Array.isArray(v) && v.length === 0);
                    }).length;
                    const progressPercentage = totalQuestions > 0 ? Math.round((answeredCount / totalQuestions) * 100) : 0;
                    const passFail = passingScore != null ? (progressPercentage >= passingScore ? 'Pass' : 'Fail') : undefined;
                    const title = await this.translationService.translateAsync('@general:messages.form.saved.success.title');
                    const content = await this.translationService.translateAsync('@general:messages.form.saved.success.description');
                    this.toastService.show({ color: 'success', title, content });
                    return { score: progressPercentage, passFail, totalQuestions, answeredCount };
                },
            });
            return {
                success: true,
                message: {
                    text: `Submitted Successfully`,
                },
            };
        }
        catch (error) {
            return {
                success: false,
                message: {
                    text: `Error previewing questionnaire: ${error.message || 'Unknown error'}`,
                },
            };
        }
    }
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.3.19", ngImport: i0, type: PreviewQuestionnaireCommand, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); }
    static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "20.3.19", ngImport: i0, type: PreviewQuestionnaireCommand }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.3.19", ngImport: i0, type: PreviewQuestionnaireCommand, decorators: [{
            type: Injectable
        }] });

export { PreviewQuestionnaireCommand };
//# sourceMappingURL=acorex-modules-assessment-management-preview-questionnaire.command-a6iOcFCi.mjs.map
