import {
  DEFAULT_OPTIONS,
} from './constants';

import isCharUpperCase from './utils/isCharUpperCase';
import hiraganaToKatakana from './utils/hiraganaToKatakana';
import {
  getRomajiToKanaTree,
  IME_MODE_MAP,
  USE_OBSOLETE_KANA_MAP,
} from './romajiToKanaMap';
import { applyMapping } from './kanaMappingUtils';

/**
 * Convert [Romaji](https://en.wikipedia.org/wiki/Romaji) to [Kana](https://en.wikipedia.org/wiki/Kana), lowercase text will result in [Hiragana](https://en.wikipedia.org/wiki/Hiragana) and uppercase text will result in [Katakana](https://en.wikipedia.org/wiki/Katakana).
 * @param  {String} [input=''] text
 * @param  {DefaultOptions} [options=defaultOptions]
 * @return {String} converted text
 * @example
 * toKana('onaji BUTTSUUJI')
 * // => 'おなじ ブッツウジ'
 * toKana('ONAJI buttsuuji')
 * // => 'オナジ ぶっつうじ'
 * toKana('座禅‘zazen’スタイル')
 * // => '座禅「ざぜん」スタイル'
 * toKana('batsuge-mu')
 * // => 'ばつげーむ'
 * toKana('!?.:/,~-‘’“”[](){}') // Punctuation conversion
 * // => '！？。：・、〜ー「」『』［］（）｛｝'
 * toKana('we', { useObsoleteKana: true })
 * // => 'ゑ'
 */
export function toKana(input = '', options = {}) {
  // just throw away the substring index information and just concatenate all the kana
  return splitIntoKana(input, options).map((kanaToken) => kanaToken[2]).join('');
}

export function splitIntoKana(input = '', options = {}) {
  const config = Object.assign({}, DEFAULT_OPTIONS, options);

  let map = getRomajiToKanaTree();
  map = config.IMEMode? IME_MODE_MAP(map): map;
  map = config.useObsoleteKana? USE_OBSOLETE_KANA_MAP(map): map;
  map = config.customKanaMapping(map);

  const result = applyMapping(input.toLowerCase(), map, !config.IMEMode);

  return result.map((element) => {
    const [start, end, kan] = element;
    if (kan === null) {
      // we encountered the last chunk, that should not be converted yet
      return [start, end, input.slice(start)];
    }
    if (!config.ignoreCase && isCharUpperCase(input[start])) {
      return [start, end, hiraganaToKatakana(kan)];
    }
    return element;
  });
}

export default toKana;

@@ -47,133 +47,41 @@
 s';%0A
-%0A
 import 
-isCharUpperCase from './utils/isCharUpperCase';%0Aimport hiraganaToKatakana from './utils/hiraganaToKatakana';%0Aimport %7B
+%7B%0A  getKanaPostProcessing,
 %0A  g
@@ -1314,16 +1314,76 @@
 ptions);
+%0A  const kanaPostProcessing = getKanaPostProcessing(config);
 %0A%0A  let 
@@ -1408,16 +1408,22 @@
 anaTree(
+config
 );%0A  map
@@ -1578,17 +1578,24 @@
   const 
-r
+mappingR
 esult = 
@@ -1655,378 +1655,138 @@
 e);%0A
-%0A
   
-r
+l
 et
-urn
  result
-.map((element) =%3E %7B%0A    const %5Bstart, end, kan%5D = element;%0A    if (kan === null) %7B%0A      // we encountered the last chunk, that should not be converted yet%0A      return %5Bstart, end, input.slice(start)%5D;%0A    %7D%0A    if (!config.ignoreCase && isCharUpperCase(input%5Bstart%5D)) %7B%0A      return %5Bstart, end, hiraganaToKatakana(kan)%5D;%0A    %7D%0A    return element;%0A  %7D)
+ = kanaPostProcessing(%5Binput, mappingResult%5D);%0A  result = config.customKanaPostProcessing(result);%0A%0A  return result%5B1%5D
 ;%0A%7D%0A

Add post processing to toKana