"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getActionSequenceRowCount = getActionSequenceRowCount;
exports.exceedsDatasetRowLimit = exceedsDatasetRowLimit;
exports.getEditingUserResponseSelectedRowCount = getEditingUserResponseSelectedRowCount;
exports.shouldShowDatasetRowLimitBannerForUserResponse = shouldShowDatasetRowLimitBannerForUserResponse;
exports.turnHasActions = turnHasActions;
exports.getLastActionTurnIndex = getLastActionTurnIndex;
exports.isLastActionTurnId = isLastActionTurnId;
exports.getLastUserResponseActionRef = getLastUserResponseActionRef;
exports.getLastSequenceUserResponseActionRef = getLastSequenceUserResponseActionRef;
exports.getMinSequenceDatasetRows = getMinSequenceDatasetRows;
exports.countUserSequenceGeneratedActions = countUserSequenceGeneratedActions;
exports.shouldShowDatasetRowLimitBannerForAction = shouldShowDatasetRowLimitBannerForAction;
function getTurnActions(turn) {
    return [...(turn.actionsAbove ?? []), ...(turn.actionsBelow ?? [])];
}
function isSequenceUserResponseAction(action) {
    return action.type === 'userResponse' && action.sequenceType === 'sequence';
}
function isUserResponseAction(action) {
    return action.type === 'userResponse';
}
function getActionAtRef(turns, ref) {
    const turn = turns.find((t) => String(t.id) === String(ref.turnId));
    if (!turn) {
        return undefined;
    }
    const actions = ref.position === 'above' ? turn.actionsAbove : turn.actionsBelow;
    return actions?.[ref.actionIndex];
}
const RAW_PROMPT_LINKED_DATASET_REGEX = /\{\{([^}/]+)\/([^}]+)\}\}/;
function rawPromptHasLinkedDatasets(rawPrompt) {
    if (!rawPrompt?.trim()) {
        return false;
    }
    return RAW_PROMPT_LINKED_DATASET_REGEX.test(rawPrompt);
}
/** Row count for a sequence user-response action (selected rows or HuggingFace / raw row count). */
function getActionSequenceRowCount(action) {
    if (!isSequenceUserResponseAction(action)) {
        return undefined;
    }
    if (action.datasetSelectionType === 'raw') {
        const total = Number(action.totalTestCase ?? 0);
        return Number.isFinite(total) && total > 0 ? total : undefined;
    }
    if (Array.isArray(action.selectedColumnValues) && action.selectedColumnValues.length > 0) {
        return action.selectedColumnValues.length;
    }
    const total = Number(action.totalTestCase ?? 0);
    if (Number.isFinite(total) && total > 0) {
        return total;
    }
    return undefined;
}
function exceedsDatasetRowLimit(selectedRowCount, rowLimit) {
    if (rowLimit == null || rowLimit <= 0 || selectedRowCount == null) {
        return false;
    }
    return selectedRowCount > rowLimit;
}
/** Live row count while editing UserResponseInput (sequence type only). */
function getEditingUserResponseSelectedRowCount(data, live) {
    if (data.sequenceType !== 'sequence') {
        return undefined;
    }
    if (data.datasetSelectionType === 'raw') {
        const rawPromptText = live?.rawPromptText ?? '';
        if (!rawPromptHasLinkedDatasets(rawPromptText)) {
            return undefined;
        }
        const count = Number(live?.rawPromptRowCount ?? data.totalTestCase ?? 0);
        return Number.isFinite(count) && count > 0 ? count : undefined;
    }
    const hfCount = Number(live?.huggingfaceRowCount ?? 0);
    if (Number.isFinite(hfCount) && hfCount > 0) {
        return hfCount;
    }
    const selectedLen = Array.isArray(data.selectedColumnValues)
        ? data.selectedColumnValues.length
        : 0;
    if (selectedLen > 0) {
        return selectedLen;
    }
    const total = Number(data.totalTestCase ?? 0);
    return Number.isFinite(total) && total > 0 ? total : undefined;
}
function shouldShowDatasetRowLimitBannerForUserResponse(data, rowLimit, live) {
    if (data.sequenceType !== 'sequence') {
        return false;
    }
    return exceedsDatasetRowLimit(getEditingUserResponseSelectedRowCount(data, live), rowLimit);
}
function isSequenceUserResponseWithRowCount(action) {
    return isSequenceUserResponseAction(action) && getActionSequenceRowCount(action) !== undefined;
}
/** True when a turn has at least one saved action (not an empty trailing placeholder turn). */
function turnHasActions(turn) {
    return ((Array.isArray(turn.actionsAbove) && turn.actionsAbove.length > 0) ||
        (Array.isArray(turn.actionsBelow) && turn.actionsBelow.length > 0));
}
/** Index of the last turn that has actions; walks back past empty trailing turns. */
function getLastActionTurnIndex(turns) {
    if (!Array.isArray(turns) || turns.length === 0) {
        return -1;
    }
    for (let i = turns.length - 1; i >= 0; i--) {
        if (turnHasActions(turns[i])) {
            return i;
        }
    }
    return turns.length - 1;
}
function isLastActionTurnId(turns, turnId) {
    if (!Array.isArray(turns) || turns.length === 0 || turnId == null) {
        return false;
    }
    const lastActionIndex = getLastActionTurnIndex(turns);
    if (lastActionIndex < 0) {
        return false;
    }
    return String(turns[lastActionIndex]?.id) === String(turnId);
}
/** Last user-response in conversation order (sequence or AI; actionsAbove → actionsBelow per turn). */
function getLastUserResponseActionRef(turns) {
    let last = null;
    for (const turn of turns) {
        for (let actionIndex = 0; actionIndex < (turn.actionsAbove ?? []).length; actionIndex++) {
            const action = turn.actionsAbove[actionIndex];
            if (isUserResponseAction(action)) {
                last = {
                    turnId: turn.id,
                    position: 'above',
                    actionIndex,
                    actionId: action.id,
                };
            }
        }
        for (let actionIndex = 0; actionIndex < (turn.actionsBelow ?? []).length; actionIndex++) {
            const action = turn.actionsBelow[actionIndex];
            if (isUserResponseAction(action)) {
                last = {
                    turnId: turn.id,
                    position: 'below',
                    actionIndex,
                    actionId: action.id,
                };
            }
        }
    }
    return last;
}
/** Last sequence user-response in conversation order (actionsAbove → actionsBelow per turn). */
function getLastSequenceUserResponseActionRef(turns) {
    let last = null;
    for (const turn of turns) {
        for (let actionIndex = 0; actionIndex < (turn.actionsAbove ?? []).length; actionIndex++) {
            const action = turn.actionsAbove[actionIndex];
            if (isSequenceUserResponseAction(action)) {
                last = {
                    turnId: turn.id,
                    position: 'above',
                    actionIndex,
                    actionId: action.id,
                };
            }
        }
        for (let actionIndex = 0; actionIndex < (turn.actionsBelow ?? []).length; actionIndex++) {
            const action = turn.actionsBelow[actionIndex];
            if (isSequenceUserResponseAction(action)) {
                last = {
                    turnId: turn.id,
                    position: 'below',
                    actionIndex,
                    actionId: action.id,
                };
            }
        }
    }
    return last;
}
/** Minimum dataset rows selected across all sequence user-response steps in the conversation. */
function getMinSequenceDatasetRows(turns) {
    const allSequenceActions = turns.flatMap((turn) => getTurnActions(turn).filter(isSequenceUserResponseWithRowCount));
    if (allSequenceActions.length === 0) {
        return undefined;
    }
    const lengths = allSequenceActions
        .map((action) => getActionSequenceRowCount(action))
        .filter((len) => len !== undefined);
    if (lengths.length === 0) {
        return undefined;
    }
    return Math.min(...lengths);
}
function countUserSequenceGeneratedActions(turns) {
    return turns.reduce((count, turn) => {
        const sequenceActions = getTurnActions(turn).filter(isSequenceUserResponseAction);
        return count + sequenceActions.length;
    }, 0);
}
/** Show dataset row cap banner on a saved sequence user-response when selection exceeds the cap. */
function shouldShowDatasetRowLimitBannerForAction(turns, rowLimit, target) {
    if (rowLimit == null || rowLimit <= 0 || target.isNewSequenceAction) {
        return false;
    }
    const actionIndex = target.actionIndex;
    if (typeof actionIndex !== 'number') {
        return false;
    }
    const action = getActionAtRef(turns, {
        turnId: target.turnId,
        position: target.position,
        actionIndex,
        actionId: target.actionId,
    });
    if (!action || !isSequenceUserResponseAction(action)) {
        return false;
    }
    return exceedsDatasetRowLimit(getActionSequenceRowCount(action), rowLimit);
}
//# sourceMappingURL=getMinSequenceDatasetRows.js.map