import { defineComponent, ref, computed, nextTick, watch, resolveComponent, createElementBlock, openBlock, normalizeClass, unref, normalizeStyle, createBlock, createCommentVNode, Fragment, renderList, withCtx, createElementVNode, toDisplayString, createTextVNode, withKeys } from 'vue';
import { checkableTagProps } from './checkable-tag.mjs';
import '../../../utils/index.mjs';
import { createNamespace } from '../../../utils/create-namespace.mjs';

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

export { _sfc_main as default };
//# sourceMappingURL=checkable-tag copy.vue2.mjs.map
