/**
 * tdesign v0.32.0
 * (c) 2021 tdesign
 * @license MIT
 */

'use strict';

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

var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var cloneDeep = require('lodash/cloneDeep');
var utils_event = require('../utils/event.js');
require('../utils/helper.js');
require('@babel/runtime/helpers/objectWithoutProperties');
require('@babel/runtime/helpers/slicedToArray');
require('lodash/camelCase');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var _defineProperty__default = /*#__PURE__*/_interopDefaultLegacy(_defineProperty);
var cloneDeep__default = /*#__PURE__*/_interopDefaultLegacy(cloneDeep);

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty__default["default"](target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
var TRANSFER_NAME = "TTransfer";

function findTopNode(vm) {
  if (vm.$options.name === TRANSFER_NAME) {
    return vm;
  }

  if (vm.$parent) {
    return findTopNode(vm.$parent);
  }

  return vm;
}

function getTransferListOption(prop) {
  if (Array.isArray(prop)) {
    return {
      source: prop[0],
      target: prop[1]
    };
  }

  return {
    source: prop,
    target: prop
  };
}

function getDataValues(data, filterValues) {
  var _ref = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {},
      _ref$isTreeMode = _ref.isTreeMode,
      isTreeMode = _ref$isTreeMode === void 0 ? false : _ref$isTreeMode,
      _ref$include = _ref.include,
      include = _ref$include === void 0 ? true : _ref$include;

  if (isTreeMode) {
    var result = [];

    if (data) {
      for (var i = 0; i < data.length; i++) {
        var _item$children;

        var item = data[i];
        var isInclude = filterValues.includes(item.value);

        if (!include && isInclude) {
          continue;
        }

        if (item !== null && item !== void 0 && item.children && ((_item$children = item.children) === null || _item$children === void 0 ? void 0 : _item$children.length) > 0) {
          var childResult = getDataValues(item.children, filterValues, {
            isTreeMode: isTreeMode,
            include: include
          });
          result = result.concat(childResult);
        } else if (include && isInclude || !include && !isInclude) {
          result.push(item.value);
        }
      }
    }

    return result;
  }

  return data.filter(function (item) {
    var isInclude = filterValues.includes(item.value);
    return (include && isInclude || !include && !isInclude) && !item.disabled;
  }).map(function (item) {
    return item.value;
  });
}

function getTransferData(data, keys) {
  var isTreeMode = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
  var list = data.map(function (transferDataItem, index) {
    var _transferDataItem$dis;

    var labelKey = (keys === null || keys === void 0 ? void 0 : keys.label) || "label";
    var valueKey = (keys === null || keys === void 0 ? void 0 : keys.value) || "value";

    if (transferDataItem[labelKey] === void 0) {
      throw "".concat(labelKey, " is not in DataOption ").concat(JSON.stringify(transferDataItem));
    }

    if (transferDataItem[valueKey] === void 0) {
      throw "".concat(valueKey, " is not in DataOption ").concat(JSON.stringify(transferDataItem));
    }

    var result = {
      label: transferDataItem[labelKey],
      value: transferDataItem[valueKey],
      key: "key__value_".concat(transferDataItem[valueKey], "_index_").concat(index),
      disabled: (_transferDataItem$dis = transferDataItem.disabled) !== null && _transferDataItem$dis !== void 0 ? _transferDataItem$dis : false,
      data: transferDataItem
    };

    if (isTreeMode && transferDataItem.children) {
      result.children = getTransferData(transferDataItem.children, keys, true);
    }

    return result;
  });
  return list;
}

function isAllNodeValid(data, filterValues, needMatch) {
  if (filterValues.includes(data.value)) {
    return needMatch;
  }

  return false;
}

function isTreeNodeValid(data, filterValues, needMatch) {
  if (filterValues.includes(data.value)) {
    return needMatch;
  }

  if (data !== null && data !== void 0 && data.children) {
    return data === null || data === void 0 ? void 0 : data.children.some(function (item) {
      return isTreeNodeValid(item, filterValues, needMatch);
    });
  }

  return !needMatch;
}

function cloneTreeWithFilter(sourceTree, targetTree, filterValues, needMatch) {
  sourceTree.forEach(function (item) {
    var newNode;

    if (isAllNodeValid(item, filterValues, needMatch)) {
      newNode = cloneDeep__default["default"](item);
      targetTree.push(newNode);
    } else if (isTreeNodeValid(item, filterValues, needMatch)) {
      newNode = _objectSpread({}, item);
      delete newNode.children;
      targetTree.push(newNode);

      if (item.children) {
        newNode.children = [];
        cloneTreeWithFilter(item.children, newNode.children, filterValues, needMatch);

        if (newNode.children.length === 0) {
          delete newNode.children;
        }
      }
    }
  });
}

function filterTransferData(data, filterValues) {
  var needMatch = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
  var isTreeMode = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;

  if (!isTreeMode) {
    return data.filter(function (item) {
      var isMatch = filterValues.includes(item.value);
      return needMatch ? isMatch : !isMatch;
    });
  }

  var result = [];
  cloneTreeWithFilter(data, result, filterValues, needMatch);
  return result;
}

function getLeefCount(nodes) {
  var total = 0;
  nodes.forEach(function (child) {
    if (child.children && child.children.length > 0) {
      total += getLeefCount(child.children);
    } else {
      total += 1;
    }
  });
  return total;
}

exports.emitEvent = utils_event.emitEvent;
exports.TRANSFER_NAME = TRANSFER_NAME;
exports.cloneTreeWithFilter = cloneTreeWithFilter;
exports.filterTransferData = filterTransferData;
exports.findTopNode = findTopNode;
exports.getDataValues = getDataValues;
exports.getLeefCount = getLeefCount;
exports.getTransferData = getTransferData;
exports.getTransferListOption = getTransferListOption;
//# sourceMappingURL=utils.js.map
