"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});

function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }

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

/**
 * 选中菜单的路径下标
 * @param indexPath {array}
 * @param menu {array}
 * @return {array}
 * */
var getOpenKeys = exports.getOpenKeys = function getOpenKeys() {
  var indexPath = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  var menu = arguments[1];

  var openKeys = [];
  indexPath.slice(0, indexPath.length - 1).reduce(function () {
    var target = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
    var index = arguments[1];

    openKeys.push(target[index].menuCode);
    return target[index] && target[index].subMenus;
  }, menu);
  return openKeys;
};

/**
 *  获得某个菜单项第一个 深度优先遍历
 *  @param func {function}
 *  @param menu {array}
 *  @return {object}
 * */
var getMenuItemByFunc = exports.getMenuItemByFunc = function getMenuItemByFunc(func, menu) {
  var tag = null;
  menu && menu.some(function (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 */
var getMenuItemAndPathByFunc = exports.getMenuItemAndPathByFunc = function getMenuItemAndPathByFunc(func, menu) {
  // 获得选中菜单项和他的openkeys/keypath
  var tag = null;
  var indexPath = [];

  var filter = function filter(item) {
    if (item.subMenus) {
      item.subMenus.every(function (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(function (m, index) {
    if (index !== 0) {
      indexPath.pop();
    }
    indexPath.push(index);
    filter(m);
    return !tag;
  });
  !tag && indexPath.pop();

  var openKeys = getOpenKeys(indexPath, menu);

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

/**
 * 根据keypaths获得菜单项
 * @param paths {array}
 * @param menuRoot {array}
 * @return {object}
 * */
var getMenuItemByKeyPaths = exports.getMenuItemByKeyPaths = function getMenuItemByKeyPaths() {
  var paths = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  var menuRoot = arguments[1];
  return paths.reverse().reduce(function (target, p) {
    return target && target.subMenus && target.subMenus.filter(function (a) {
      return a.menuCode === p;
    })[0];
  }, { subMenus: menuRoot });
};

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

// 找前一个菜单和后一个菜单
var searchBeforeAndAfter = exports.searchBeforeAndAfter = function searchBeforeAndAfter(menuCode, menu) {
  var list = [];
  menu.forEach(function (s) {
    var tmp = transformToList(s);
    list = list.concat(tmp);
  });
  list.forEach(function (value, key) {
    if (value.subMenus && value.subMenus.length > 0) {
      list.splice(key, 1);
    }
  });
  var left = {};
  var right = {};
  list.some(function (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: left, right: right };
};

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

// 递归实现
var getAllLeafs = exports.getAllLeafs = function getAllLeafs(menu) {
  return menu.reduce(function (sum, item) {
    return !item.subMenus ? [].concat(_toConsumableArray(sum), [item]) : [].concat(_toConsumableArray(sum), _toConsumableArray(getAllLeafs(item.subMenus)));
  }, []);
};