import * as IllustActions from '@/actions/illust';
import { IIllustAction } from '@/actions/illust';

const types = IllustActions.types;

export interface IIllustState {
  items: any;
  comments: any[];
  page: number;
  isCommentsEnd: boolean;
  isFetching: boolean;
  isError: boolean;
  isFetchingComments: boolean;
  isCommentsError: boolean;
}

export default function illust(
  state: IIllustState = {
    items: {},
    comments: [],
    page: 1,
    isCommentsEnd: false,
    isFetching: true,
    isError: false,
    isFetchingComments: true,
    isCommentsError: false
  },
  action: IIllustAction
): IIllustState {
  switch (action.type) {
    case types.SET_ITEM:
      return {
        ...state,
        items: {
          ...state.items,
          ...{
            [action.payload.data.id]: action.payload.data
          }
        }
      };

    case types.SET_FETCH_ERROR:
      return {
        ...state,
        isError: action.payload.isError
      };

    case types.SET_FETCH_STATUS:
      return {
        ...state,
        isFetching: action.payload.isFetching
      };

    case types.SET_COMMENTS:
      return {
        ...state,
        comments: [...state.comments, ...action.payload.data]
      };

    case types.SET_COMMENTS_PAGE:
      return {
        ...state,
        page: action.payload.page
      };

    case types.SET_COMMENTS_END:
      return {
        ...state,
        isCommentsEnd: action.payload.isCommentsEnd
      };

    case types.SET_FETCH_COMMENTS_ERROR:
      return {
        ...state,
        isCommentsError: action.payload.isError
      };

    case types.SET_FETCH_COMMENTS_STATUS:
      return {
        ...state,
        isFetchingComments: action.payload.isFetching
      };

    case types.CLEAR_COMMENTS:
      return {
        ...state,
        comments: action.payload.comments,
        page: action.payload.page,
        isCommentsEnd: action.payload.isCommentsEnd
      };

    default:
      return state;
  }
}
