/**
 * ldesign v0.3.13
 * (c) 2023 ldesign
 * @license MIT
 */

'use strict';

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

var isNumber = require('lodash/isNumber');
var isObject = require('lodash/isObject');
var isArray = require('lodash/isArray');
var isEmpty = require('lodash/isEmpty');

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

var isNumber__default = /*#__PURE__*/_interopDefaultLegacy(isNumber);
var isObject__default = /*#__PURE__*/_interopDefaultLegacy(isObject);
var isArray__default = /*#__PURE__*/_interopDefaultLegacy(isArray);
var isEmpty__default = /*#__PURE__*/_interopDefaultLegacy(isEmpty);

function getSingleContent(cascaderContext) {
  var value = cascaderContext.value,
    multiple = cascaderContext.multiple,
    treeStore = cascaderContext.treeStore,
    showAllLevels = cascaderContext.showAllLevels;
  if (multiple || value !== 0 && !value) return "";
  if (isArray__default["default"](value)) return "";
  var node = treeStore && treeStore.getNodes(value);
  if (!(node && node.length)) return value;
  var path = node && node[0].getPath();
  if (path && path.length) return showAllLevels ? path.map(function (node2) {
    return node2.label;
  }).join(" / ") : path[path.length - 1].label;
  return value;
}
function getMultipleContent(cascaderContext) {
  var value = cascaderContext.value,
    multiple = cascaderContext.multiple,
    treeStore = cascaderContext.treeStore,
    showAllLevels = cascaderContext.showAllLevels;
  if (!multiple) return [];
  if (multiple && !isArray__default["default"](value)) return [];
  var node = treeStore && treeStore.getNodes(value);
  if (!node) return [];
  return value.map(function (item) {
    var node2 = treeStore.getNodes(item);
    return showAllLevels ? getFullPathLabel(node2[0]) : node2[0].label;
  }).filter(function (item) {
    return !!item;
  });
}
function getPanels(treeNodes) {
  var panels = [];
  treeNodes.forEach(function (node) {
    if (panels[node.level]) panels[node.level].push(node);else panels[node.level] = [node];
  });
  return panels;
}
function getFullPathLabel(node) {
  var separator = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "/";
  return node === null || node === void 0 ? void 0 : node.getPath().map(function (node2) {
    return node2.label;
  }).join(separator);
}
var getTreeValue = function getTreeValue(value) {
  var treeValue = [];
  if (isArray__default["default"](value)) {
    if (value.length > 0 && isObject__default["default"](value[0])) treeValue = value.map(function (val) {
      return val.value;
    });else if (value.length) treeValue = value;
  } else if (value) {
    if (isObject__default["default"](value)) treeValue = [value.value];else treeValue = [value];
  }
  return treeValue;
};
var getCascaderValue = function getCascaderValue(value, valueType, multiple) {
  if (valueType === "single") return value;
  if (multiple) return value.map(function (item) {
    return item[item.length - 1];
  });
  return value[value.length - 1];
};
function isEmptyValues(value) {
  if (isNumber__default["default"](value) && !isNaN(value)) return false;
  return isEmpty__default["default"](value);
}
function isValueInvalid(value, cascaderContext) {
  var multiple = cascaderContext.multiple,
    showAllLevels = cascaderContext.showAllLevels;
  return multiple && !isArray__default["default"](value) || !multiple && isArray__default["default"](value) && !showAllLevels;
}

exports.getCascaderValue = getCascaderValue;
exports.getFullPathLabel = getFullPathLabel;
exports.getMultipleContent = getMultipleContent;
exports.getPanels = getPanels;
exports.getSingleContent = getSingleContent;
exports.getTreeValue = getTreeValue;
exports.isEmptyValues = isEmptyValues;
exports.isValueInvalid = isValueInvalid;
//# sourceMappingURL=helper.js.map
