import { Type } from '@angular/core';
import { Action, combineReducers, Selector, createSelector } from '@ngrx/store';

import * as CompanyStore from './company';
import * as InitStore from './init';
import * as VersionInfoStore from './version-info';

export interface State {
  company: CompanyStore.State;
  init: InitStore.State;
  versionInfo: VersionInfoStore.State;
}

export const effects: Type<any>[] = [
  CompanyStore.Effects,
  InitStore.Effects,
  VersionInfoStore.Effects
];

export function reducers(state: State | undefined, action: Action) {
  return combineReducers({
    company: CompanyStore.reducer,
    init: InitStore.reducer,
    versionInfo: VersionInfoStore.reducer
  })(state, action);
}

export function selectors<S>(selector: Selector<any, State>) {
  return {
    CompanySelector: CompanyStore.selectors(
      createSelector(
        selector,
        (state: State) => state.company
      )
    ),
    InitSelector: InitStore.selectors(
      createSelector(
        selector,
        (state: State) => state.init
      )
    ),
    VersionInfoSelector: VersionInfoStore.selectors(
      createSelector(
        selector,
        (state: State) => state.versionInfo
      )
    )
  };
}
