import { template } from "./config";
import { IServiceConfig, sendRequest } from "./utils";

const SERVICES: { [key: string]: IServiceConfig } = {
	CREATE_FOLDER: {
		...template,
		endpoint: "/folders",
		method: "POST",
	},
	UPDATE_FOLDER: {
		...template,
		endpoint: "/folders/",
		method: "PUT",
	},
	DELETE_FOLDER: {
		...template,
		endpoint: ["/folders/", "/type/"],
		method: "DELETE",
	},
	DELETE_FOLDER_BULK: {
		...template,
		endpoint: "/folders/bulk",
		method: "DELETE",
	},
};

const createFolder = async (data: {
	type: "images" | "files";
	folderName: string;
	site: number | null;
	parentId: number | null;
}) => {
	return sendRequest(SERVICES.CREATE_FOLDER, data);
};

const updateFolder = async (folderID: number, data: { parentId: number; folderName: string }) => {
	const { host, endpoint } = SERVICES.UPDATE_FOLDER;
	SERVICES.UPDATE_FOLDER.dynamicUrl = `${host}${endpoint}${folderID}`;

	return sendRequest(SERVICES.UPDATE_FOLDER, data);
};

const deleteFolder = async (folderID: number, type: "images" | "files") => {
	const {
		host,
		endpoint: [prefix, suffix],
	} = SERVICES.DELETE_FOLDER;
	SERVICES.DELETE_FOLDER.dynamicUrl = `${host}${prefix}${folderID}${suffix}${type}`;

	return sendRequest(SERVICES.DELETE_FOLDER);
};

const deleteFolderBulk = async (ids: number[]) => {
	return sendRequest(SERVICES.DELETE_FOLDER_BULK, { ids });
};

export default {
	createFolder,
	updateFolder,
	deleteFolder,
	deleteFolderBulk,
};
