// LICENSE : MIT
"use strict";
// This RegExp are based of https://github.com/recruit-tech/redpen/blob/master/redpen-core/src/main/java/cc/redpen/validator/sentence/JapaneseStyleValidator.java
const DEARU_PATTERN = /のだが|ないかと|してきた|であるから/g;
const DEARU_END_PATTERN = /(?:だ|である|った|ではない｜ないか|しろ|しなさい|いただきたい|いただく|ならない|あろう|られる)。$/g;

const DESUMASU_PATTERN = /でしたが|でしたので|ですので|ですが/g;
const DESUMASU_END_PATTERN = /(?:です|ます|ました|ません|ですね|でしょうか|ください|ませ)。$/g;
/**
 *
 * @param text
 * @param reg
 * @returns {{value:string, columnIndex: number, lineNumber:number}}[]
 */
function countMatchContent(text, reg) {
    let matches = [];
    let tmpMatch;
    while ((tmpMatch = reg.exec(text)) != null) {
        matches.push({
            value: tmpMatch[0],
            lineNumber: 1,
            columnIndex: reg.lastIndex - tmpMatch[0].length
        });
    }
    return matches;
}
/**
 *
 * @param text
 * @param reg
 * @returns {{value:string, columnIndex: number, lineNumber:number}}[]
 */
function countMatchContentEnd(text, reg) {
    let lines = text.split(/\r\n|\r|\n|\u2028|\u2029/g);
    let matches = [];
    lines.forEach((line, index) => {
        let ret = countMatchContent(line, reg);
        // adjust line number
        ret.forEach(match => match.lineNumber += index);
        matches = matches.concat(ret);
    });
    return matches;
}
/**
 * `text` の敬体(ですます調)について解析します
 * @param {string} text
 * @param {object} options
 * @param {boolean} options.analyzeConjunction 接続詞を解析するかどうか default: true
 * @returns {{value:string, columnIndex: number, lineNumber:number}}[]
 */

export function analyzeDesumasu(text, options = {analyzeConjunction: true}) {
    let {analyzeConjunction} = options;
    if (!analyzeConjunction) {
        return countMatchContentEnd(text, DESUMASU_END_PATTERN);
    }
    let retDesumasu = countMatchContent(text, DESUMASU_PATTERN);
    let retDesumasuEnd = countMatchContentEnd(text, DESUMASU_END_PATTERN);
    return retDesumasu.concat(retDesumasuEnd)
}
/**
 * `text` の常体(である調)について解析します
 * @param {string} text
 * @param {object} options
 * @param {boolean} options.analyzeConjunction 接続詞を解析するかどうか default: true
 * @returns {{value:string, columnIndex: number, lineNumber:number}}[]
 */
export function analyzeDearu(text, options = {analyzeConjunction: true}) {
    let {analyzeConjunction} = options;
    if (!analyzeConjunction) {
        return countMatchContentEnd(text, DEARU_END_PATTERN);
    }
    let retDearu = countMatchContent(text, DEARU_PATTERN);
    let retDearuEnd = countMatchContentEnd(text, DEARU_END_PATTERN);
    return retDearu.concat(retDearuEnd)
}
@@ -260,18 +260,16 @@
 ERN = /(
-?:
 %E3%81%A0%7C%E3%81%A7%E3%81%82%E3%82%8B%7C%E3%81%A3%E3%81%9F
@@ -312,19 +312,19 @@
 %E3%82%8D%E3%81%86%7C%E3%82%89%E3%82%8C%E3%82%8B)%E3%80%82
+?
 $/
-g
 ;%0A%0Aconst
@@ -402,10 +402,8 @@
 = /(
-?:
 %E3%81%A7%E3%81%99%7C%E3%81%BE
@@ -435,11 +435,11 @@
 %E3%81%BE%E3%81%9B)%E3%80%82
+?
 $/
-g
 ;%0A/*
@@ -1141,113 +1141,183 @@
     
-let ret = countMatchContent(line, reg);%0A        // adjust line number%0A        ret.forEach(match =%3E match.
+var match = line.match(reg);%0A        if (!match) %7B%0A            return;%0A        %7D%0A        // adjust line number%0A        matches.push(%7B%0A            value: match%5B0%5D,%0A            
 line
@@ -1326,28 +1326,31 @@
 mber
+: 1
  +
-=
  index
-);%0A
+,%0A  
         
 matc
@@ -1349,36 +1349,44 @@
     
-matches = matches.concat(ret
+  columnIndex: match.index%0A        %7D
 );%0A 

refactor countMatchContentEnd