import { ref, h } from 'vue';
import { toast } from 'vue-sonner';

export const notifications = ref([]);

export const hideNotification = id => {
	notifications.value = notifications.value.filter(props => props.id !== id);
};

export const notify = props => {
	// TODO: remove the line below once the jingrow-ui bug (onError triggers twice) is fixed
	if (notifications.value.some(n => n.message === props.message)) return;
	props.id = Math.floor(Math.random() * 1000 + Date.now());
	notifications.value.push(props);
	setTimeout(() => hideNotification(props.id), props.timeout || 5000);
};

export function getToastErrorMessage(error, fallbackMessage = 'An error occurred') {
	if (!error) return fallbackMessage;
	
	try {
		const errorMessage = error.messages?.length
			? error.messages.join('<br>')
			: (error.message || fallbackMessage);
		
		return h('div', {
			innerHTML: errorMessage,
		});
	} catch (e) {
		return fallbackMessage;
	}
}
