'use strict';

var logger = require('./logger.js');
var queryMappers = require('./queryMappers.js');

function getActiveJourneyValue(req, keyParts, // { reportId, id, tableId?, executionId? }
field) {
    const store = req.session?.activeReport;
    if (!store)
        return undefined;
    logger.default.info('getActiveJourneyValue', keyParts, field);
    const { reportId, id, executionId, tableId } = keyParts;
    const baseKey = buildJourneyKey({ reportId, id });
    const execKey = executionId ? buildJourneyKey({ reportId, id, executionId }) : undefined;
    const tableKey = tableId ? buildJourneyKey({ reportId, id, tableId }) : undefined;
    // 1. Table key takes highest priority (final async stage)
    if (tableKey && store[tableKey]) {
        const value = store[tableKey][field];
        logger.default.info('getActiveJourneyValue: tableKey', { field, value });
        return value;
    }
    // 2. Execution key is next (loading async stage)
    if (execKey && store[execKey]) {
        const value = store[execKey][field];
        logger.default.info('getActiveJourneyValue: execKey', { field, value });
        return value;
    }
    // 3. Fall back to base key (sync or start of async)
    const value = store[baseKey]?.[field];
    logger.default.info('getActiveJourneyValue: base', { field, value });
    return value;
}
/**
 * Sets a key value on the active report session
 *
 * @template F
 * @param {Request} req
 * @param {ActiveJourneyKey} keyParts
 * @param {F} field
 * @param {ActiveReportSessionData[F]} value
 */
function setActiveJourneyValue(req, keyParts, field, value) {
    if (!req.session)
        return;
    if (!req.session.activeReport) {
        req.session.activeReport = {};
    }
    const store = req.session.activeReport;
    const { reportId, id, executionId, tableId } = keyParts;
    const baseKey = buildJourneyKey({ reportId, id });
    const execKey = executionId ? buildJourneyKey({ reportId, id, executionId }) : undefined;
    const tableKey = tableId ? buildJourneyKey({ reportId, id, tableId }) : undefined;
    const targetKey = tableKey ?? execKey ?? baseKey;
    if (!store[targetKey]) {
        store[targetKey] = {};
    }
    store[targetKey][field] = value;
}
/**
 * sets the current sort search on the active report session
 *
 * @export
 * @param {Request} req
 * @param {{ reportId: string; id: string; tableId?: string }} sessionKey
 * @param {(Record<string, string> | undefined)} sortData
 */
function setActiveJourneySortSearch(req, sessionKey, sortData) {
    if (sortData) {
        setActiveJourneyValue(req, sessionKey, 'requestedSortSearch', queryMappers.queryObjectToQs(sortData));
    }
}
/**
 * Builds the active journey key
 *
 * @export
 * @param {ActiveJourneyKey} { reportId, id, type, executionId, tableId }
 * @return {*}  {string}
 */
function buildJourneyKey({ reportId, id, executionId, tableId }) {
    const parts = [reportId, id];
    if (executionId)
        parts.push(`exec:${executionId}`);
    if (tableId)
        parts.push(`table:${tableId}`);
    return parts.join(':');
}

exports.buildJourneyKey = buildJourneyKey;
exports.getActiveJourneyValue = getActiveJourneyValue;
exports.setActiveJourneySortSearch = setActiveJourneySortSearch;
//# sourceMappingURL=sessionHelper.js.map
