/**
 * Created by baoyinghai on 12/1/16.
 */

/**
 * 选中菜单的路径下标
 * @param indexPath {array}
 * @param menu {array}
 * @return {array}
 * */
export const getOpenKeys = (indexPath = [], menu) => {
  const openKeys = [];
  indexPath.slice(0, indexPath.length - 1).reduce((target = [], index) => {
    openKeys.push(target[index].menuCode);
    return target[index] && target[index].subMenus;
  }, menu);
  return openKeys;
};

/**
 *  获得某个菜单项第一个 深度优先遍历
 *  @param func {function}
 *  @param menu {array}
 *  @return {object}
 * */
export const getMenuItemByFunc = (func, menu) => {
  let tag = null;
  menu && menu.some(m => {
    if (func(m)) {
      tag = m;
      return true;
    } else if (m.subMenus) {
      tag = getMenuItemByFunc(func, m.subMenus);
      if (tag) {
        return true;
      }
    }
    return false;
  });
  return tag;
};

/* eslint-disable */
export const getMenuItemAndPathByFunc = (func , menu) => {
  // 获得选中菜单项和他的openkeys/keypath
  let tag = null;
  const indexPath = [];

  const filter = (item) => {
    if (item.subMenus) {
      item.subMenus.every((i, index) => {
        if(index !==0 ) {
          indexPath.pop();
        }
        indexPath.push(index);
        filter(i);
        return !tag;
      });
      !tag && indexPath.pop();
    }
    if (!tag && func(item)) {
      tag = item;
    }
  };
  menu.every((m, index) => {
    if(index !== 0 ) {
      indexPath.pop();
    }
    indexPath.push(index);
    filter(m);
    return !tag;
  });
  !tag && indexPath.pop();

  const openKeys = getOpenKeys(indexPath, menu);

  return { linkInfo: tag, openKeys, indexPath };
};

/**
 * 根据keypaths获得菜单项
 * @param paths {array}
 * @param menuRoot {array}
 * @return {object}
 * */
export const getMenuItemByKeyPaths = (paths = [], menuRoot) => paths.reverse()
  .reduce((target, p) => target && target.subMenus && target.subMenus.filter(a => a.menuCode === p)[0], { subMenus: menuRoot });

const transformToList = a => {
  if (!a.subMenus) {
    return [a];
  }
  // TODO: 默认的菜单root节点不应该渲染界面.
  let ret = [a];
  // let ret = [];
  a.subMenus.forEach(s => {
    const sub = transformToList(s);
    ret = ret.concat(sub);
  });
  return ret;
};

// 找前一个菜单和后一个菜单
export const searchBeforeAndAfter = (menuCode, menu) => {
  let list = [];
  menu.forEach(s => {
    const tmp = transformToList(s);
    list = list.concat(tmp);
  });
  list.forEach((value, key) => {
    if (value.subMenus && value.subMenus.length > 0) {
      list.splice(key,1);
    }
  });
  let left = {};
  let right = {};
  list.some((item, index) => {
    if (item.menuCode === menuCode) {
      if (index === 0) {
        left = null;
      } else {
        left = list[index - 1];
      }
      if (index === list.length - 1) {
        right = null;
      } else {
        right = list[index + 1];
      }
      return true;
    }
    return false;
  });
  return { left, right };
};

export const searchAllMenusByFunc = (m, func) => {
  const ret = [];
  function search(menu) {
    menu && menu.forEach(item => {
      if (item.subMenus) {
        search(item.subMenus);
      } else {
        if (func(item)) {
          ret.push(item);
        }
      }
    })
  }
  search(m);
  return ret;
};

// 递归实现
export const getAllLeafs = menu =>
  menu.reduce((sum, item) =>
    !item.subMenus? [...sum, item]: [...sum, ...getAllLeafs(item.subMenus)], []);


