import { call, put } from 'redux-saga/effects';
// import _ from 'lodash';
// import userDefaults from 'react-native-user-defaults';
import * as types from '~/store/actionTypes';
// import { Nixplay } from '~/lib/sdk';
// import { config } from '~/config';
import { resolveCallback, rejectCallback } from './helper';
// import * as selectors from './selectors';

export function* changeAppRoot(action) {
	try {
		const { root } = action.payload;

		if (root === 'main') {
			// const token = yield select(selectors.token);
			// const profile = yield call(Nixplay.user.getProfile, token);
			const payload = { root: 'home' };
			yield call(resolveCallback, action, payload);
			yield put({ type: types.CHANGE_APP_ROOT_SUCCESS, payload });
		}
	} catch (err) {
		const { root } = action.payload;
		if (root === 'main') {
			const payload = { root: 'home' };
			yield call(resolveCallback, action, payload);
			yield put({ type: types.CHANGE_APP_ROOT_SUCCESS, payload });
		}
		yield call(rejectCallback, action, { root: 'home' });
	}
}
