import { AppState } from '../appState' ; import { ResourceObject } from '../../api/models/jsonapi' ; import { Entity , EntityRef } from './state' ; export const first = < R extends any > ( arr ) : R => ( Array . isArray ( arr ) ? arr [ 0 ] : arr ) ; export const getEntity = < R extends ResourceObject > ( state , type , id ) : Entity < R > => ( state . jsonapi . entities [ type ] ? state . jsonapi . entities [ type ] [ id ] : undefined ) ; export const getEntityResource = < R extends ResourceObject > ( state , type , id ) : R | undefined => state . jsonapi . entities [ type ] && state . jsonapi . entities [ type ] [ id ] && state . jsonapi . entities [ type ] [ id ] . resource ; export const getEntities = < R extends ResourceObject > ( state , entities ) : Entity < R > [ ] => entities ? entities . map ( ( ref ) => getEntity < R > ( state , ref . type , ref . id ) ) . filter ( ( x ) => ! ! x ) : [ ] ; export const getEntityResources = < R extends ResourceObject > ( state , entities ) : R [ ] => entities ? entities . map ( ( ref ) => getEntityResource < R > ( state , ref . type , ref . id ) ) . filter ( ( x ) => ! ! x ) : [ ] ; export const getCollection = ( state , path ) => state . jsonapi . collections [ path ] ; export const getCollectionEntities = < R extends ResourceObject > ( state , path ) : Entity < R > [ ] => getEntities < R > ( state , getCollectionEntityRefs ( state , path ) ) ; export const getCollectionResources = < R extends ResourceObject > ( state , path ) : R [ ] => getEntityResources < R > ( state , getCollectionEntityRefs ( state , path ) ) ; export const getCollectionEntityRefs = ( state , path ) : EntityRef [ ] => state . jsonapi . collections [ path ] && state . jsonapi . collections [ path ] . entities ;