/**
 * ldesign v0.2.14
 * (c) 2022/11/2 16:16 built by swimly
 * @license MIT
 */

'use strict';

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

var cloneDeep = require('lodash/cloneDeep');
var utils_event = require('../../utils/event.js');
require('../../utils/helper.js');
require('lodash/camelCase');

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

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

const TRANSFER_NAME = "LTransfer";
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) {
  let {
    isTreeMode = false,
    include = true
  } = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  if (isTreeMode) {
    let result = [];
    if (data) {
      for (let i = 0; i < data.length; i++) {
        var _item$children;
        const item = data[i];
        const 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) {
          const childResult = getDataValues(item.children, filterValues, {
            isTreeMode,
            include
          });
          result = result.concat(childResult);
        } else if (include && isInclude || !include && !isInclude) {
          result.push(item.value);
        }
      }
    }
    return result;
  }
  return data.filter(item => {
    const isInclude = filterValues.includes(item.value);
    return (include && isInclude || !include && !isInclude) && !item.disabled;
  }).map(item => item.value);
}
function getTransferData(data, keys) {
  let isTreeMode = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
  const list = data.map((transferDataItem, index) => {
    const labelKey = (keys === null || keys === void 0 ? void 0 : keys.label) || "label";
    const valueKey = (keys === null || keys === void 0 ? void 0 : keys.value) || "value";
    if (transferDataItem[labelKey] === void 0) console.error(`${labelKey} is not in DataOption ${JSON.stringify(transferDataItem)}`);
    if (transferDataItem[valueKey] === void 0) console.error(`${valueKey} is not in DataOption ${JSON.stringify(transferDataItem)}`);
    const result = {
      label: transferDataItem[labelKey],
      value: transferDataItem[valueKey],
      key: `key__value_${transferDataItem[valueKey]}_index_${index}`,
      disabled: transferDataItem.disabled ?? 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(item => isTreeNodeValid(item, filterValues, needMatch));
  return !needMatch;
}
function cloneTreeWithFilter(sourceTree, targetTree, filterValues, needMatch) {
  sourceTree.forEach(item => {
    let newNode;
    if (isAllNodeValid(item, filterValues, needMatch)) {
      newNode = cloneDeep__default["default"](item);
      targetTree.push(newNode);
    } else if (isTreeNodeValid(item, filterValues, needMatch)) {
      newNode = {
        ...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) {
  let needMatch = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
  let isTreeMode = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
  if (!isTreeMode) {
    if (needMatch) {
      return filterValues.map(value => data.find(item => item.value === value));
    }
    return data.filter(item => {
      const isMatch = filterValues.includes(item.value);
      return !isMatch;
    });
  }
  const result = [];
  cloneTreeWithFilter(data, result, filterValues, needMatch);
  return result;
}
function getLeafCount(nodes) {
  let total = 0;
  nodes.forEach(child => {
    if (child.children && child.children.length > 0) total += getLeafCount(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.getLeafCount = getLeafCount;
exports.getTransferData = getTransferData;
exports.getTransferListOption = getTransferListOption;
//# sourceMappingURL=utils.js.map
