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

import isNumber from 'lodash/isNumber';
import isObject from 'lodash/isObject';
import isArray from 'lodash/isArray';
import isEmpty from 'lodash/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(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(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(value)) {
    if (value.length > 0 && isObject(value[0])) treeValue = value.map(function (val) {
      return val.value;
    });else if (value.length) treeValue = value;
  } else if (value) {
    if (isObject(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(value) && !isNaN(value)) return false;
  return isEmpty(value);
}
function isValueInvalid(value, cascaderContext) {
  var multiple = cascaderContext.multiple,
    showAllLevels = cascaderContext.showAllLevels;
  return multiple && !isArray(value) || !multiple && isArray(value) && !showAllLevels;
}

export { getCascaderValue, getFullPathLabel, getMultipleContent, getPanels, getSingleContent, getTreeValue, isEmptyValues, isValueInvalid };
//# sourceMappingURL=helper.js.map
