import { HttpError } from '@bcs/sdk/lib/sdk-core/models';
import { ApiResource } from '@bcs/sdk/lib/sdk-core/monads/api-resource';
import { AnyActionWithPayload } from '@bcs/sdk/lib/sdk-core/persistence';
import { RemoteData } from '@devexperts/remote-data-ts';

import { DEFAULT_STATE } from './constants';
import { IBankSignerModuleActions, IOwnStore, ISignerInfo } from './types';

export default (actions: IBankSignerModuleActions) => {
  const bankSigners = (
    state: IOwnStore['bankSigners'] = DEFAULT_STATE.bankSigners,
    { type, payload }: AnyActionWithPayload<RemoteData<HttpError<string>, ApiResource<ISignerInfo[]>>>,
  ) => {
    switch (type) {
      case actions.getBankSignersConst.start:
      case actions.getBankSignersConst.finish:
        return payload;

      default:
        return state;
    }
  };

  const sentStatus = (
    state: IOwnStore['sentStatus'] = DEFAULT_STATE.sentStatus,
    { type, payload }: AnyActionWithPayload<RemoteData<HttpError<string>, ApiResource<string>>>,
  ) => {
    switch (type) {
      case actions.postBankSignersConst.start:
      case actions.postBankSignersConst.finish:
        return payload;

      default:
        return state;
    }
  };

  return {
    bankSigners,
    sentStatus,
  };
};
