import * as i0 from '@angular/core';
import { inject, Injectable } from '@angular/core';
import { AXPopupService } from '@acorex/components/popup';

//#region ----   Imports   ----
//#endregion
//#region ----   Service   ----
class AXMQuestionnaireViewerService {
    constructor() {
        //#region ----   Services & Dependencies   ----
        this.popupService = inject(AXPopupService);
    }
    //#endregion
    //#region ----   Public Methods   ----
    /**
     * Open questionnaire viewer in a dialog
     */
    async open(config) {
        const { AXMQuestionnaireViewerPopupComponent } = await import('./acorex-modules-assessment-management-questionnaire-viewer-popup.component-B9pP4j23.mjs');
        const structure = config.structure;
        const totalQuestions = structure?.sections?.reduce((s, sec) => s + (sec.items?.length ?? 0), 0) ?? 0;
        const passingScore = config.prePostConfig?.passingScore;
        const submitHandler = 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;
                return { score: progressPercentage, passFail, totalQuestions, answeredCount };
            });
        const result = await this.popupService.open(AXMQuestionnaireViewerPopupComponent, {
            title: config.title,
            size: config.size || 'lg',
            maximizable: true,
            data: {
                structure: config.structure,
                prePostConfig: config.prePostConfig ?? null,
                config: config.config,
                initialAnswers: config.initialAnswers || {},
                submitHandler,
            },
        });
        return result.data || null;
    }
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.3.19", ngImport: i0, type: AXMQuestionnaireViewerService, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); }
    static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "20.3.19", ngImport: i0, type: AXMQuestionnaireViewerService, providedIn: 'root' }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.3.19", ngImport: i0, type: AXMQuestionnaireViewerService, decorators: [{
            type: Injectable,
            args: [{
                    providedIn: 'root',
                }]
        }] });

export { AXMQuestionnaireViewerService as A };
//# sourceMappingURL=acorex-modules-assessment-management-questionnaire-viewer.service-BwxkdgXr.mjs.map
