import { notify } from '@/utils/toast';

export function loginAsAdmin(siteName) {
	return {
		url: 'jcloud.api.site.login',
		params: { name: siteName },
		onSuccess(data) {
			// Use the URL returned by backend which includes correct route
			if (data?.url) {
				window.open(data.url, '_blank');
			}
		},
		validate() {
			// hack to display the toast
			notify({
				title: 'Attempting to login as Administrator',
				message: `Please wait...`,
				icon: 'alert-circle',
				color: 'yellow'
			});
		},
		onError(err) {
			notify({
				title: 'Could not login as Administrator',
				message: err.messages.join('\n'),
				color: 'red',
				icon: 'x'
			});
		}
	};
}
