import NetInfo from '@react-native-community/netinfo';
import PropTypes from 'prop-types';
import { ToasterService } from 'nixplay-ui-kit';
import i18n from '~/locale/i18n';

export const STATE_NO_NETWORK = 'STATE_NO_NETWORK';

export const defaultNetworkType = ['wifi', 'cellular'];

export const isNetworkAvailable = (props) => (
	NetInfo.getConnectionInfo().then((connectionInfo) => {
		if (props.allowNetworkTypes.indexOf(connectionInfo.type) < 0) {
			if (props.withToaster) {
				ToasterService.toast({
					theme: 'error',
					text: i18n.t('common.noInternetConnection'),
				});
			}
			const error = new Error(STATE_NO_NETWORK);
			return Promise.reject(error);
		}
		props.callback();
		return Promise.resolve(connectionInfo);
	})
);

isNetworkAvailable.propTypes = {
	allowNetworkTypes: PropTypes.array.isRequired,
	callback: PropTypes.func.isRequired,
	withToaster: PropTypes.bool,
};

isNetworkAvailable.defaultProps = {
	withToaster: true,
};

// export { isNetworkAvailable, defaultNetworkType };

