text: 'use strict'
  
  import Vue from 'vue'
  
  import iziToast from 'izitoast'
  import 'izitoast/dist/css/iziToast.css'
  
  import { displaySpeed } from './display.js'
  import i18n from '../i18n'
  import store from '../store'
  import { extractFileName } from '../utils/path.js'
  
  const defaults = {
  	layout: 2,
  	transitionIn: 'fadeInLeft',
  	transitionOut: 'fadeOutRight'
  }
  
  let openNotifications = []
  
  export function makeNotification(type, title, message, timeout) {
  	if (timeout === undefined) {
  		timeout = (type === 'error' && store.state.settings.notifications.errorsPersistent) ? 0 : store.state.settings.notifications.timeout;
  	}
  
  	// If there is already an equal notification, reset its time and don't display a new one
  	const equalNotification = openNotifications.find(item => item.type === type && item.title == title && item.message === message);
  	if (equalNotification) {
  		if (timeout > 0) {
  			equalNotification.resetTimeout();
  		}
  		return equalNotification;
  	}
  
  	// Prepare and show new toast
  	const item = {}, options = Object.assign({
  		class: 'new-toast',
  		title: title ? title.replace(/\n/g, '<br>') : '',
  		message: message ? message.replace(/\n/g, '<br>') : '',
  		onClosed() {
  			openNotifications = openNotifications.filter(notification => notification !== item);
  		},
  		timeout
  	}, defaults);
  
  	switch (type) {
  		case 'info':
  			iziToast.info(options);
  			break;
  		case 'success':
  			iziToast.success(options);
  			break;
  		case 'warning':
  			iziToast.warning(options);
  			break;
  		case 'error':
  			iziToast.error(options);
  			break;
  	}
  
  	// Get the new toast instance (iziToast does not return it)
  	const toast = document.querySelector('.new-toast');
  	toast.classList.remove('new-toast');
  
  	// Fix layout of the created toast (by default layout 2 toasts are too wide)
  	if (message) {
  		const messageElement = toast.querySelector('p.iziToast-message');
  		messageElement.style.width = 'auto';
  		messageElement.parentNode.insertBefore(document.createElement('br'), messageElement);
  	}
  
  	item.type = type;
  	item.title = title;
  	item.message = message;
  	item.timeout = timeout;
  	item.domElement = toast;
  	item.hide = function() {
  		if (openNotifications.indexOf(item) !== -1) {
  			iziToast.hide({}, toast);
  			openNotifications = openNotifications.filter(notification => notification !== item);
  		}
  	};
  	item.resetTimeout = function() {
  		if (openNotifications.indexOf(item) !== -1) {
  			iziToast.progress(options, toast).reset();
  			setTimeout(iziToast.progress(options, toast).start, 100);
  		}
  	};
  
  	openNotifications.push(item);
  	return item;
  }
  
  export function makeFileTransferNotification(type, destination, cancellationToken) {
  	const filename = extractFileName(destination);
  
  	// Prepare toast
  	iziToast.info({
  		class: 'file-transfer',
  		title: i18n.t(`notification.${type}.title`, [filename, displaySpeed(0), 0]),
  		message: i18n.t(`notification.${type}.message`),
  		layout: 2,
  		timeout: false,
  		onClosing: () => cancellationToken.cancel()
  	});
  
  	// Get it and fix up the layout
  	const toast = document.querySelector('.file-transfer');
  	toast.classList.remove('file-transfer');
  
  	const messageElement = toast.querySelector('p.iziToast-message');
  	messageElement.style.width = 'auto';
  	messageElement.parentNode.insertBefore(document.createElement('br'), messageElement);
  
  	// Get progress bar
  	const title = toast.querySelector('strong.iziToast-title');
  	const progressBar = toast.querySelector('div.iziToast-progressbar > div');
  	progressBar.style.width = '0%';
  
  	// Return object with enough info about it
  	return {
  		domElement: toast,
  		onProgress(loaded, total, speed) {
  			if (total > 0) {
  				const progress = (loaded / total) * 100;
  				title.textContent = i18n.t(`notification.${type}.title`, [filename, displaySpeed(speed), Math.round(progress)]);
  				progressBar.style.width = progress.toFixed(1) + '%';
  			}
  		},
  		hide() {
  			iziToast.hide({}, toast);
  		}
  	}
  }
  
  export function showMessage(message) {
  	const options = Object.assign({
  		class: 'display-message',
  		title: i18n.t('notification.message'),
  		message: message.replace(/\n/g, '<br>'),
  		timeout: false
  	}, defaults);
  
  	const toastContent = document.querySelector('.display-message p');
  	if (toastContent) {
  		toastContent.textContent = message;
  	} else {
  		iziToast.info(options);
  	}
  }
  
  // Register extensions
  Vue.prototype.$makeNotification = makeNotification
  Vue.prototype.$makeFileTransferNotification = makeFileTransferNotification
  Vue.prototype.$showMessage = showMessage
  