/**
 * tdesign v0.39.1-alpha.1
 * (c) 2022 tdesign
 * @license MIT
 */

import _defineProperty from '@babel/runtime/helpers/defineProperty';
import isFunction from 'lodash/isFunction';
import isEmpty from 'lodash/isEmpty';

function getCloseIconClass(prefix, CLASSNAMES, cascaderContext) {
  var _ref;

  var visible = cascaderContext.visible,
      disabled = cascaderContext.disabled;
  return ["".concat(prefix, "-cascader__icon"), (_ref = {}, _defineProperty(_ref, CLASSNAMES.STATUS.visible, visible), _defineProperty(_ref, CLASSNAMES.STATUS.disabled, disabled), _ref)];
}
function getFakeArrowIconClass(prefix, CLASSNAMES, cascaderContext) {
  var disabled = cascaderContext.disabled;
  return ["".concat(prefix, "-cascader__icon"), _defineProperty({}, CLASSNAMES.STATUS.disabled, disabled)];
}
function getCascaderInnerClasses(prefix, CLASSNAMES, cascaderContext) {
  var _ref3;

  var disabled = cascaderContext.disabled,
      visible = cascaderContext.visible,
      size = cascaderContext.size,
      multiple = cascaderContext.multiple;
  return ["".concat(prefix, "-cascader"), (_ref3 = {}, _defineProperty(_ref3, CLASSNAMES.STATUS.disabled, disabled), _defineProperty(_ref3, CLASSNAMES.STATUS.active, visible), _defineProperty(_ref3, CLASSNAMES.SIZE[size], size), _defineProperty(_ref3, "".concat(prefix, "-cascader--multiple"), multiple), _ref3)];
}
function getCloseShow(isHover, cascaderContext) {
  var value = cascaderContext.value,
      disabled = cascaderContext.disabled,
      clearable = cascaderContext.clearable,
      visible = cascaderContext.visible;
  return !!(!visible && clearable && isHover && !disabled && !isEmpty(value));
}
function getPlaceholderShow(cascaderContext, singleContent, multipleContent) {
  var filterable = cascaderContext.filterable,
      visible = cascaderContext.visible,
      multiple = cascaderContext.multiple;
  var valEmpty = !(multiple ? multipleContent.length : singleContent);

  if (filterable) {
    return valEmpty && !visible;
  }

  return valEmpty;
}
function getSingleContent(cascaderContext) {
  var value = cascaderContext.value,
      multiple = cascaderContext.multiple,
      treeStore = cascaderContext.treeStore,
      showAllLevels = cascaderContext.showAllLevels,
      setValue = cascaderContext.setValue;
  if (multiple || !value) return "";
  if (Array.isArray(value)) return "";
  var node = treeStore && treeStore.getNodes(value);

  if (!(node && node.length)) {
    if (value) {
      setValue(multiple ? [] : "", "invalid-value");
    }

    return "";
  }

  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;
  if (!multiple) return [];
  if (multiple && !Array.isArray(value)) return [];
  var node = treeStore && treeStore.getNodes(value);
  if (!node) return [];
  var path = value.map(function (item) {
    var node2 = treeStore.getNodes(item);
    return node2[0];
  });

  if (value && value.length) {
    return path && path.length ? path : value;
  }

  return [];
}
function outerClickListenerEffect(ref, cascaderContext, event) {
  var visible = cascaderContext.visible,
      setVisible = cascaderContext.setVisible;

  if (!ref || ref.contains(event.target)) {
    return;
  }

  if (visible) {
    setVisible(false);
  }
}
function closeIconClickEffect(cascaderContext) {
  var setVisible = cascaderContext.setVisible,
      multiple = cascaderContext.multiple,
      setExpend = cascaderContext.setExpend,
      setValue = cascaderContext.setValue;
  setVisible(false);

  if (multiple) {
    setExpend([]);
  }

  setValue(multiple ? [] : "", "clear");
}
function handleRemoveTagEffect(cascaderContext, node, onRemove) {
  var disabled = cascaderContext.disabled,
      setValue = cascaderContext.setValue,
      valueType = cascaderContext.valueType,
      treeStore = cascaderContext.treeStore;
  if (disabled) return;
  var checked = node.setChecked(!node.isChecked());
  var resValue = valueType === "single" ? checked : checked.map(function (val) {
    return treeStore.getNode(val).getPath().map(function (item) {
      return item.value;
    });
  });
  setValue(resValue, "unchecked", node.getModel());

  if (isFunction(onRemove)) {
    onRemove({
      value: checked,
      node: node
    });
  }
}
function innerContentClickEffect(cascaderContext) {
  var setVisible = cascaderContext.setVisible,
      visible = cascaderContext.visible,
      filterActive = cascaderContext.filterActive,
      disabled = cascaderContext.disabled;

  if (!disabled) {
    if (visible && filterActive) return;
    setVisible(!visible);
  }
}

export { closeIconClickEffect, getCascaderInnerClasses, getCloseIconClass, getCloseShow, getFakeArrowIconClass, getMultipleContent, getPlaceholderShow, getSingleContent, handleRemoveTagEffect, innerContentClickEffect, outerClickListenerEffect };
//# sourceMappingURL=inputContent.js.map
