import _prefix from './tools/prefix';

const curry = require('1-liners/curry');
const property = curry(require('1-liners/property'));
const assign = require('object-assign');
const prefix = _prefix('doxie.input');
const injectError = require('tiny-error')({prefix});

export default ({input} = {}) => {
  if (typeof input === 'undefined') throw injectError(
    'Wrong parameters. Pass an `{Object} parameters` with a `{String} ' +
    'parameters.input`.'
  );

  input = String(input);

  const betweenMarkers =
    /(<!--\s*@doxie\.inject\s+start\s*-->)[^]*(<!--\s*@doxie\.inject\s+end\s*-->)/g
  ;

  return (data) => {
    let error = null;
    let output;

    if (!betweenMarkers.test(input)) {
      output = input;
      error = {error:
        prefix + 'Warning: Markers not found. Make sure you have a `<!-- ' +
        '@doxie.inject start -->` followed by a `<!-- @doxie.inject end ' +
        '-->` in your document.'
      };
    } else {
      output = input.replace(
        betweenMarkers,
        `$1${data.docs.map(property('output')).join('')}$2`
      );
    }

    return assign({}, data,
      {['doxie.inject']: {output}},
      error
    );
  };
};

@@ -278,21 +278,66 @@
 fault (%7B
+%0A  
 input
+,%0A  as = null,  /* jshint ignore: line */%0A
 %7D = %7B%7D) 
@@ -530,45 +530,152 @@
 t);%0A
-%0A  const betweenM
+  if (as !== null) as = String(as);%0A%0A  const markerName = (as !== null ?%0A    %60%5C%5Cs*$%7Bas%7D%60 :%0A    ''%0A  );%0A%0A  const m
 arker
-s
  =
+ (role) =%3E (
 %0A    
-/(
+%60
 %3C!--%5C
+%5C
 s*@d
@@ -679,24 +679,25 @@
 *@doxie%5C
+%5C
 .inject%5C
 s+start%5C
@@ -692,66 +692,135 @@
 ect%5C
+%5C
 s+
-start%5Cs*--%3E)%5B%5E%5D*(%3C!--%5Cs*@doxie%5C.inject%5Cs+end%5Cs*--%3E)/g
+$%7Brole%7D$%7BmarkerName%7D%5C%5Cs*--%3E%60%0A  );%0A%0A  const betweenMarkers = new RegExp(%0A    %60($%7Bmarker('start')%7D)%5B%5E%5D*($%7Bmarker('end')%7D)%60
 %0A  
+)
 ;%0A%0A 

Implement named markers