/**
 * ldesign v0.2.2
 * (c) 2022年9月20日 swimly
 * @license MIT
 */

import { h as helper } from '../../../_chunks/dep-b13ef886.js';
import _defineProperty from '@babel/runtime/helpers/defineProperty';
import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
import { toRefs, getCurrentInstance, ref, computed } from '@vue/composition-api';
import isObject from 'lodash/isObject';
import useDefaultValue from '../../../hooks/use-default-value.js';
import { usePrefixClass } from '../../../hooks/use-config.js';
import '../../config-provider/useConfig.js';
import '../../config-provider/context.js';
import 'lodash';
import '../../../_chunks/dep-a0f2090c.js';

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); 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 = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
var DEFAULT_KEYS = {
  label: "label",
  key: "key",
  children: "children"
};
function useMultiple(props, context) {
  var _toRefs = toRefs(props),
      inputValue = _toRefs.inputValue;

  var classPrefix = usePrefixClass();
  var instance = getCurrentInstance();
  var tagInputRef = ref();

  var _useDefaultValue = useDefaultValue(inputValue, props.defaultInputValue, props.onInputChange, "inputValue", "input-change"),
      _useDefaultValue2 = _slicedToArray(_useDefaultValue, 2),
      tInputValue = _useDefaultValue2[0],
      setTInputValue = _useDefaultValue2[1];

  var iKeys = computed(function () {
    return _objectSpread(_objectSpread({}, DEFAULT_KEYS), props.keys);
  });
  var tags = computed(function () {
    if (!(props.value instanceof Array)) {
      return isObject(props.value) ? [props.value[iKeys.value.label]] : [props.value];
    }

    return props.value.map(function (item) {
      return isObject(item) ? item[iKeys.value.label] : item;
    });
  });
  var tPlaceholder = computed(function () {
    return !tags.value || !tags.value.length ? props.placeholder : "";
  });

  var onTagInputChange = function onTagInputChange(val, context2) {
    var _props$onTagChange;

    if (context2.trigger === "tag-remove") {
      var _context2$e;

      (_context2$e = context2.e) === null || _context2$e === void 0 ? void 0 : _context2$e.stopPropagation();
    }

    (_props$onTagChange = props.onTagChange) === null || _props$onTagChange === void 0 ? void 0 : _props$onTagChange.call(props, val, context2);
    instance.emit("tag-change", val, context2);
  };

  var renderSelectMultiple = function renderSelectMultiple(p, h) {
    var tagInputProps = _objectSpread(_objectSpread(_objectSpread({}, props.tagInputProps), p.commonInputProps), {}, {
      tagProps: props.tagProps,
      label: props.label,
      autoWidth: props.autoWidth,
      readonly: props.readonly,
      placeholder: tPlaceholder.value,
      minCollapsedNum: props.minCollapsedNum,
      collapsedItems: props.collapsedItems,
      tag: props.tag,
      valueDisplay: props.valueDisplay,
      value: tags.value,
      inputValue: tInputValue.value || "",
      inputProps: _objectSpread({
        readonly: !props.allowInput || props.readonly,
        inputClass: _defineProperty({}, "".concat(classPrefix.value, "-input--focused"), p.popupVisible)
      }, props.inputProps),
      suffixIcon: !props.disabled && props.loading ? function () {
        return h("l-loading", {
          "attrs": {
            "loading": true,
            "size": "small"
          }
        });
      } : props.suffixIcon
    });

    return h("l-tag-input", helper([{
      "ref": "tagInputRef",
      "scopedSlots": context.slots
    }, {
      "props": tagInputProps
    }, {}, {
      "on": {
        "input-change": function inputChange(val, context2) {
          if ((context2 === null || context2 === void 0 ? void 0 : context2.trigger) === "enter") return;
          setTInputValue(val, {
            trigger: context2.trigger,
            e: context2.e
          });
        }
      }
    }, {
      "on": {
        "change": onTagInputChange,
        "clear": p.onInnerClear,
        "blur": function blur(val, context2) {
          var _props$onBlur;

          (_props$onBlur = props.onBlur) === null || _props$onBlur === void 0 ? void 0 : _props$onBlur.call(props, props.value, _objectSpread(_objectSpread({}, context2), {}, {
            tagInputValue: val
          }));
          instance.emit("blur", props.value, _objectSpread(_objectSpread({}, context2), {}, {
            tagInputValue: val
          }));
        },
        "focus": function focus(val, context2) {
          var _props$onFocus;

          (_props$onFocus = props.onFocus) === null || _props$onFocus === void 0 ? void 0 : _props$onFocus.call(props, props.value, _objectSpread(_objectSpread({}, context2), {}, {
            tagInputValue: val
          }));
          instance.emit("focus", props.value, _objectSpread(_objectSpread({}, context2), {}, {
            tagInputValue: val
          }));
        }
      }
    }]));
  };

  return {
    tags: tags,
    tPlaceholder: tPlaceholder,
    tagInputRef: tagInputRef,
    renderSelectMultiple: renderSelectMultiple
  };
}

export { useMultiple as default };
//# sourceMappingURL=use-multiple.js.map
