text: const { NotImplementedError } = require('../extensions/index.js');
  
  /**
   * Given an array of domains, return the object with the appearances of the DNS.
   *
   * @param {Array} domains
   * @return {Object}
   *
   * @example
   * domains = [
   *  'code.yandex.ru',
   *  'music.yandex.ru',
   *  'yandex.ru'
   * ]
   *
   * The result should be the following:
   * {
   *   '.ru': 3,
   *   '.ru.yandex': 3,
   *   '.ru.yandex.code': 1,
   *   '.ru.yandex.music': 1,
   * }
   *
   */
   function getDNSStats(domains) { 
  
    let domainMap = new Map ()
  
    let obj = {}
    
    domains.forEach((val) => {
    let a = val.split('.').reverse()
    a.forEach((i, index) => domainMap.set(i, index) )  //create Map with all "small" domains
  })
  
  //calc value for each Map value
  let baseArr = Array.from(domainMap.keys())
  for (let i = 0; i< baseArr.length; i++){
    let value = domains.reduce((sum, val) => val.includes(baseArr[i]) ? sum = sum + 1 : sum , 0 )
  
    //create obj from Map
  if (domainMap.get(baseArr[i]) == 0){
    obj['.' + baseArr[i]] = value }
  if (domainMap.get(baseArr[i]) == 1){
  let a = getByValue(domainMap, 0)
  let objval = '.' + a + '.' + baseArr[i]
    obj[objval] = value }
  if (domainMap.get(baseArr[i]) == 2){
  let a = getByValue(domainMap, 0)
  let b = getByValue(domainMap, 1)
  let objval = '.' + a + '.' + b + '.' + baseArr[i] 
    obj[objval] = value }
  }
  
    //support func for return key by value from map 
    function getByValue(map, searchValue) {
    for (let [key, value] of map.entries()) {
      if (value === searchValue)
        return key;
    }
  }
  
  
  return obj
  }
  
  module.exports = {
    getDNSStats
  };
  