'use strict';

chrome.browserAction.onClicked.addListener(function () {
	chrome.windows.getCurrent(function (win) {
		chrome.tabs.query({
			currentWindow: false,
			windowType: 'normal'
		}, function (tabs) {
			var tabIds = tabs.map(function (el) {
				return el.id;
			});

			chrome.tabs.move(tabIds, {index: -1, windowId: win.id}, function () {
				// preserve pinned tabs
				tabs.forEach(function (el) {
					if (el.pinned) {
						chrome.tabs.update(el.id, {pinned: true});
					}
				});
			});
		});
	});
});

'use strict';

function getTabIds(cb) {
	chrome.tabs.query({
		currentWindow: false,
		windowType: 'normal'
	}, function (tabs) {
		chrome.tabs.query({
			currentWindow: false,
			windowType: 'popup'
		}, function (tabs2) {
			var tabIds = tabs.concat(tabs2).map(function (x) {
				return x.id;
			});

			cb(tabIds);
		});
	});
}

chrome.browserAction.onClicked.addListener(function () {
	getTabIds(function (tabIds) {
		chrome.windows.getCurrent(function (win) {
			chrome.tabs.move(tabIds, {
				windowId: win.id,
				index: -1
			}, function () {
				// preserve pinned tabs
				tabs.forEach(function (x) {
					if (x.pinned) {
						chrome.tabs.update(x.id, {pinned: true});
					}
				});
			});
		});
	});
});

support merging `popups` too - fixes #2