import { call, put, select } from 'redux-saga/effects';
import { get, post, del } from 'services/requests';
import { Api } from 'routes/Api';
import { handleError } from 'modules/error-handler';
import * as actions from 'lib/redux/actions/comments-actions';
import { message } from 'antd';
import { showGlobalLogin } from '../actions/global-helpers-actions';

export function* postComment(action): Generator {
	const hide = message.loading('Posting new comment');
	try {
		const res: Record<string, any> = yield call(() => post({
			url: Api.comments,
			data: action.payload
		}));

		const getDataToAlter = state => state[action.dataToAlter];

		const dataToAlter: Object = yield select(getDataToAlter);

		// dataToAlter - news_item

		dataToAlter['comments'].push(res.data);
		const data = {};
		data[action.dataToAlter] = dataToAlter;

		const status = {};
		status[action.dataToAlter] = { posting: false, error: false };

		yield put(actions.postedComment({ data: { ...data, status } }));
		message.success('New comment posted');
	}
	catch (error) {
		yield put(actions.failedPostComment({ error: error.message }));
		message.error('Unable to post comment');
	}
	finally {
		hide();
	}
}


export function* deleteComment(action): Generator {
	const hide = message.loading('Deleting comment');
	try {
		yield call(() => del({
			url: Api.comments + '/' + action.payload.id
		}));

		const getDataToAlter = state => state[action.dataToAlter];

		const dataToAlter: Object = yield select(getDataToAlter);

		// dataToAlter - news_item

		dataToAlter['comments'] = dataToAlter['comments'].filter(cm => cm.id !== action.payload.id);
		const data = {};
		data[action.dataToAlter] = dataToAlter;

		const status = {};
		status[action.dataToAlter] = { posting: false, error: false };

		yield put(actions.postedComment({ data: { ...data, status } }));
		message.success('Comment deleted');
	}
	catch (error) {
		yield put(actions.failedPostComment({ error: error.message }));

		if (error?.response?.status == 401 || error?.response?.status == 403) yield showGlobalLogin();

		message.error('Unable to delete comment');
	}
	finally {
		hide();
	}
}


export function* editComment(action): Generator {
	const hide = message.loading('Edditing comment');
	try {
		const res: Record<string, any> = yield call(() => post({
			url: Api.comments + '/' + action.payload.id,
			data: action.payload
		}));

		const getDataToAlter = state => state[action.dataToAlter];

		const dataToAlter: Object = yield select(getDataToAlter);

		// dataToAlter - news_item

		dataToAlter['comments'] = dataToAlter['comments'].filter(cm => {
			if (cm.id == res.data.id) cm = res.data;
			return cm;
		});
		const data = {};
		data[action.dataToAlter] = dataToAlter;

		const status = {};
		status[action.dataToAlter] = { posting: false, error: false };

		yield put(actions.postedComment({ data: { ...data, status } }));
		message.success('Comment editted');
	}
	catch (error) {
		yield put(actions.failedPostComment({ error: error.message }));
		message.error('Unable to edit comment');
	}
	finally {
		hide();
	}
}


