function nowIso() {
    return new Date().toISOString();
}
function getItemIdentifier(itemRef) {
    return (itemRef.identifier || itemRef.item?.identifier || itemRef.item?.id || null);
}
function resolveItemIdentifiers(args) {
    const refs = args.itemRefs || args.activityDefinition?.assessmentItemRefs || [];
    const identifiers = refs
        .map((itemRef) => getItemIdentifier(itemRef))
        .filter((itemIdentifier) => !!itemIdentifier);
    return Array.from(new Set(identifiers));
}
function resolveAssessmentId(args) {
    return (args.assessmentId ||
        args.activityDefinition?.assessmentId ||
        args.activityDefinition?.identifier ||
        args.activityDefinition?.id ||
        "unknown-assessment");
}
function resolveTestAttemptSessionIdentifier(args, assessmentId) {
    return (args.testAttemptSessionIdentifier ||
        args.activitySession?.activitySessionId ||
        args.activitySession?.id ||
        `tas_${assessmentId}`);
}
function resolveBackendItemSessions(args) {
    return {
        ...(args.activitySession?.itemSessions || {}),
        ...(args.itemSessionsByItemIdentifier || {}),
    };
}
function mapItemSessions(itemIdentifiers, backendItemSessions) {
    return itemIdentifiers.reduce((acc, itemIdentifier) => {
        const rawSession = backendItemSessions[itemIdentifier];
        if (!rawSession)
            return acc;
        const pieSessionId = typeof rawSession.id === "string" ? rawSession.id : undefined;
        const attemptCount = typeof rawSession.attemptCount === "number" &&
            rawSession.attemptCount > 0
            ? rawSession.attemptCount
            : pieSessionId
                ? 1
                : 0;
        const isCompleted = typeof rawSession.isCompleted === "boolean"
            ? rawSession.isCompleted
            : Boolean(rawSession.complete);
        const startedAt = typeof rawSession.startedAt === "string"
            ? rawSession.startedAt
            : undefined;
        const updatedAt = typeof rawSession.updatedAt === "string"
            ? rawSession.updatedAt
            : undefined;
        const completedAt = typeof rawSession.completedAt === "string"
            ? rawSession.completedAt
            : undefined;
        acc[itemIdentifier] = {
            itemIdentifier,
            pieSessionId,
            attemptCount,
            isCompleted,
            startedAt,
            updatedAt,
            completedAt,
            session: rawSession,
        };
        return acc;
    }, {});
}
export function mapActivityToTestAttemptSession(args) {
    const assessmentId = resolveAssessmentId(args);
    const itemIdentifiers = resolveItemIdentifiers(args);
    const testAttemptSessionIdentifier = resolveTestAttemptSessionIdentifier(args, assessmentId);
    const backendItemSessions = resolveBackendItemSessions(args);
    const now = nowIso();
    const startedAt = args.activitySession?.startedAt || now;
    const updatedAt = args.activitySession?.updatedAt || startedAt;
    return {
        version: 1,
        testAttemptSessionIdentifier,
        assessmentId,
        startedAt,
        updatedAt,
        completedAt: args.activitySession?.completedAt,
        navigationState: {
            currentItemIndex: args.activitySession?.currentItemIndex ?? -1,
            visitedItemIdentifiers: [
                ...(args.activitySession?.visitedItemIdentifiers || []),
            ],
            currentSectionIdentifier: args.activitySession?.currentSectionIdentifier,
        },
        realization: {
            seed: testAttemptSessionIdentifier,
            itemIdentifiers,
        },
        itemSessions: mapItemSessions(itemIdentifiers, backendItemSessions),
    };
}
export function toItemSessionsRecord(testAttemptSession) {
    return Object.entries(testAttemptSession.itemSessions).reduce((acc, [itemIdentifier, itemSession]) => {
        if (itemSession.session) {
            acc[itemIdentifier] = itemSession.session;
        }
        return acc;
    }, {});
}
export function buildActivitySessionPatchFromTestAttemptSession(testAttemptSession) {
    return {
        activitySession: {
            id: testAttemptSession.testAttemptSessionIdentifier,
            startedAt: testAttemptSession.startedAt,
            updatedAt: testAttemptSession.updatedAt,
            completedAt: testAttemptSession.completedAt,
            currentItemIndex: testAttemptSession.navigationState.currentItemIndex,
            currentSectionIdentifier: testAttemptSession.navigationState.currentSectionIdentifier,
            visitedItemIdentifiers: [
                ...testAttemptSession.navigationState.visitedItemIdentifiers,
            ],
            itemSessions: toItemSessionsRecord(testAttemptSession),
        },
    };
}
export function buildActivitySessionItemUpdate(testAttemptSession, itemIdentifier) {
    const itemSession = testAttemptSession.itemSessions[itemIdentifier];
    return {
        activitySession: {
            id: testAttemptSession.testAttemptSessionIdentifier,
            currentItemIndex: testAttemptSession.navigationState.currentItemIndex,
            currentSectionIdentifier: testAttemptSession.navigationState.currentSectionIdentifier,
            visitedItemIdentifiers: [
                ...testAttemptSession.navigationState.visitedItemIdentifiers,
            ],
            itemSessions: itemSession?.session
                ? { [itemIdentifier]: itemSession.session }
                : {},
        },
    };
}
//# sourceMappingURL=activity-to-test-attempt-session.js.map