import { isNullOrUndefined } from '@coveo/bueno';
import { createSelector } from '@reduxjs/toolkit';
import { stateKey } from '../../../app/state-key.js';
import { totalEntriesPrincipalSelector } from '../pagination/pagination-selectors.js';
/**
 * Duplicate selector since the state is no longer accessible externally
 * TODO: KIT-3199: Update all other selectors to use the engine as a parameter
 */
export const responseIdSelectorFromEngine = createSelector((engine) => engine[stateKey].productListing.responseId, (responseId) => responseId);
export const responseIdSelector = createSelector((state) => state.productListing.responseId, (responseId) => responseId);
export const requestIdSelector = createSelector((state) => state.productListing.requestId, (requestId) => requestId);
export const numberOfProductsSelector = createSelector((state) => state.productListing?.products.length || 0, (len) => len);
export const moreProductsAvailableSelector = createSelector((state) => ({
    total: totalEntriesPrincipalSelector(state),
    current: numberOfProductsSelector(state),
}), ({ current, total }) => current < total);
export const isLoadingSelector = createSelector((state) => state.productListing?.isLoading, (isLoading) => (isNullOrUndefined(isLoading) ? false : isLoading));
export const errorSelector = createSelector((state) => state.productListing?.error, (error) => error ?? null);
