import _ from 'lodash';

// "#chan,#chan2" => 2 channels without a key
// "#chan,#chan2 key" => 2 channels, the first having a key
// "#chan,#chan2 key1,key2" => 2 channels, both having a key
// "#chan,#chan2 ,key2" => 2 channels, the second having a key
export function extractBuffers(str) {
    let spaceIdx = str.indexOf(' ');
    if (spaceIdx === -1) spaceIdx = str.length;

    let bufferNames = str.substr(0, spaceIdx).split(',');
    let keys = str.substr(spaceIdx + 1).split(',');

    let buffers = [];
    bufferNames.forEach((bufferName, idx) => {
        buffers.push({
            name: bufferName,
            key: keys[idx] || '',
        });
    });

    return buffers;
}

export function splitHost(uri) {

}

// Does a string mention a nickname
export function mentionsNick(input, nick) {
    let punc = ',.!:;-+)]?¿\\/<>@';

    let idx = input.toLowerCase().indexOf(nick.toLowerCase());
    if (idx === -1) {
        return false;
    }

    let startIdx = input.lastIndexOf(' ', idx);
    if (startIdx === -1) {
        startIdx = 0;
    } else {
        startIdx++;
    }

    let endIdx = input.indexOf(' ', idx);
    if (endIdx === -1) {
        endIdx = input.length;
    }

    let segment = input.substring(startIdx, endIdx);
    let potentialNick = _.trim(segment, punc);

    return potentialNick.toLowerCase() === nick.toLowerCase();
}


// Get a users prefix symbol on a buffer from its modes
export function userModePrefix(user, buffer) {
    // The user may not be on the buffer
    if (!user.buffers[buffer.id]) {
        return '';
    }

    let modes = user.buffers[buffer.id].modes;
    if (modes.length === 0) {
        return '';
    }

    let network = buffer.getNetwork();
    let netPrefixes = network.ircClient.network.options.PREFIX;

    // find the highest ranking user mode
    // some users may have been given voice and op
    for (let p in netPrefixes) {
        if (netPrefixes[p]) {
            for (let m in modes) {
                if (modes[m] && netPrefixes[p].mode === modes[m]) {
                    return netPrefixes[p].symbol;
                }
            }
        }
    }

    return '';
}


// Get a users mode on a buffer
export function userMode(user, buffer) {
    // The user may not be on the buffer
    if (!user.buffers[buffer.id]) {
        return '';
    }

    let modes = user.buffers[buffer.id].modes;
    if (modes.length === 0) {
        return '';
    }

    let network = buffer.getNetwork();
    let netPrefixes = network.ircClient.network.options.PREFIX;

    // find the highest ranking user mode
    // some users may have been given voice and op
    for (let p in netPrefixes) {
        if (netPrefixes[p]) {
            for (let m in modes) {
                if (modes[m] && netPrefixes[p].mode === modes[m]) {
                    return modes[m];
                }
            }
        }
    }

    return '';
}


// Get a query string value from the current URL
export function queryStringVal(_name, _url) {
    let url = _url || window.location.href;
    let name = _.escapeRegExp(_name);
    let regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)');
    let results = regex.exec(url);

    if (!results) {
        return null;
    }
    if (!results[2]) {
        return '';
    }

    return decodeURIComponent(results[2].replace(/\+/g, ' '));
}


// Convert a known error code to a human readable message
export function networkErrorMessage(err) {
    let errs = {
        err_unknown_host: 'Unknown domain name or host',
        err_forbidden: 'Forbidden to connect',
        err_timeout: 'Took too long to connect',
        err_refused: 'The server refused the connection',
        err_tls: 'Could not connect securely',
        err_proxy: 'The Kiwi IRC server had an error',
    };

    return errs[err] || 'Unknown error';
}

@@ -1773,33 +1773,32 @@
 PREFIX;%0A
-%0A
     // 
-f
+F
 ind the 
 highest 
@@ -1793,360 +1793,183 @@
 the 
-highest ranking user mode%0A    // some users may have been given voice and op%0A    for (let p in netPrefixes) %7B%0A        if (netPrefixes%5Bp%5D) %7B%0A            for (let m in modes) %7B%0A                if (modes%5Bm%5D && netPrefixes%5Bp%5D.mode === modes%5Bm%5D) %7B%0A                    return netPrefixes%5Bp%5D.symbol;%0A                %7D%0A            %7D%0A        %7D%0A    %7D%0A%0A    return
+first (highest) netPrefix in the users buffer modes%0A    let prefix = _.find(netPrefixes, p =%3E modes.indexOf(p.mode) %3E -1);%0A%0A    return prefix ?%0A        prefix.symbol :%0A       
  '';
@@ -2359,25 +2359,24 @@
 IX;%0A
-%0A
     // 
-f
+F
 ind the 
 high
@@ -2375,347 +2375,181 @@
 the 
-highest ranking user mode%0A    // some users may have been given voice and op%0A    for (let p in netPrefixes) %7B%0A        if (netPrefixes%5Bp%5D) %7B%0A            for (let m in modes) %7B%0A                if (modes%5Bm%5D && netPrefixes%5Bp%5D.mode === modes%5Bm%5D) %7B%0A                    return modes%5Bm%5D;%0A                %7D%0A            %7D%0A        %7D%0A    %7D%0A%0A    return
+first (highest) netPrefix in the users buffer modes%0A    let prefix = _.find(netPrefixes, p =%3E modes.indexOf(p.mode) %3E -1);%0A%0A    return prefix ?%0A        prefix.mode :%0A       
  '';

add prawnsalad's changes