'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _isEmpty = require('./utils/isEmpty');

var _isEmpty2 = _interopRequireDefault(_isEmpty);

var _isCharKana = require('./utils/isCharKana');

var _isCharKana2 = _interopRequireDefault(_isCharKana);

var _isCharPunctuation = require('./utils/isCharPunctuation');

var _isCharPunctuation2 = _interopRequireDefault(_isCharPunctuation);

var _isJapanese = require('./isJapanese');

var _isJapanese2 = _interopRequireDefault(_isJapanese);

var _isKana = require('./isKana');

var _isKana2 = _interopRequireDefault(_isKana);

var _isKanji = require('./isKanji');

var _isKanji2 = _interopRequireDefault(_isKanji);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }

/**
 * Strips trailing [Okurigana](https://en.wikipedia.org/wiki/Okurigana) if `input` is a mix of [Kanji](https://en.wikipedia.org/wiki/Kanji) and [Kana](https://en.wikipedia.org/wiki/Kana)
 * @param  {String} input text
 * @param  {Object} [options={ all: false }] config object specifying if *all* kana should be removed, not just trailing okurigana
 * @return {String} text with okurigana removed
 * @example
 * stripOkurigana('踏み込む')
 * // => '踏み込'
 * stripOkurigana('粘り。')
 * // => '粘。'
 * stripOkurigana('お祝い')
 * // => 'お祝'
 * stripOkurigana('踏み込む', { all: true })
 * // => '踏込'
 * stripOkurigana('お祝い', { all: true })
 * // => '祝'
 */
function stripOkurigana() {
  var input = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
  var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : { all: false };

  if ((0, _isEmpty2.default)(input) || !(0, _isJapanese2.default)(input) || (0, _isKana2.default)(input)) return input;
  var chars = [].concat(_toConsumableArray(input));

  // strip every kana
  if (options.all) return chars.filter(function (char) {
    return !(0, _isCharKana2.default)(char);
  }).join('');

  // strip trailing only
  var reverseChars = chars.reverse();
  for (var i = 0, len = reverseChars.length; i < len; i += 1) {
    var char = reverseChars[i];
    // pass if it's punctuation
    if ((0, _isCharPunctuation2.default)(char)) {
      continue; // eslint-disable-line no-continue
    }
    // blank out if not kanji
    if (!(0, _isKanji2.default)(char)) {
      reverseChars[i] = '';
    } else {
      break; // stop when we hit a kanji char
    }
  }

  return reverseChars.reverse().join('');
}

exports.default = stripOkurigana;