import {message} from "antd";
import {BizError} from "@/service/types.ts";

export function showToast(content: string, type?: 'success' | 'info' | 'warning' | 'error', duration?: number) {

	message.open({
		type,
		content,
		duration,
		className: 'aui-toast'
	}).then();
}

export function showErrorToast(e: Error | BizError) {
	showToast(String(((e instanceof BizError) ? e.data : '') || e.message), 'error')
}


export function showLoading(content = 'Loading...') {
	const key = 'globalLoading_' + (new Date().getTime());
	message.open({
		key,
		type: 'loading',
		content,
	}).then();
	return {
		update(content: string, type?: 'success' | 'info' | 'warning' | 'error') {
			message.open({
				key,
				content,
				type
			}).then();
		},
		close() {
			message.destroy(key);
		}
	}
}