import type { AxiosResponse } from "axios";

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

export interface IShareData {
	entity: string;
	daysValid: number;
	startDate: string;
	endDate: string;
	visitCount: number;
}

const SERVICES: { [key: string]: IServiceConfig } = {
	CREATE_SHARE: {
		...template,
		endpoint: ["/page/", "/share"],
		method: "POST",
	},
	GET_SHARE: {
		...template,
		endpoint: ["/page/", "/share"],
		method: "GET",
	},
	RENEW_SHARE: {
		...template,
		endpoint: ["/page/", "/share"],
		method: "PUT",
	},
};

const createShare = async (pageID: number): Promise<AxiosResponse> => {
	const {
		host,
		endpoint: [prefix, suffix],
	} = SERVICES.CREATE_SHARE;
	SERVICES.CREATE_SHARE.dynamicUrl = `${host}${prefix}${pageID}${suffix}`;

	return sendRequest(SERVICES.CREATE_SHARE);
};

const getShare = async (pageID: number): Promise<AxiosResponse> => {
	const {
		host,
		endpoint: [prefix, suffix],
	} = SERVICES.GET_SHARE;
	SERVICES.GET_SHARE.dynamicUrl = `${host}${prefix}${pageID}${suffix}`;

	return sendRequest(SERVICES.GET_SHARE);
};

const renewShare = async (pageID: number): Promise<AxiosResponse> => {
	const {
		host,
		endpoint: [prefix, suffix],
	} = SERVICES.RENEW_SHARE;
	SERVICES.RENEW_SHARE.dynamicUrl = `${host}${prefix}${pageID}${suffix}`;

	return sendRequest(SERVICES.RENEW_SHARE);
};

export default { createShare, getShare, renewShare };
