import { parseURI } from 'lbry-redux';

const protocol = 'lbry://';
const locateURI = (value, fromIndex) => value.indexOf(protocol, fromIndex);
const locateMention = (value, fromIndex) => value.indexOf('@', fromIndex);

// Generate a valid markdown link
const createURI = (text, uri, addProtocol = true) => ({
  type: 'link',
  url: (addProtocol ? protocol : '') + uri,
  children: [{ type: 'text', value: text }],
});

// Generate a markdown link from channel name
function tokenizeMention(eat, value, silent) {
  const match = /^@(\w+)/.exec(value);

  if (match) {
    const text = match[0];
    const href = match[0];
    return silent ? true : eat(text)(createURI(text, href));
  }
}

// Generate a markdown link from lbry url
function tokenizeURI(eat, value, silent) {
  const match = /^(lbry:\/\/)+([A-z0-9-_#@])+/.exec(value);

  if (match) {
    try {
      const text = match[0];
      const uri = parseURI(text, true);
      // Create channel link
      if (uri.isChannel) {
        return eat(text)(createURI(uri.claimName, uri.claimName));
      }
      // Create uri link
      return eat(text)(createURI(text, text, false));
    } catch (err) {
      // Silent errors: console.error(err)
    }
  }
}

// Configure tokenizer for lbry urls
tokenizeURI.locator = locateURI;
tokenizeURI.notInLink = true;
tokenizeURI.notInBlock = true;

// Configure tokenizer for lbry channels
tokenizeMention.locator = locateMention;
tokenizeMention.notInLink = true;
tokenizeMention.notInBlock = true;

// Main module
export default function remarkLBRY() {
  const Parser = this.Parser;
  const tokenizers = Parser.prototype.inlineTokenizers;
  const methods = Parser.prototype.inlineMethods;

  // Add an inline tokenizer (defined in the following example).
  tokenizers.uri = tokenizeURI;
  tokenizers.mention = tokenizeMention;

  // Run it just before `text`.
  methods.splice(methods.indexOf('text'), 0, 'uri');
  methods.splice(methods.indexOf('text'), 0, 'mention');
}

@@ -279,28 +279,8 @@
  uri
-, addProtocol = true
 ) =%3E
@@ -311,21 +311,39 @@
 l: (
-addP
+uri.startsWith(p
 rotocol
+)
  ?
+ '' :
  pro
@@ -351,13 +351,8 @@
 ocol
- : ''
 ) + 
@@ -410,423 +410,40 @@
 );%0A%0A
-// Generate a markdown link from channel name%0Afunction tokenizeMention(eat, value, silent) %7B%0A  const match = /%5E@(%5Cw+)/.exec(value);%0A%0A  if (match) %7B%0A    const text = match%5B0%5D;%0A    const href = match%5B0%5D;%0A    return silent ? true : eat(text)(createURI(text, href));%0A  %7D%0A%7D%0A%0A// Generate a markdown link from lbry url%0Afunction tokenizeURI(eat, value, silent) %7B%0A  const 
+const validateURI = (
 match
+)
  =
- /%5E(lbry:%5C/%5C/)+(%5BA-z0-9-_#@%5D)+/.exec(value);%0A
+%3E %7B
 %0A  i
@@ -528,14 +528,8 @@
 text
-, true
 );%0A 
@@ -633,29 +633,20 @@
 imName, 
-uri.claimName
+text
 ));%0A    
@@ -721,15 +721,8 @@
 text
-, false
 ));%0A
@@ -797,16 +797,357 @@
 %0A  %7D%0A%7D%0A%0A
+// Generate a markdown link from channel name%0Afunction tokenizeMention(eat, value, silent) %7B%0A  const match = /%5E@(%5Cw+)/.exec(value);%0A  return validateURI(match);%0A%7D%0A%0A// Generate a markdown link from lbry url%0Afunction tokenizeURI(eat, value, silent) %7B%0A  const match = /%5E(lbry:%5C/%5C/)+(%5BA-z0-9-_#@%5D)+/.exec(value);%0A  return validateURI(match);%0A%7D%0A%0A
 // Confi

improve validation of lbry urls