var toastTargetUrl = "";

function toast(string, url) {
	toastTargetUrl = url;
	
	const toast = document.getElementById("notify_toast");
	const text = toast.getElementsByTagName('p')[0];

	const tagHeight = toast.clientHeight;
    const viewportHeight = window.innerHeight;
    const scrollY = window.scrollY;
    
    if (scrollY >= viewportHeight - tagHeight) {
        const bottomPosition = viewportHeight - scrollY;
		toast.style.top = "unset";
        toast.style.bottom = 20 + "px";
    } else {
		toast.style.bottom = "unset";
        toast.style.top = (viewportHeight - tagHeight + scrollY - 160) + "px";
    }

	toast.classList.contains("reveal") ?
		(clearTimeout(removeToast), removeToast = setTimeout(function() {
			toast.classList.remove("reveal")
		}, 5000)) :
		removeToast = setTimeout(function() {
			toast.classList.remove("reveal")
		}, 5000)
	toast.classList.add("reveal"),
		text.innerText = string
}

function toastConfirm() {
	if(toastTargetUrl != "") {
		location.href=toastTargetUrl;
	}
	const toast = document.getElementById("notify_toast");
	toast.classList.remove("reveal")
}

// 메시지 띄우기
function showMessage() {
	var urlString = window.location.href;
	var url = new URL(urlString);
	var queryParams = url.searchParams;
	var paramValue = queryParams.get("message");

	if (paramValue != null) {
		const encodedDataFromServer = paramValue;
		const decodedData = decodeURIComponent(encodedDataFromServer);
		toast(decodedData, '');

		var updatedUrl = removeParamFromUrl(window.location.href, "message");
		window.history.pushState(null, null, updatedUrl);
	}
}

// URL에서 파라미터 제거하는 함수 정의
function removeParamFromUrl(url, paramName) {
	var urlParts = url.split('?');
	if (urlParts.length >= 2) {
		var params = urlParts[1].split('&');
		for (var i = params.length - 1; i >= 0; i--) {
			var param = params[i].split('=')[0];
			if (param === paramName) {
				params.splice(i, 1);
			}
		}
		if (params.length > 0) {
			return urlParts[0] + '?' + params.join('&');
		} else {
			return urlParts[0];
		}
	}
	return url;
}

// 모아보기 연결 링크설정
function fn_notifyViewPopup(url, type){
	window.open(url);
}