/**
 *
 * Close current window (and focus on an other window).
 *
 * <example>
    :close.js
    client
        .url('http://github.com')
        .newWindow('http://google.com')
        .getTitle().then(function(title) {
            console.log(title); // outputs: "Google"
        })
        .close()
        .getTitle().then(function(title) {
            console.log(title); // outputs: "GitHub · Build software better, together."
        });
 * </example>
 *
 * @param {String=} windowHandle new window to focus on
 *
 * @uses protocol/window, window/switchTab
 * @type window
 *
 */

import { CommandError } from '../utils/ErrorHandler'

let close = function (windowHandle) {
    /*!
     * parameter check
     */
    if (windowHandle && typeof windowHandle !== 'string') {
        throw new CommandError('number or type of arguments don\'t agree with close command')
    }

    return this.window().switchTab(windowHandle)
}

export default close

@@ -55,16 +55,100 @@
 window).
+ If no window handle is given%0A * it automatically switches back to the first handle.
 %0A *%0A * %3C
@@ -674,23 +674,23 @@
 mport %7B 
-Command
+Runtime
 Error %7D 
@@ -765,103 +765,146 @@
     
-/*!%0A     * parameter check
+if (typeof windowHandle !== 'string') %7B%0A        return this.getTabIds().then((tabIds) =%3E %7B
 %0A     
-*/%0A
     
-if (windowHandle && typeof windowHandle !== 'string'
+   if (tabIds.length === 0
 ) %7B%0A
+        
     
@@ -921,82 +921,258 @@
 new 
-CommandError('number or type of arguments don%5C't agree with close command'
+RuntimeError(%60%60 +%0A                    %60Can't switch to the next tab because all windows are closed. %60 +%0A                    %60Make sure you keep at least one window open!%60)%0A            %7D%0A%0A            return this.window().switchTab(tabIds%5B0%5D)%0A        %7D
 )%0A  

make sure to switch to the next window handle after using close