import LTT from 'list-to-tree';
import {
  orderBy,
  find,
  filter,
  flatten,
  map
} from 'lodash';

export function convertToTree (itemList) {
  // ------------------------------------
  // Combine navigation number and names
  // and
  // Give each item in the navigation a level specific id for sorting
  // ------------------------------------
  itemList.results.map(item => {
    item.name = item.function_id + ' ' + item.name;
    item.sort_id = item.function_id.substring(item.function_id.length - 2, item.function_id.length);
    item.path = [];
  });
  const ltt = new LTT(itemList.results, {
    key_id: 'id',
    key_parent: 'parent',
    key_child: 'children'
  });
  const unOrderedTree = ltt.GetTree();
  // ------------------------------------
  // Sort the tree, as ltt doesnt automatically do it
  // ------------------------------------
  const sortTree = tree => {
    tree = orderBy(tree, ['sort_id'], 'asc');
    return tree.map(item => {
      if (item.children !== undefined) {
        // ------------------------------------
        // Generate path to show when navigation is minimized and TOS is shown
        // ------------------------------------
        item.path.push(item.name);
        item.children.map(child => {
          item.path.map(path => child.path.push(path));
        });
        item.children = orderBy(item.children, ['sort_id'], 'asc');
        sortTree(item.children);
      }
      return item;
    });
  };
  return sortTree(unOrderedTree);
}

/**
 * Find item by id (nested array with `children`-key)
 * @param items
 * @param id
 * @returns {*}
 */
export function itemById (items, id) {
  let searchResult = find(items, (item) => (item.id === id));

  if (!searchResult) {
    let filteredItems = filter(items, (item) => (item.children));
    let subset = flatten(map(filteredItems, (item) => (item.children)));

    if (subset.length !== 0) {
      return itemById(subset, id);
    }
    return null;
  }
  return searchResult;
}

/**
 * Centered PopUp-Window
 * @param url
 * @param title
 * @param w
 * @param h
 * @returns {Window}
 */
export function centeredPopUp (url, title, w, h) {
  const left = (screen.width / 2) - (w / 2);
  const top = (screen.height / 2) - (h / 2);
  return window.open(
    url,
    title,
    `
    toolbar=no,
    location=no,
    directories=no,
    status=no, menubar=no,
    scrollbars=no,
    resizable=no,
    copyhistory=no,
    width=${w}, height=${h}, top=${top}, left=${left}
  `);
}

@@ -1487,24 +1487,721 @@
 edTree);%0A%7D%0A%0A
+/**%0A * Normalize tos for API%0A * @param tos%0A * @returns %7B*%7D%0A */%0Aexport function normalizeTosForApi (tos) %7B%0A  // TODO: needs some serious refactoring...%0A  const phases = Object.keys(tos.phases).map(phase =%3E tos.phases%5Bphase%5D);%0A  phases.map(phase =%3E phase.actions.map((action, actionIndex) =%3E %7B%0A    delete phase.actions%5BactionIndex%5D;%0A    phase.actions%5BactionIndex%5D = tos.actions%5Baction%5D;%0A    phase.actions%5BactionIndex%5D.records.map((record, recordsIndex) =%3E %7B%0A      delete phase.actions%5BactionIndex%5D.records%5BrecordsIndex%5D;%0A      phase.actions%5BactionIndex%5D.records%5BrecordsIndex%5D = tos.records%5Brecord%5D;%0A    %7D);%0A  %7D));%0A%0A  delete tos.actions;%0A  delete tos.records;%0A  tos.phases = phases;%0A  return tos;%0A%7D%0A%0A
 /**%0A * Find 

Add helper to purify data for API