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

import { h as helper } from '../../../chunks/helper.js';
import { toRefs, getCurrentInstance, ref, computed } from '@vue/composition-api';
import isObject from 'lodash/isObject';
import { TagInput } from '../../tag-input/index.js';
import { Loading } from '../../loading/index.js';
import useDefaultValue from '../../../hooks/use-default-value.js';
import { usePrefixClass } from '../../../hooks/use-config.js';
import '../../../chunks/_commonjsHelpers.js';
import '../../../utils/with-install.js';
import 'vue';
import 'lodash';
import '../../../config.js';
import '../../tag-input/tag-input.js';
import '@ldesign/icons-vue';
import '../../input/index.js';
import '../../../utils/map-props.js';
import '../../input/input.js';
import 'lodash/camelCase';
import 'lodash/kebabCase';
import '../../../chunks/helper2.js';
import '../../../chunks/defineProperty-838e4192.js';
import '../../../chunks/index.js';
import '../../../chunks/log.js';
import '../../../chunks/_commonjsHelpers-2e6815bd.js';
import '../../../utils/helper.js';
import '../../config-provider/config-receiver.js';
import 'lodash/mergeWith';
import '../../config-provider/context.js';
import '../../../chunks/zh_CN.js';
import '../../config-provider/type.js';
import '../../../utils/mixins.js';
import '../../../utils/event.js';
import '../../../utils/render-node.js';
import '../../input/props.js';
import '../../input/group.js';
import '../../input/style';
import '../../input/type.js';
import '../../../hooks/use-global-icon.js';
import '../../config-provider/useConfig.js';
import '../../tag-input/props.js';
import '../../tag-input/hooks/use-tag-scroll.js';
import '../../tag-input/hooks/use-tag-list.js';
import '../../tag/index.js';
import '../../tag/tag.js';
import '../../tag/props.js';
import '../../tag/check-tag.js';
import '../../tag/style';
import '../../tag/type.js';
import '../../../hooks/use-vmodel.js';
import '../../../hooks/lnode.js';
import 'lodash/isEmpty';
import 'lodash/isFunction';
import '../../../hooks/render-lnode.js';
import '../../tag-input/hooks/use-hover.js';
import '../../tag-input/hooks/use-drag-sorter.js';
import '../../tag-input/style';
import '../../tag-input/type.js';
import '../../loading/loading.js';
import '../../../utils/dom.js';
import 'raf';
import 'lodash/isString';
import '../../../utils/easing.js';
import '../../../utils/transfer-dom.js';
import '../../loading/props.js';
import '../../loading/icon/gradient.js';
import '../../../chunks/set-style.js';
import '../../loading/style';
import '../../loading/type.js';
import '../../loading/plugin.js';

const DEFAULT_KEYS = {
  label: "label",
  key: "key",
  children: "children"
};
function useMultiple(props, context) {
  const {
    inputValue
  } = toRefs(props);
  const classPrefix = usePrefixClass();
  const instance = getCurrentInstance();
  const tagInputRef = ref();
  const [tInputValue, setTInputValue] = useDefaultValue(inputValue, props.defaultInputValue, props.onInputChange, "inputValue", "input-change");
  const iKeys = computed(() => ({
    ...DEFAULT_KEYS,
    ...props.keys
  }));
  const tags = computed(() => {
    if (!Array.isArray(props.value)) return isObject(props.value) ? [props.value[iKeys.value.label]] : [props.value];
    return props.value.map(item => isObject(item) ? item[iKeys.value.label] : item);
  });
  const tPlaceholder = computed(() => !tags.value || !tags.value.length ? props.placeholder : "");
  const onTagInputChange = (val, context2) => {
    var _context2$e, _props$onTagChange;
    if (context2.trigger === "tag-remove") (_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);
  };
  const renderSelectMultiple = (p, h) => {
    const tagInputProps = {
      ...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: {
        readonly: !props.allowInput || props.readonly,
        inputClass: {
          [`${classPrefix.value}-input--focused`]: p.popupVisible
        },
        ...props.inputProps
      },
      suffixIcon: !props.disabled && props.loading ? () => h(Loading, {
        "attrs": {
          "loading": true,
          "size": "small"
        }
      }) : props.suffixIcon
    };
    return h(TagInput, helper([{
      "ref": "tagInputRef",
      "scopedSlots": context.slots
    }, {
      "props": tagInputProps
    }, {}, {
      "on": {
        "input-change": (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": (val, context2) => {
          var _props$onBlur;
          (_props$onBlur = props.onBlur) === null || _props$onBlur === void 0 ? void 0 : _props$onBlur.call(props, props.value, {
            ...context2,
            tagInputValue: val
          });
          instance.emit("blur", props.value, {
            ...context2,
            tagInputValue: val
          });
        },
        "focus": (val, context2) => {
          var _props$onFocus;
          (_props$onFocus = props.onFocus) === null || _props$onFocus === void 0 ? void 0 : _props$onFocus.call(props, props.value, {
            ...context2,
            tagInputValue: val
          });
          instance.emit("focus", props.value, {
            ...context2,
            tagInputValue: val
          });
        }
      }
    }]));
  };
  return {
    tags,
    tPlaceholder,
    tagInputRef,
    renderSelectMultiple
  };
}

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