/**
 * @param {{}} object
 * @param {string[]} root
 * @param {{}} result
 * @returns {string[]}
 */
function extractPaths(object, root = [], result = {}) {
  if (!object || typeof object !== 'object') return [];

  const keys = Object.keys(object);
  const results = keys.reduce((res, key) => {
    const path = root.concat(key);

    /* Ensure that empty value is missing translation */
    if (object[key] === '') return res;

    if (typeof object[key] === 'object' && object[key] !== null) {
      extractPaths(object[key], path, res);
    } else if (Array.isArray(res[object[key]])) {
      res[object[key]].push(path);
    } else {
      res[object[key]] = [path];
    }

    return res;
  }, result);

  const merged = Object.keys(results).reduce((acc, key) => [...acc, ...results[key]], []);

  return merged.map((data) => data.join('.'));
}

module.exports = extractPaths;
