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

'use strict';

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

var isEmpty = require('lodash/isEmpty');

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

var isEmpty__default = /*#__PURE__*/_interopDefaultLegacy(isEmpty);

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