'use strict';

require([
	'wrapper/chrome',
	'storage/chrome-storage',
	'vendor/showdown.min'
], function(chrome, ChromeStorage, showdown) {
	$('#opt-in').click(() => {
		update(false);
	});

	$('#opt-out').click(() => {
		update(true);
	});

	let update = (value) => {
		const options = ChromeStorage.getStorage(ChromeStorage.OPTIONS);

		options.get().then((data) => {
			data.disableGa = value;
			options.set(data);
		});

		window.close();
		$('.controls').hide();
		$('.finished').show();
	};

	const locale = window.navigator.language.split('-')[0];
	const defaultPrivacyDoc = 'PRIVACY.md';

	let privacyDoc = defaultPrivacyDoc;
	if (locale !== 'en') {
		privacyDoc = `PRIVACY.${locale}.md`;
	}

	fetch(chrome.runtime.getURL(privacyDoc))
		.then((response) => {
			if (!response.ok) {
				throw new Error(response.statusText);
			}
			response.text()
				.then((text) => {
					let converter = new showdown.Converter();
					let content = converter.makeHtml(text);
					$('.privacy-policy').html(content);
				});
		}).catch(() => {
			console.log(`Failed to load ${privacyDoc}`);

			fetch(chrome.runtime.getURL(defaultPrivacyDoc))
				.then((response) => {
					response.text()
						.then((text) => {
							let converter = new showdown.Converter();
							let content = converter.makeHtml(text);
							$('.privacy-policy').html(content);
						});
				});
		});
});

@@ -493,16 +493,53 @@
 );%0A%09%7D;%0A%0A
+%09const loadPrivacy =  async () =%3E %7B%0A%09
 %09const l
@@ -587,16 +587,17 @@
 -')%5B0%5D;%0A
+%09
 %09const d
@@ -630,16 +630,17 @@
 Y.md';%0A%0A
+%09
 %09let pri
@@ -646,19 +646,21 @@
 ivacyDoc
+s
  = 
+%5B
 defaultP
@@ -668,18 +668,21 @@
 ivacyDoc
-;%0A
+%5D;%0A%0A%09
 %09if (loc
@@ -699,16 +699,21 @@
 n') %7B%0A%09%09
+%09let 
 privacyD
@@ -746,345 +746,85 @@
 %60;%0A%09
-%7D%0A%0A%09fetch(chrome.runtime.getURL(privacyDoc))%0A%09%09.then((response) =%3E %7B%0A%09%09%09if (!response.ok) %7B%0A%09%09%09%09throw new Error(response.statusText
+%09%09privacyDocs.unshift(privacyDoc
 );%0A
-%09
 %09%09%7D%0A
-%09%09%09response.text()%0A%09%09%09%09.then((text) =%3E %7B%0A%09%09%09%09%09let converter = new showdown.Converter();%0A%09%09%09%09%09let content = converter.makeHtml(text);%0A%09%09%09%09%09$('.privacy-policy').html(content);%0A%09%09%09%09%7D);%0A%09%09%7D).catch(() =%3E
+%0A%09%09for (let privacyDoc of privacyDocs)
  %7B%0A%09
@@ -842,22 +842,16 @@
 og(%60
-Failed to load
+fetching
  $%7Bp
@@ -864,20 +864,52 @@
 Doc%7D%60);%0A
-%0A
 %09%09%09
+try %7B%0A%09%09%09%09const response = await 
 fetch(ch
@@ -928,24 +928,17 @@
 .getURL(
-defaultP
+p
 rivacyDo
@@ -944,83 +944,54 @@
 oc))
+;
 %0A%09%09%09%09
-.then((response) =%3E %7B%0A%09%09%09%09%09response.text()%0A%09%09%09%09%09%09.then((text) =%3E %7B%0A%09%09%09
+const markdown = await response.text();%0A
 %09%09%09%09
@@ -1036,19 +1036,16 @@
 ();%0A%09%09%09%09
-%09%09%09
 let cont
@@ -1073,18 +1073,19 @@
 tml(
-text);%0A%09%09%09
+markdown);%0A
 %09%09%09%09
@@ -1128,25 +1128,124 @@
 %09%09%09%09
-%09%09%7D);%0A%09%09%09%09%7D);%0A%09%09%7D
+break;%0A%09%09%09%7D catch (err) %7B%0A%09%09%09%09console.log(%60Failed to load privacy $%7Berr%7D%60);%0A%09%09%09%09continue;%0A%09%09%09%7D%0A%09%09%7D%0A%09%7D%0A%0A%09loadPrivacy(
 );%0A%7D

use async for privacy