import * as _vue from "vue";

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

import { inject, TransitionGroup } from 'vue';
import PropTypes from '../../../../_util/vue-types';
import { createRef } from '../../util';
import generateSelector, { selectorPropTypes } from '../../Base/BaseSelector';
import SearchInput from '../../SearchInput';
import Selection from './Selection';
import { getComponent, getSlot } from '../../../../_util/props-util';
import getTransitionProps from '../../../../_util/getTransitionProps';
import BaseMixin from '../../../../_util/BaseMixin';
var TREE_SELECT_EMPTY_VALUE_KEY = 'RC_TREE_SELECT_EMPTY_VALUE_KEY';
var Selector = generateSelector('multiple'); // export const multipleSelectorContextTypes = {
//   onMultipleSelectorRemove: PropTypes.func.isRequired,
// }

var MultipleSelector = {
  name: 'MultipleSelector',
  mixins: [BaseMixin],
  inheritAttrs: false,
  props: _objectSpread(_objectSpread(_objectSpread({}, selectorPropTypes()), SearchInput.props), {}, {
    selectorValueList: PropTypes.array,
    disabled: PropTypes.bool,
    searchValue: PropTypes.string,
    labelInValue: PropTypes.bool,
    maxTagCount: PropTypes.number,
    maxTagPlaceholder: PropTypes.any // onChoiceAnimationLeave: PropTypes.func,

  }),
  setup: function setup() {
    return {
      vcTreeSelect: inject('vcTreeSelect', {})
    };
  },
  created: function created() {
    this.inputRef = createRef();
  },
  methods: {
    onPlaceholderClick: function onPlaceholderClick() {
      this.inputRef.current.focus();
    },
    focus: function focus() {
      this.inputRef.current.focus();
    },
    blur: function blur() {
      this.inputRef.current.blur();
    },
    _renderPlaceholder: function _renderPlaceholder() {
      var _this$$props = this.$props,
          prefixCls = _this$$props.prefixCls,
          placeholder = _this$$props.placeholder,
          searchPlaceholder = _this$$props.searchPlaceholder,
          searchValue = _this$$props.searchValue,
          selectorValueList = _this$$props.selectorValueList;
      var currentPlaceholder = placeholder || searchPlaceholder;
      if (!currentPlaceholder) return null;
      var hidden = searchValue || selectorValueList.length; // [Legacy] Not remove the placeholder

      return _vue.createVNode("span", {
        "style": {
          display: hidden ? 'none' : 'block'
        },
        "onClick": this.onPlaceholderClick,
        "class": "".concat(prefixCls, "-search__field__placeholder")
      }, [currentPlaceholder]);
    },
    onChoiceAnimationLeave: function onChoiceAnimationLeave() {
      for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
        args[_key] = arguments[_key];
      }

      this.__emit.apply(this, ['choiceAnimationLeave'].concat(args));
    },
    renderSelection: function renderSelection() {
      var _this = this;

      var _this$$props2 = this.$props,
          selectorValueList = _this$$props2.selectorValueList,
          choiceTransitionName = _this$$props2.choiceTransitionName,
          prefixCls = _this$$props2.prefixCls,
          labelInValue = _this$$props2.labelInValue,
          maxTagCount = _this$$props2.maxTagCount;
      var children = getSlot(this);
      var onMultipleSelectorRemove = this.vcTreeSelect.onMultipleSelectorRemove; // Check if `maxTagCount` is set

      var myValueList = selectorValueList;

      if (maxTagCount >= 0) {
        myValueList = selectorValueList.slice(0, maxTagCount);
      } // Selector node list


      var selectedValueNodes = myValueList.map(function (_ref) {
        var label = _ref.label,
            value = _ref.value;
        return _vue.createVNode(Selection, _vue.mergeProps(_objectSpread(_objectSpread({}, _this.$props), {}, {
          label: label,
          value: value,
          onRemove: onMultipleSelectorRemove
        }), {
          "key": value || TREE_SELECT_EMPTY_VALUE_KEY
        }), {
          default: function _default() {
            return [children];
          }
        });
      }); // Rest node count

      if (maxTagCount >= 0 && maxTagCount < selectorValueList.length) {
        var content = "+ ".concat(selectorValueList.length - maxTagCount, " ...");
        var maxTagPlaceholder = getComponent(this, 'maxTagPlaceholder', {}, false);

        if (typeof maxTagPlaceholder === 'string') {
          content = maxTagPlaceholder;
        } else if (typeof maxTagPlaceholder === 'function') {
          var restValueList = selectorValueList.slice(maxTagCount);
          content = maxTagPlaceholder(labelInValue ? restValueList : restValueList.map(function (_ref2) {
            var value = _ref2.value;
            return value;
          }));
        }

        var restNodeSelect = _vue.createVNode(Selection, _vue.mergeProps(_objectSpread(_objectSpread({}, this.$props), {}, {
          label: content,
          value: null
        }), {
          "key": "rc-tree-select-internal-max-tag-counter"
        }), {
          default: function _default() {
            return [children];
          }
        });

        selectedValueNodes.push(restNodeSelect);
      }

      selectedValueNodes.push(_vue.createVNode("li", {
        "class": "".concat(prefixCls, "-search ").concat(prefixCls, "-search--inline"),
        "key": "__input"
      }, [_vue.createVNode(SearchInput, _vue.mergeProps(_objectSpread(_objectSpread(_objectSpread({}, this.$props), this.$attrs), {}, {
        needAlign: true
      }), {
        "ref": this.inputRef
      }), {
        default: function _default() {
          return [children];
        }
      })]));
      var className = "".concat(prefixCls, "-selection__rendered");

      if (choiceTransitionName) {
        var transitionProps = getTransitionProps(choiceTransitionName, {
          tag: 'ul',
          onAfterLeave: this.onChoiceAnimationLeave
        });
        return _vue.createVNode(TransitionGroup, _vue.mergeProps(transitionProps, {
          "class": className
        }), {
          default: function _default() {
            return [selectedValueNodes];
          }
        });
      }

      return _vue.createVNode("ul", {
        "class": className,
        "role": "menubar"
      }, [selectedValueNodes]);
    }
  },
  render: function render() {
    var _this2 = this;

    return _vue.createVNode(Selector, _objectSpread(_objectSpread(_objectSpread({}, this.$props), this.$attrs), {}, {
      tabindex: -1,
      showArrow: false,
      renderSelection: this.renderSelection,
      renderPlaceholder: this._renderPlaceholder
    }), {
      default: function _default() {
        return [getSlot(_this2)];
      }
    });
  }
};
export default MultipleSelector;