import _ from "lodash";
import { getUrl } from "../helper";
import { authedFunction } from "../session";

export const reactionActions = {
	add: (entityId, entityType, reaction, site) => {
		return authedFunction({
			method: "POST",
			url: getUrl("reactions", "add"),
			data: {
				entityId,
				entityType,
				reaction,
				site,
			},
		});
	},
	remove: (entityId, entityType, reaction) => {
		return authedFunction({
			method: "POST",
			url: getUrl("reactions", "remove"),
			data: {
				entityId,
				entityType,
				reaction,
			},
		});
	},
	addComment: (
		entityId,
		entityType,
		entityName,
		site,
		comment,
		image,
		parentId,
	) => {
		const data = {
			entityId,
			entityType,
			entityName,
			site,
			comment,
			parentId,
		};
		if (!_.isEmpty(image)) data.image = image;
		return authedFunction({
			method: "POST",
			url: getUrl("reactions", "comments/add"),
			data,
		});
	},
	removeComment: (id) => {
		return authedFunction({
			method: "POST",
			url: getUrl("reactions", "comments/remove"),
			data: {
				id,
			},
		});
	},
	reportComment: (id) => {
		return authedFunction({
			method: "POST",
			url: getUrl("reactions", "comments/report"),
			data: {
				id,
			},
		});
	},
	getComments: (entityId, entityType, minTime) => {
		const query = {
			entityId,
			entityType,
		};
		if (minTime) {
			query.minTime = minTime;
		}
		return authedFunction({
			method: "GET",
			url: getUrl("reactions", "comments/get", query),
		});
	},
};
