import { Selector, createSelector } from '@ngrx/store';
import { StatusBulkInfo } from '@ucap-webmessenger/protocol-status';
import { EntityState, createEntityAdapter } from '@ngrx/entity';

export interface StatusBulkInfoState extends EntityState<StatusBulkInfo> {}

export interface State {
  statusBulkInfo: StatusBulkInfoState;
}

export const adapterStatusBulkInfo = createEntityAdapter<StatusBulkInfo>({
  selectId: statusBulkInfo => statusBulkInfo.userSeq
});

const statusBulkInfoInitialState: StatusBulkInfoState = adapterStatusBulkInfo.getInitialState(
  {}
);

export const initialState: State = {
  statusBulkInfo: statusBulkInfoInitialState
};

const {
  selectAll: ngeSelectAllStatusBulkInfo,
  selectEntities: ngeSelectEntitiesStatusBulkInfo,
  selectIds: ngeSelectIdsStatusBulkInfo,
  selectTotal: ngeSelectTotalStatusBulkInfo
} = adapterStatusBulkInfo.getSelectors();

export function selectors<S>(selector: Selector<any, State>) {
  const selectStatusBulkInfo = createSelector(
    selector,
    (state: State) => state.statusBulkInfo
  );

  return {
    selectAllStatusBulkInfo: createSelector(
      selectStatusBulkInfo,
      ngeSelectAllStatusBulkInfo
    ),
    selectEntitiesStatusBulkInfo: createSelector(
      selectStatusBulkInfo,
      ngeSelectEntitiesStatusBulkInfo
    ),
    selectStatusBulkInfo: (userSeq: number) =>
      createSelector(
        selectStatusBulkInfo,
        ngeSelectEntitiesStatusBulkInfo,
        (_, entities) => (!!entities ? entities[userSeq] : undefined)
      )
  };
}
