import I18n from '../../../i18n';
import Navigation from '../../navigation/appNavigation';
import { closeLivechat as closeLivechatService } from '../../services/restApi';
import { showErrorAlert } from './info';
import log from './log';

export const closeLivechat = async ({
	rid,
	comment,
	isMasterDetail,
	tags
}: {
	rid: string;
	isMasterDetail: boolean;
	comment?: string;
	tags?: string[];
}) => {
	try {
		await closeLivechatService(rid, comment, tags);
		if (isMasterDetail) {
			Navigation.navigate('DrawerNavigator');
		} else {
			Navigation.navigate('RoomsListView');
		}
	} catch (e: any) {
		showErrorAlert(I18n.isTranslated(e.error) ? I18n.t(e.error) : e.reason, I18n.t('Oops'));
		log(e);
	}
};
