/**
 *
 * 设置默认选中项和选中项丢失后的处理
 *
 *   记录中没有选中项或者选中项和store中保存的项不匹配 --则需要进行重新选择
 *
 *      先从记录中找到匹配的项，找到则直接设置为选中
 *      否则查找该项的父节点设置为选中
 *
 *   如果无父节点则选中所有记录中的第一项
 *
 *   否则清空选中
 *
 * 要求 Record 中必须包含字段
 *   key 当前节点的标识
 *   parentId 父节点标识
 *   itemType 节点类型
 *
 * */
export default (function (dataSet, store, getEnvItem) {
  var selectRecord = function selectRecord(record) {
    record.isSelected = true;
    var data = record.toData();
    store.setSelectedMenu(data);

    if (getEnvItem) {
      getEnvItem(record);
    }
  };

  if (dataSet.length) {
    var selectedRecord = dataSet.find(function (record) {
      return record.isSelected;
    });
    var selectedMenu = store.getSelectedMenu;
    var selectedKey = selectedMenu.key,
        selectedParentId = selectedMenu.parentId;

    if (!selectedRecord || selectedRecord.get('key') !== selectedKey) {
      var nextSelected = dataSet.find(function (record) {
        return record.get('key') === selectedKey;
      });

      if (nextSelected) {
        selectRecord(nextSelected);
      } else {
        var parent = dataSet.find(function (record) {
          return record.get('key') === selectedParentId;
        });

        if (parent) {
          selectRecord(parent);
        } else {
          var first = dataSet.get(0);

          if (first) {
            selectRecord(first);
          } else {
            store.setSelectedMenu({});
          }
        }
      }
    }
  }
});