"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.restoreDialogHierarchy = restoreDialogHierarchy;
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const dialog_1 = require("../../dialog");
const dialog_global_registry_1 = require("../../dialog-global-registry");
const dialog_instance_registry_1 = require("../../dialog-instance-registry");
const log_1 = require("../../log");
const persistence_1 = require("../../persistence");
async function restoreDialogHierarchy(mainDialogId, status = 'running') {
    try {
        const mainDialog = await (0, dialog_instance_registry_1.getOrRestoreMainDialog)(mainDialogId, status);
        if (!mainDialog) {
            throw new Error(`Failed to restore dialog hierarchy for ${mainDialogId} from status ${status}`);
        }
        dialog_global_registry_1.globalDialogRegistry.register(mainDialog);
        const sideDialogs = new Map();
        const rootPath = persistence_1.DialogPersistence.getMainDialogPath(mainDialog.id, status);
        const sideDialogsPath = path_1.default.join(rootPath, 'sideDialogs');
        let allSideDialogIds = [];
        try {
            const entries = await fs_1.default.promises.readdir(sideDialogsPath, { withFileTypes: true });
            allSideDialogIds = entries.filter((entry) => entry.isDirectory()).map((entry) => entry.name);
        }
        catch (err) {
            const code = typeof err === 'object' && err !== null && 'code' in err
                ? err.code
                : undefined;
            if (code !== 'ENOENT') {
                log_1.log.warn(`Failed to read sideDialogs directory: ${sideDialogsPath}, returning empty array`, err);
            }
            allSideDialogIds = [];
        }
        for (const sideDialogId of allSideDialogIds) {
            const restoredSideDialogId = new dialog_1.DialogID(sideDialogId, mainDialog.id.rootId);
            const dialog = await (0, dialog_instance_registry_1.ensureDialogLoaded)(mainDialog, restoredSideDialogId, status);
            if (dialog && dialog.id.selfId !== dialog.id.rootId) {
                sideDialogs.set(sideDialogId, dialog);
            }
        }
        let totalMessages = mainDialog.msgs.length;
        let totalCourses = mainDialog.currentCourse;
        for (const dialog of sideDialogs.values()) {
            totalMessages += dialog.msgs.length;
            if (dialog.currentCourse > totalCourses) {
                totalCourses = dialog.currentCourse;
            }
        }
        const summary = {
            totalMessages,
            totalCourses,
            completionStatus: 'incomplete',
        };
        return {
            mainDialog,
            sideDialogs,
            summary,
        };
    }
    catch (error) {
        log_1.log.error(`Failed to restore dialog hierarchy for ${mainDialogId}:`, error);
        throw error;
    }
}
