const ASSESSMENT_SESSION_VERSION = 1;
const STORAGE_PREFIX = "pie:assessment-session:v1:";
function nowIso() {
    return new Date().toISOString();
}
export function getAssessmentSessionStorageKey(assessmentAttemptSessionIdentifier) {
    return `${STORAGE_PREFIX}${assessmentAttemptSessionIdentifier}`;
}
export function loadAssessmentSession(storage, assessmentAttemptSessionIdentifier) {
    const raw = storage.getItem(getAssessmentSessionStorageKey(assessmentAttemptSessionIdentifier));
    if (!raw)
        return null;
    try {
        const parsed = JSON.parse(raw);
        if (!parsed || parsed.version !== ASSESSMENT_SESSION_VERSION)
            return null;
        if (parsed.assessmentAttemptSessionIdentifier !==
            assessmentAttemptSessionIdentifier) {
            return null;
        }
        return parsed;
    }
    catch {
        return null;
    }
}
export function saveAssessmentSession(storage, session) {
    const updated = {
        ...session,
        updatedAt: nowIso(),
    };
    storage.setItem(getAssessmentSessionStorageKey(session.assessmentAttemptSessionIdentifier), JSON.stringify(updated));
}
export function createNewAssessmentSession(args) {
    const startedAt = nowIso();
    const initialSectionIdentifier = args.sectionIdentifiers[0];
    return {
        version: ASSESSMENT_SESSION_VERSION,
        assessmentAttemptSessionIdentifier: args.assessmentAttemptSessionIdentifier,
        assessmentId: args.assessmentId,
        startedAt,
        updatedAt: startedAt,
        navigationState: {
            currentSectionIndex: 0,
            visitedSectionIdentifiers: initialSectionIdentifier
                ? [initialSectionIdentifier]
                : [],
            currentSectionIdentifier: initialSectionIdentifier,
        },
        realization: {
            seed: args.seed,
            sectionIdentifiers: args.sectionIdentifiers,
        },
        sectionSessions: {},
    };
}
export function upsertSectionSession(session, args) {
    if (!args.sectionIdentifier)
        return session;
    const updatedAt = nowIso();
    return {
        ...session,
        updatedAt,
        sectionSessions: {
            ...session.sectionSessions,
            [args.sectionIdentifier]: {
                sectionIdentifier: args.sectionIdentifier,
                updatedAt,
                session: args.sectionSession,
            },
        },
    };
}
export function setCurrentSectionPosition(session, args) {
    const visited = new Set(session.navigationState.visitedSectionIdentifiers || []);
    if (args.currentSectionIdentifier) {
        visited.add(args.currentSectionIdentifier);
    }
    return {
        ...session,
        navigationState: {
            ...session.navigationState,
            currentSectionIndex: args.currentSectionIndex,
            currentSectionIdentifier: args.currentSectionIdentifier,
            visitedSectionIdentifiers: Array.from(visited),
        },
    };
}
//# sourceMappingURL=AssessmentSession.js.map