import { call } from 'redux-saga/effects';
import { ToasterService } from 'nixplay-ui-kit';
import _ from 'lodash';
import i18n from '~/locale/i18n';

export function* toast({ payload }) {
	yield call(ToasterService.toast, payload);
}

export function* toastError({ payload }) {
	let text = typeof payload.text.toString === 'function' ? payload.text.toString() : payload.text;
	if (_.isEqual(typeof payload.text, 'Error')) {
		text = payload.text.message;
	} else if (_.includes(text, 'Network request failed')) {
		text = i18n.t('common.noInternetConnection');
	} else if (_.includes(text, 'Error: ')) {
		text = _.replace(text, 'Error: ', '');
	}
	yield call(ToasterService.toast, {
		...payload,
		theme: 'error',
		text,
	});
}
