export function getCachedCandidateSnapshot(cacheKey, loadCandidates, resolutionContext) {
    if (!resolutionContext) {
        return loadCandidates();
    }
    const cached = resolutionContext.cache.get(cacheKey);
    if (cached) {
        return cached;
    }
    const candidates = loadCandidates();
    resolutionContext.cache.set(cacheKey, candidates);
    return candidates;
}
export function readResolvedBinding(resolutionContext, bindingName) {
    const resolvedBinding = resolutionContext?.resolvedBindings.get(bindingName);
    if (!resolvedBinding || typeof resolvedBinding !== 'object') {
        return undefined;
    }
    return resolvedBinding;
}
export function readResolvedBindingNumber(resolutionContext, bindingName) {
    const resolvedBinding = readResolvedBinding(resolutionContext, bindingName);
    if (!resolvedBinding) {
        return undefined;
    }
    const number = resolvedBinding.number;
    return typeof number === 'number' ? number : undefined;
}
export function readPreviousSelectedNumber(state, stepName, bindingName) {
    const context = state.systemContexts.get(stepName);
    if (!context || typeof context !== 'object') {
        return undefined;
    }
    const selectedItem = context[bindingName];
    if (!selectedItem || typeof selectedItem !== 'object') {
        return undefined;
    }
    const number = selectedItem.number;
    return typeof number === 'number' ? number : undefined;
}
export function selectNextCandidate(candidates, previousNumber) {
    if (candidates.length === 0) {
        return undefined;
    }
    if (previousNumber === undefined) {
        return candidates[0];
    }
    const previousIndex = candidates.findIndex((candidate) => candidate.number === previousNumber);
    if (previousIndex === -1) {
        return candidates[0];
    }
    return candidates[(previousIndex + 1) % candidates.length];
}
//# sourceMappingURL=system-selection-helpers.js.map