"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.isReadOnly = isReadOnly;
exports.findOption = findOption;
exports.generateOpts = generateOpts;
exports.traverseOptions = traverseOptions;
exports.getSelectOpts = getSelectOpts;

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));

function isReadOnly(showSearch, readOnly) {
  if (typeof showSearch !== 'undefined') {
    return !showSearch;
  }

  return readOnly;
}

function findOption(options, targetVal, filedNames) {
  var target = null;

  function dfs(data) {
    for (var i = 0; i < data.length; i += 1) {
      if (!data[i]) continue;

      if (data[i][filedNames.value] === targetVal) {
        target = data[i];
      }

      if (data[i][filedNames.children]) {
        dfs(data[i][filedNames.children]);
      }
    }
  }

  dfs(options);
  return target;
}

function valueIntoArr(key, value, opt) {
  opt[key].forEach(function (val) {
    val.unshift(value);
  });
} // 生成select的options
// 需要将当前value组装为 父value/value/子value


function generateOpts(filedNames, value, data) {
  if (!data || !data.length || !value) return [[], []]; // 保存不同的value的路径

  var target = []; // 记录已经存在的value，剪枝

  var map = new Map();
  var valuesMap = new Map();
  value.forEach(function (v) {
    valuesMap.set(v, true);
  });
  var allCheckedValues = [];
  var allCheckedLabels = [];

  var _loop = function _loop(i) {
    var curVal = value[i];
    if (map.has(curVal)) return "continue";
    var targetOpt = findOption(data, curVal, filedNames); // 保存当前value的子元素路径和父元素路径

    var opt = {
      values: [],
      labels: []
    }; // 全排列 回溯

    if (targetOpt) {
      map.set(targetOpt[filedNames.value], true);
      var childs = targetOpt[filedNames.children];

      var helper = function helper(options, values, labels) {
        if (!options || !options.length) {
          // 所有数据必须在valuesMap中都存在
          for (var k = 0; k < values.length; k += 1) {
            if (!valuesMap.has(values[k])) {
              return;
            }
          } // 这里一定要拷贝


          opt.values.push((0, _toConsumableArray2.default)(values));
          opt.labels.push((0, _toConsumableArray2.default)(labels));
          return;
        }

        for (var j = 0; j < options.length; j += 1) {
          var item = options[j];
          map.set(item[filedNames.value], true); // 必须是valuesMap存在的值

          values.push(item[filedNames.value]);
          labels.push(item[filedNames.label]);
          helper(item[filedNames.children], values, labels);
          values.pop();
          labels.pop();
        }
      };

      helper(childs, [], []); // if (valuesMap.has(targetOpt[filedNames.value])) {
      // 拼接当前option的值

      valueIntoArr('values', targetOpt[filedNames.value], opt);
      valueIntoArr('labels', targetOpt[filedNames.label], opt); // }
      // 拼接当前option父级节点的值

      var p = targetOpt.parent;

      while (p) {
        map.set(p[filedNames.value], true); // if (valuesMap.has(p[filedNames.value])) {

        valueIntoArr('values', p[filedNames.value], opt);
        valueIntoArr('labels', p[filedNames.label], opt); // }

        p = p.parent;
      }
    }

    target.push(opt);
  };

  for (var i = 0; i < value.length; i += 1) {
    var _ret = _loop(i);

    if (_ret === "continue") continue;
  }

  target.forEach(function (item) {
    allCheckedValues = allCheckedValues.concat(item.values);
    allCheckedLabels = allCheckedLabels.concat(item.labels);
  });
  console.log('allCheckedValues...', allCheckedValues);
  return [allCheckedValues, allCheckedLabels];
} // 重新组装options数据


function traverseOptions(options, fieldNames) {
  if (!options || !options.length) return [];

  function dfs(data, prevNode, level) {
    return data.map(function (option) {
      if (option) {
        var _target;

        var target = (_target = {}, (0, _defineProperty2.default)(_target, fieldNames.label, option[fieldNames.label]), (0, _defineProperty2.default)(_target, fieldNames.value, option[fieldNames.value]), (0, _defineProperty2.default)(_target, fieldNames.children, option[fieldNames.children]), (0, _defineProperty2.default)(_target, "disabled", option.disabled), (0, _defineProperty2.default)(_target, "parent", prevNode), _target);

        if (target[fieldNames.children]) {
          target[fieldNames.children] = dfs(target[fieldNames.children], target, level + 1);
        }

        return target;
      }

      return option;
    });
  }

  return dfs(options, null, 0);
}

function getSelectOpts(fields, value, options) {
  var selectOptions = [];
  var selectVals = [];

  if (options && value) {
    var storeMap = new Map();
    value.forEach(function (keys) {
      var opt = {};
      var labels = [];
      if (!keys.length) return;
      keys.forEach(function (key) {
        var target = null;

        if (storeMap.has(key)) {
          target = storeMap.get(key);
        } else {
          target = findOption(options, key, fields);
          storeMap.set(key, target);
        }

        if (target) {
          labels.push(target[fields.label]);
        }
      });

      if (keys.length === labels.length) {
        opt.value = keys.join('/');
        opt.label = labels.join('/');
        selectVals.push(opt.value);
        selectOptions.push(opt);
      }
    });
  }

  return [selectOptions, selectVals];
}