import { actionTypes } from 'constants/action-types';
import { versionState } from './updateVersion.types';

export function updateVersion({ targetVersion, show = true, theme = 'black' }) {
  return async (dispatch, getState) => {
    const { user } = getState();

    dispatch({
      type: actionTypes.UPDATE_VERSION_DATA,
      payload: {
        targetVersion,
        currentVersion: user?.userInfo?.edition.introduction,
        endDate: user?.userInfo?.edition.expiredAt,
        show: show,
        theme
      }
    });
  };
}

const initialState: versionState = {
  data: {
    show: false,
    theme: `black`
  }
};

export default function reducer(state = initialState, { type, payload }): versionState {
  switch (type) {
    case actionTypes.UPDATE_VERSION_DATA: {
      return { ...state, data: payload };
    }
    default:
      return state;
  }
}
