import type { PayloadAction } from '../createAction' import type { IsAny } from '../tsHelpers' /** * @public */ export type EntityId = number | string /** * @public */ export type Comparer = (a: T, b: T) => number /** * @public */ export type IdSelector = (model: T) => EntityId /** * @public */ export interface DictionaryNum { [id: number]: T | undefined } /** * @public */ export interface Dictionary extends DictionaryNum { [id: string]: T | undefined } /** * @public */ export type Update = { id: EntityId; changes: Partial } /** * @public */ export interface EntityState { ids: EntityId[] entities: Dictionary } /** * @public */ export interface EntityDefinition { selectId: IdSelector sortComparer: false | Comparer } export type PreventAny = IsAny, S> /** * @public */ export interface EntityStateAdapter { addOne>(state: PreventAny, entity: T): S addOne>( state: PreventAny, action: PayloadAction ): S addMany>( state: PreventAny, entities: readonly T[] | Record ): S addMany>( state: PreventAny, entities: PayloadAction> ): S setOne>(state: PreventAny, entity: T): S setOne>( state: PreventAny, action: PayloadAction ): S setMany>( state: PreventAny, entities: readonly T[] | Record ): S setMany>( state: PreventAny, entities: PayloadAction> ): S setAll>( state: PreventAny, entities: readonly T[] | Record ): S setAll>( state: PreventAny, entities: PayloadAction> ): S removeOne>(state: PreventAny, key: EntityId): S removeOne>( state: PreventAny, key: PayloadAction ): S removeMany>( state: PreventAny, keys: readonly EntityId[] ): S removeMany>( state: PreventAny, keys: PayloadAction ): S removeAll>(state: PreventAny): S updateOne>( state: PreventAny, update: Update ): S updateOne>( state: PreventAny, update: PayloadAction> ): S updateMany>( state: PreventAny, updates: ReadonlyArray> ): S updateMany>( state: PreventAny, updates: PayloadAction>> ): S upsertOne>(state: PreventAny, entity: T): S upsertOne>( state: PreventAny, entity: PayloadAction ): S upsertMany>( state: PreventAny, entities: readonly T[] | Record ): S upsertMany>( state: PreventAny, entities: PayloadAction> ): S } /** * @public */ export interface EntitySelectors { selectIds: (state: V) => EntityId[] selectEntities: (state: V) => Dictionary selectAll: (state: V) => T[] selectTotal: (state: V) => number selectById: (state: V, id: EntityId) => T | undefined } /** * @public */ export interface EntityAdapter extends EntityStateAdapter { selectId: IdSelector sortComparer: false | Comparer getInitialState(): EntityState getInitialState(state: S): EntityState & S getSelectors(): EntitySelectors> getSelectors( selectState: (state: V) => EntityState ): EntitySelectors }