const htmlToText = require('html-to-text');
const inlineCss = require('inline-css');
const overwrite = require('overwrite').default;

// Really hacky way of exposing Caja HTML rules
// Line 1098 as of sanitizer@0.1.3
const sanitizer = overwrite('sanitizer', {
	'sanitizer.js': contents => {
		const lines = contents.split('\n');
		lines.splice(1098, 0, 'Sanitizer.html4 = html4;');
		return lines.join('\n');
	}
});

// Allow inline style attributes
sanitizer.html4.ATTRIBS['*::style'] = 0;

function sanitize(rawText, rawHtml, subject, callback) {
	let text = (rawText || '').trim();

	// Generate plain text message if needed
	if (!rawText && rawHtml) {
		text = htmlToText.fromString(rawHtml, {
			wordwrap: 120
		});
	}

	if (!rawHtml) {
		rawHtml = text.replace(/(?:\r\n|\r|\n)/g, '<br />');
	}

	// Inline CSS <style> tags
	inlineCss(rawHtml, { url: 'file://' }).then(inlineHtml => {
		// Sanitize the HTML message with Caja
		let html = sanitizer.sanitize(inlineHtml, url => {
			if (/^https?/.test(url.getScheme())) return url.toString();
			if (/^mailto?/.test(url.getScheme())) return url.toString();
			if (url.getScheme() == 'data' && /^image/.test(url.getPath())) return url.toString();
		}, tokens => tokens).trim();

		// Construct valid HTML document
		html = `<!doctype html>
<html>
	<head>
		<meta name="viewport" content="width=device-width">
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<title>${subject}</title>
		<base target="_blank">
		<style>
			body {
				margin: 10px 15px;
				padding: 0;
				font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Helvetica Neue", Arial, sans-serif;
			}
		</style>
	</head>
	<body>
		${html}
	</body>
</html>`;

		callback(text, html);
	});
}

module.exports = sanitize;

@@ -1605,16 +1605,14 @@
 I%22, 
-%22
 Roboto
-%22
 , %22H
@@ -1638,18 +1638,109 @@
 al, 
-sans-serif
+%22Noto Sans%22, sans-serif, %22Apple Color Emoji%22, %22Segoe UI Emoji%22, %22Segoe UI Symbol%22, %22Noto Color Emoji%22
 ;%0A%09%09

Update fonts