/* globals popout */

this.popout = {
	context: null,
	docked: true,
	open(config = {}, fn) {
		this.close();
		this.context = Blaze.renderWithData(Template.popout, config, document.body);
		this.fn = fn;
		this.config = config;
		this.timer = null;
		if (config.timer) {
			this.timer = setTimeout(() => this.close(), config.timer);
		}
		if (config.docked != null) {
			this.docked = config.docked;
		}
	},
	close() {
		if (this.context) {
			Blaze.remove(this.context);
		}
		this.context = null;
		this.fn = null;
		if (this.timer) {
			clearTimeout(this.timer);
		}
	}
};

Template.popout.helpers({
	state() {
		return Template.instance().isMinimized.get() ? 'closed' : 'open';
	},
	style() {
		return Template.instance().isDocked.get() ? 'docked' : 'undocked';
	},
	isDocked() {
		return Template.instance().isDocked.get();
	}
});

Template.popout.onRendered(function() {
	if (this.data.onRendered) {
		this.data.onRendered();
	}
});
Template.popout.onCreated(function() {
	this.isDocked = new ReactiveVar(popout.docked);
	this.isMinimized = new ReactiveVar(false);
});
Template.popout.onDestroyed(function() {
});

Template.popout.events({
	'click .js-action'(e, instance) {
		!this.action || this.action.call(instance.data.data, e, instance);
		e.stopPropagation();
		popout.close();
	},
	'click .js-close'(e, i) {
		e.stopPropagation();
		popout.docked = true;
		const livestreamTab = document.querySelector('.flex-tab--livestream');
		const livestreamTabSource = Blaze.getView(livestreamTab).templateInstance().streamingOptions.get().url;
		const popoutSource = Blaze.getData(popout.context).data && Blaze.getData(popout.context).data.streamingSource;
		if (livestreamTab == null || livestreamTabSource !== popoutSource) {
			popout.close();
			popout.open({
				content: 'liveStreamView',
				data: {
					'streamingSource': livestreamTabSource
				}
			});
		} else {
			i.isDocked.set(true);
		}
	},
	'click .js-minimize'(e, i) {
		e.stopPropagation();
		if (i.isMinimized.get()) {
			i.isMinimized.set(false);
			document.querySelector('.rc-popout object').height = '350px';
		} else {
			i.isMinimized.set(true);
			document.querySelector('.rc-popout object').height = '40px';
		}
	},
	'click .js-dock'(e, i) {
		e.stopPropagation();
		popout.docked = !i.isDocked.get();
		i.isDocked.set(!i.isDocked.get());
	}
});

RocketChat.callbacks.add('afterLogoutCleanUp', () => popout.close(), RocketChat.callbacks.priority.MEDIUM, 'popout-close-after-logout-cleanup');

@@ -1434,30 +1434,80 @@
 stream');%0A%09%09
-const 
+let livestreamTabSource;%0A%09%09let popoutSource;%0A%09%09try %7B%0A%09%09%09
 livestreamTa
@@ -1594,22 +1594,17 @@
 .url;%0A%09%09
-const 
+%09
 popoutSo
@@ -1698,24 +1698,25 @@
 ngSource;%0A%09%09
+%09
 if (livestre
@@ -1771,24 +1771,25 @@
 ource) %7B%0A%09%09%09
+%09
 popout.close
@@ -1791,24 +1791,25 @@
 close();%0A%09%09%09
+%09
 popout.open(
@@ -1810,16 +1810,17 @@
 .open(%7B%0A
+%09
 %09%09%09%09cont
@@ -1850,16 +1850,17 @@
 %09%09%09%09
+%09
 data: %7B%0A
 %09%09%09%09
@@ -1855,16 +1855,17 @@
 data: %7B%0A
+%09
 %09%09%09%09%09'st
@@ -1908,17 +1908,20 @@
 %09%09%09%09
+%09
 %7D%0A
+%09
 %09%09%09%7D);%0A
+%09
 %09%09%7D 
@@ -1926,24 +1926,25 @@
 %7D else %7B%0A%09%09%09
+%09
 i.isDocked.s
@@ -1953,16 +1953,75 @@
 (true);%0A
+%09%09%09%7D%0A%09%09%7D catch (e) %7B%0A%09%09%09console.log(e);%0A%09%09%09popout.close();%0A
 %09%09%7D%0A%09%7D,%0A

Resolve exception when closing popout from different channel