import type { InventoryState } from '.';

// Minimal state shape a consuming store must expose for these selectors. Any
// feature store that composes `inventoryReducer` under the `inventory` key
// satisfies it.
export interface WithInventory {
  inventory: InventoryState;
}

export const selectInventoryMapping = (state: WithInventory) => state.inventory.mapping;
export const selectInventoryLoading = (state: WithInventory) => state.inventory.loading;
export const selectInventoryLoaded = (state: WithInventory) => state.inventory.loaded;
export const selectInventoryError = (state: WithInventory) => state.inventory.error;

// True once variables have been fetched and at least one attribute exists.
// The list view uses this to gate the "Add group" button and show the
// "requires reported inventory data" message.
export const selectHasInventory = (state: WithInventory) =>
  state.inventory.loaded && Object.keys(state.inventory.mapping).length > 0;
