'use strict';

const http = require('http');
const https = require('https');

const defaults = require('./defaults');
const externalRequest = require('./external-request');

// callback(err, protocol)
module.exports.Protocol = promisesWrapper(function (options, callback) {
    // if the local protocol is requested
    if (options.local) {
        const localDescriptor = require('./protocol.json');
        callback(null, localDescriptor);
        return;
    }
    // try to fecth the browser version information and the protocol (remotely)
    module.exports.Version(options, function (err, info) {
        if (err) {
            callback(err);
            return;
        }
        // fetch the reported browser info (Node.js returns an array)
        const browser = (info[0] || info).Browser;
        // use the proper protocol fetcher
        if (!browser.match(/^(Headless)?Chrome\//) &&
            !browser.match(/^node.js\//)) {
            callback(new Error('Unknown implementation'));
            return;
        }
        fetchFromHttpEndpoint(options, info, function (err, descriptor) {
            if (err) {
                callback(err);
                return;
            }
            // use the remotely fetched descriptor
            callback(null, descriptor);
        });
    });
});

module.exports.List = promisesWrapper(function (options, callback) {
    options.path = '/json/list';
    devToolsInterface(options, function (err, tabs) {
        if (err) {
            callback(err);
        } else {
            callback(null, JSON.parse(tabs));
        }
    });
});

module.exports.New = promisesWrapper(function (options, callback) {
    options.path = '/json/new';
    if (Object.prototype.hasOwnProperty.call(options, 'url')) {
        options.path += '?' + options.url;
    }
    devToolsInterface(options, function (err, tab) {
        if (err) {
            callback(err);
        } else {
            callback(null, JSON.parse(tab));
        }
    });
});

module.exports.Activate = promisesWrapper(function (options, callback) {
    options.path = '/json/activate/' + options.id;
    devToolsInterface(options, function (err) {
        if (err) {
            callback(err);
        } else {
            callback(null);
        }
    });
});

module.exports.Close = promisesWrapper(function (options, callback) {
    options.path = '/json/close/' + options.id;
    devToolsInterface(options, function (err) {
        if (err) {
            callback(err);
        } else {
            callback(null);
        }
    });
});

module.exports.Version = promisesWrapper(function (options, callback) {
    options.path = '/json/version';
    devToolsInterface(options, function (err, versionInfo) {
        if (err) {
            callback(err);
        } else {
            callback(null, JSON.parse(versionInfo));
        }
    });
});

// options.path must be specified; callback(err, data)
function devToolsInterface(options, callback) {
    options.host = options.host || defaults.HOST;
    options.port = options.port || defaults.PORT;
    options.secure = !!(options.secure);
    externalRequest(options.secure ? https : http, options, callback);
}

// wrapper that allows to return a promise if the callback is omitted, it works
// for DevTools methods
function promisesWrapper(func) {
    return function (options, callback) {
        // options is an optional argument
        if (typeof options === 'function') {
            callback = options;
            options = undefined;
        }
        options = options || {};
        // just call the function otherwise wrap a promise around its execution
        if (typeof callback === 'function') {
            func(options, callback);
        } else {
            return new Promise(function (fulfill, reject) {
                func(options, function (err, result) {
                    if (err) {
                        reject(err);
                    } else {
                        fulfill(result);
                    }
                });
            });
        }
    };
}

// callback(err, descriptor)
function fetchFromHttpEndpoint(options, info, callback) {
    options.path = '/json/protocol';
    devToolsInterface(options, function (err, descriptor) {
        if (err) {
            callback(err);
        } else {
            callback(null, JSON.parse(descriptor));
        }
    });
}

@@ -485,44 +485,8 @@
 the 
-browser version information and the 
 prot
@@ -494,17 +494,16 @@
 col 
-(
 remotely
 )%0A  
@@ -502,525 +502,67 @@
 tely
-)
 %0A    
-module.exports.Version(options, function (err, info) %7B%0A        if (err) %7B%0A            callback(err);%0A            return;%0A        %7D%0A        // fetch the reported browser info (Node.js returns an array)%0A        const browser = (info%5B0%5D %7C%7C info).Browser;%0A        // use the proper protocol fetcher%0A        if (!browser.match(/%5E(Headless)?Chrome%5C//) &&%0A            !browser.match(/%5Enode.js%5C//)) %7B%0A            callback(new Error('Unknown implementation'));%0A            return;%0A        %7D%0A        fetchFromHttpEndpoint
+options.path = '/json/protocol';%0A    devToolsInterface
 (opt
@@ -566,22 +566,16 @@
 options,
- info,
  functio
@@ -596,36 +596,32 @@
 ptor) %7B%0A        
- 
-   
 if (err) %7B%0A     
@@ -607,36 +607,32 @@
      if (err) %7B%0A
-    
             call
@@ -654,88 +654,16 @@
     
-        return;%0A            %7D%0A            // use the remotely fetched descriptor
+%7D else %7B
 %0A   
@@ -686,16 +686,27 @@
 k(null, 
+JSON.parse(
 descript
@@ -700,32 +700,33 @@
 arse(descriptor)
+)
 ;%0A        %7D);%0A  
@@ -716,26 +716,24 @@
 );%0A        %7D
-);
 %0A    %7D);%0A%7D);
@@ -3500,324 +3500,4 @@
 ;%0A%7D%0A
-%0A// callback(err, descriptor)%0Afunction fetchFromHttpEndpoint(options, info, callback) %7B%0A    options.path = '/json/protocol';%0A    devToolsInterface(options, function (err, descriptor) %7B%0A        if (err) %7B%0A            callback(err);%0A        %7D else %7B%0A            callback(null, JSON.parse(descriptor));%0A        %7D%0A    %7D);%0A%7D%0A

Remove version check