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

'use strict';

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

var helper = require('../../../chunks/helper.js');
var VueCompositionAPI = require('@vue/composition-api');
var isObject = require('lodash/isObject');
var components_tagInput_index = require('../../tag-input/index.js');
var components_loading_index = require('../../loading/index.js');
var hooks_useDefaultValue = require('../../../hooks/use-default-value.js');
var hooks_useConfig = require('../../../hooks/use-config.js');
require('../../../utils/with-install.js');
require('vue');
require('lodash');
require('../../../config.js');
require('../../tag-input/tag-input.js');
require('@ldesign/icons-vue');
require('../../input/index.js');
require('../../../utils/map-props.js');
require('../../input/input.js');
require('lodash/camelCase');
require('lodash/kebabCase');
require('../../../chunks/helper2.js');
require('../../../chunks/log.js');
require('../../../chunks/_commonjsHelpers-2e6815bd.js');
require('../../../utils/helper.js');
require('../../config-provider/config-receiver.js');
require('lodash/mergeWith');
require('../../config-provider/context.js');
require('../../../chunks/zh_CN.js');
require('../../../utils/mixins.js');
require('../../../utils/event.js');
require('../../../utils/render-node.js');
require('../../input/props.js');
require('../../input/group.js');
require('../../../hooks/use-global-icon.js');
require('../../config-provider/useConfig.js');
require('../../tag-input/props.js');
require('../../tag-input/hooks/use-tag-scroll.js');
require('../../tag-input/hooks/use-tag-list.js');
require('../../tag/index.js');
require('../../tag/tag.js');
require('../../tag/props.js');
require('../../tag/check-tag.js');
require('../../../hooks/use-vmodel.js');
require('../../../hooks/lnode.js');
require('lodash/isEmpty');
require('lodash/isFunction');
require('../../../hooks/render-lnode.js');
require('../../tag-input/hooks/use-hover.js');
require('../../tag-input/hooks/use-drag-sorter.js');
require('../../loading/loading.js');
require('../../../utils/dom.js');
require('raf');
require('lodash/isString');
require('../../../utils/easing.js');
require('../../../utils/transfer-dom.js');
require('../../loading/props.js');
require('../../loading/icon/gradient.js');
require('../../../chunks/defineProperty-838e4192.js');
require('../../../chunks/set-style.js');
require('../../loading/plugin.js');

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

var isObject__default = /*#__PURE__*/_interopDefaultLegacy(isObject);

const DEFAULT_KEYS = {
  label: "label",
  key: "key",
  children: "children"
};
function useMultiple(props, context) {
  const {
    inputValue
  } = VueCompositionAPI.toRefs(props);
  const classPrefix = hooks_useConfig.usePrefixClass();
  const instance = VueCompositionAPI.getCurrentInstance();
  const tagInputRef = VueCompositionAPI.ref();
  const [tInputValue, setTInputValue] = hooks_useDefaultValue["default"](inputValue, props.defaultInputValue, props.onInputChange, "inputValue", "input-change");
  const iKeys = VueCompositionAPI.computed(() => ({
    ...DEFAULT_KEYS,
    ...props.keys
  }));
  const tags = VueCompositionAPI.computed(() => {
    if (!Array.isArray(props.value)) return isObject__default["default"](props.value) ? [props.value[iKeys.value.label]] : [props.value];
    return props.value.map(item => isObject__default["default"](item) ? item[iKeys.value.label] : item);
  });
  const tPlaceholder = VueCompositionAPI.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(components_loading_index.Loading, {
        "attrs": {
          "loading": true,
          "size": "small"
        }
      }) : props.suffixIcon
    };
    return h(components_tagInput_index.TagInput, helper.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
  };
}

exports["default"] = useMultiple;
//# sourceMappingURL=use-multiple.js.map
