'use strict';

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

var vue = require('vue');
var checkableTag = require('./checkable-tag.js');
require('../../../utils/index.js');
var createNamespace = require('../../../utils/create-namespace.js');

"use strict";
var _sfc_main = /* @__PURE__ */ vue.defineComponent({
  ...{ name: "LlCheckableTag" },
  __name: "checkable-tag copy",
  props: checkableTag.checkableTagProps,
  emits: ["update:value", "change"],
  setup(__props, { emit: __emit }) {
    const props = __props;
    const emits = __emit;
    const bem = createNamespace.createNamespace("checkable-tag");
    const inputRef = vue.ref();
    const inputValue = vue.ref("");
    const inputVisible = vue.ref(false);
    const newTags = vue.ref([]);
    const newValue = vue.computed({
      get() {
        return Array.isArray(props.value) ? props.value : [];
      },
      set(val) {
        emits("update:value", val);
        emits("change", val);
      }
    });
    const isDisabledAddTag = vue.computed(() => {
      return props.disabled || props.disabledAddTag || props.maxAddableTags && newTags.value.length >= props.maxAddableTags || props.selectionMode === "multiple" && props.addAutoSelect && props.maxChecked && newValue.value.length >= props.maxChecked;
    });
    const showCloseIcon = vue.computed(() => {
      return (idx) => {
        if (props.deleteRule === "all") {
          return true;
        } else if (props.deleteRule === "none") {
          return false;
        } else if (props.deleteRule === "exceptFirstN") {
          return idx >= props.exceptCount;
        }
        return false;
      };
    });
    const handleClose = (tag) => {
      if (props.disabled || props.disabledCloseTag) return;
      newTags.value = newTags.value.filter((i) => i !== tag);
      if (newValue.value.includes(tag)) {
        newValue.value = newValue.value.filter((i) => i !== tag);
      }
    };
    const handleCheck = (tag, checked) => {
      if (props.disabled) return;
      if (props.selectionMode === "single") {
        if (!newValue.value.includes(tag)) {
          newValue.value = [tag];
        }
      } else {
        if (checked && props.maxChecked && newValue.value.length >= props.maxChecked) {
          return;
        }
        newValue.value = checked ? [...newValue.value, tag] : newValue.value.filter((i) => i !== tag);
      }
    };
    const showInput = () => {
      if (isDisabledAddTag.value) return;
      inputVisible.value = true;
      vue.nextTick(() => {
        inputRef.value.focus();
      });
    };
    const handleInputConfirm = () => {
      if (inputValue.value && !newTags.value.includes(inputValue.value)) {
        newTags.value.push(inputValue.value);
        if (props.addAutoSelect) {
          if (props.selectionMode === "single") {
            newValue.value = [inputValue.value];
          } else {
            newValue.value = [...newValue.value, inputValue.value];
          }
        }
      }
      inputVisible.value = false;
      inputValue.value = "";
    };
    vue.watch(
      () => props.selectionMode,
      (val) => {
        if (val === "single") {
          newValue.value = [newValue.value[0]];
        }
      }
    );
    vue.watch(
      () => props.options,
      (val) => {
        newTags.value = val;
      },
      { immediate: true, deep: true }
    );
    vue.watch(
      () => props.value,
      (val) => {
        if (!val || !Array.isArray(val)) return;
        const newTagsSet = new Set(newTags.value);
        const addedTags = [];
        val.forEach((tag) => {
          if (tag && !newTagsSet.has(tag)) {
            addedTags.push(tag);
            newTagsSet.add(tag);
          }
        });
        if (addedTags.length > 0) {
          newTags.value = [...newTags.value, ...addedTags];
        }
      },
      { immediate: true, deep: true }
    );
    return (_ctx, _cache) => {
      const _component_ll_icon = vue.resolveComponent("ll-icon");
      const _component_a_checkable_tag = vue.resolveComponent("a-checkable-tag");
      const _component_ll_tag = vue.resolveComponent("ll-tag");
      const _component_ll_input = vue.resolveComponent("ll-input");
      return vue.openBlock(), vue.createElementBlock(
        "div",
        {
          style: vue.normalizeStyle({ gap: `${_ctx.tagGap}px` }),
          class: vue.normalizeClass([vue.unref(bem).b(), vue.unref(bem).is(_ctx.size), vue.unref(bem).is(_ctx.disabled ? "disabled" : "")])
        },
        [
          (vue.openBlock(true), vue.createElementBlock(
            vue.Fragment,
            null,
            vue.renderList(newTags.value, (tag, idx) => {
              return vue.openBlock(), vue.createBlock(_component_a_checkable_tag, {
                key: tag,
                style: vue.normalizeStyle(_ctx.tagMaxWidth ? { maxWidth: `${_ctx.tagMaxWidth}px` } : {}),
                class: vue.normalizeClass([
                  vue.unref(bem).e("tag-content"),
                  vue.unref(bem).is(newValue.value.indexOf(tag) > -1 ? "checked" : "")
                ]),
                checked: newValue.value.indexOf(tag) > -1,
                onChange: (checked) => handleCheck(tag, checked)
              }, {
                default: vue.withCtx(() => [
                  vue.createElementVNode(
                    "div",
                    {
                      class: vue.normalizeClass(vue.unref(bem).e("tag"))
                    },
                    [
                      vue.createElementVNode(
                        "span",
                        null,
                        vue.toDisplayString(tag),
                        1
                        /* TEXT */
                      ),
                      showCloseIcon.value(idx) ? (vue.openBlock(), vue.createBlock(_component_ll_icon, {
                        key: 0,
                        "icon-name": "icon-close2",
                        onClick: ($event) => handleClose(tag)
                      }, null, 8, ["onClick"])) : vue.createCommentVNode("v-if", true)
                    ],
                    2
                    /* CLASS */
                  )
                ]),
                _: 2
                /* DYNAMIC */
              }, 1032, ["style", "class", "checked", "onChange"]);
            }),
            128
            /* KEYED_FRAGMENT */
          )),
          !_ctx.enableAdd && newTags.value.length === 0 ? (vue.openBlock(), vue.createBlock(_component_ll_tag, {
            key: 0,
            shape: "round"
          }, {
            default: vue.withCtx(() => [
              vue.createTextVNode(
                vue.toDisplayString(_ctx.emptyText || "\u6682\u65E0\u6807\u7B7E"),
                1
                /* TEXT */
              )
            ]),
            _: 1
            /* STABLE */
          })) : vue.createCommentVNode("v-if", true),
          vue.createCommentVNode(" \u65B0\u589E\u6807\u7B7E "),
          _ctx.enableAdd ? (vue.openBlock(), vue.createElementBlock(
            vue.Fragment,
            { key: 1 },
            [
              !inputVisible.value ? (vue.openBlock(), vue.createBlock(_component_ll_tag, {
                key: 0,
                shape: "round",
                icon: {
                  iconName: "icon-add"
                },
                class: vue.normalizeClass([vue.unref(bem).e("add-tag"), vue.unref(bem).is(isDisabledAddTag.value ? "disabled" : "")]),
                onClick: showInput
              }, {
                default: vue.withCtx(() => [
                  vue.createTextVNode(
                    vue.toDisplayString(_ctx.addTagText),
                    1
                    /* TEXT */
                  )
                ]),
                _: 1
                /* STABLE */
              }, 8, ["class"])) : (vue.openBlock(), vue.createBlock(_component_ll_input, {
                key: 1,
                ref_key: "inputRef",
                ref: inputRef,
                value: inputValue.value,
                "onUpdate:value": _cache[0] || (_cache[0] = ($event) => inputValue.value = $event),
                valueModifiers: { trim: true },
                placeholder: _ctx.placeholder,
                size: "small",
                style: vue.normalizeStyle({ width: `${_ctx.inputWidth}px` }),
                class: vue.normalizeClass([vue.unref(bem).e("input")]),
                onBlur: handleInputConfirm,
                onKeyup: vue.withKeys(handleInputConfirm, ["enter"])
              }, null, 8, ["value", "placeholder", "style", "class"]))
            ],
            64
            /* STABLE_FRAGMENT */
          )) : vue.createCommentVNode("v-if", true)
        ],
        6
        /* CLASS, STYLE */
      );
    };
  }
});

exports.default = _sfc_main;
//# sourceMappingURL=checkable-tag copy.vue2.js.map
