var isZalgo = require('../is/zalgo');
var categories = /[\p{Mn}\p{Me}]+/u;
var compose = string => string.normalize('NFC');
var decompose = string => string.normalize('NFD');

/**
 * Removes all Zalgo text characters for every word in a string if the word is zalgo text.
 *
 * @param {string} string string
 * @param {number} [threshold=0.55] threshold
 * @return {string} string
 *
 * @example
 * var snu = require('s-node-utils');
 *
 * snu.string.unzalgo('ť͈̓̆h̏̔̐̑ì̭ͯ͞s̈́̄̑͋');
 * //=> this
 */
module.exports = function(string, threshold = 0.55) {
  let cleaned = '';
  for (const word of decompose(string).split(/(\s+)/)) {
    if (isZalgo(word, threshold)) {
      for (const character of word) {
        if (!categories.test(character)) {
          cleaned += character;
        }
      }
    } else {
      cleaned += word;
    }
  }
  return compose(cleaned);
};
