import { debounce } from 'ts-debounce';
import { configuration } from '../../../app/common-reducers.js';
import { loadReducerError } from '../../../utils/errors.js';
/**
 * Creates the result controller core.
 *
 * @param engine - The headless engine.
 * @param props - The configurable controller properties.
 * @param action - The action to be triggered on select.
 * @returns A controller core instance.
 */
export function buildInteractiveResultCore(engine, props, action) {
    if (!loadInteractiveResultCoreReducers(engine)) {
        throw loadReducerError;
    }
    // 1 second is a reasonable amount of time to catch most longpress actions.
    const defaultDelay = 1000;
    const options = {
        selectionDelay: defaultDelay,
        debounceWait: defaultDelay,
        ...props.options,
    };
    let longPressTimer;
    return {
        select: debounce(action, options.debounceWait, { isImmediate: true }),
        beginDelayedSelect() {
            longPressTimer = setTimeout(action, options.selectionDelay);
        },
        cancelPendingSelect() {
            longPressTimer && clearTimeout(longPressTimer);
        },
    };
}
function loadInteractiveResultCoreReducers(engine) {
    engine.addReducers({ configuration });
    return true;
}
