import { defineComponent, ref, computed, watch, resolveComponent, createElementBlock, openBlock, normalizeClass, unref, createElementVNode, createVNode, withCtx, createCommentVNode, createBlock, renderSlot, normalizeStyle, Fragment, renderList, createTextVNode, toDisplayString, mergeProps, normalizeProps } from 'vue';
import { has } from 'lodash-es';
import '../../../utils/index.mjs';
import { LEFT_SELECT_MIN_WIDTH, selectGroupEmits, selectGroupProps } from './select-group.mjs';
import { createNamespace } from '../../../utils/create-namespace.mjs';
import { hasValue } from '../../../utils/data.mjs';

"use strict";
var _sfc_main = /* @__PURE__ */ defineComponent({
  ...{ name: "LlSelectGroup" },
  __name: "select-group",
  props: selectGroupProps,
  emits: selectGroupEmits,
  setup(__props, { expose: __expose, emit: __emit }) {
    const props = __props;
    const emits = __emit;
    const selectType = ref();
    const value = ref("");
    const newList = ref([]);
    const bem = createNamespace("select-group");
    const bemContainer = createNamespace("select-group-container");
    const selectItem = computed(() => {
      return newList.value.find((e) => e.value === selectType.value);
    });
    const leftSelectWidth = computed(() => {
      const names = newList.value.map((item) => item.name.length);
      const maxNameLength = Math.max(...names);
      if (maxNameLength <= 3) {
        return LEFT_SELECT_MIN_WIDTH + "px";
      }
      const width = LEFT_SELECT_MIN_WIDTH + (maxNameLength - 3) * 14;
      return width + "px";
    });
    const handleSelectChange = (_, options) => {
      const item = options.item;
      const formType = item.showFormType;
      if (!formType || formType === "input" || formType === "treeSelect") {
        value.value = "";
      } else if (formType === "range-picker") {
        value.value = [void 0, void 0];
      } else if (formType === "select") {
        value.value = item.noFirstAddAll ? void 0 : -1;
      }
      selectType.value = _;
      emits("onSelectChange", _, item);
    };
    const handleChange = () => {
      emits(
        "change",
        selectItem.value?.value,
        value.value === -1 ? null : value.value
      );
    };
    const setSelectGroup = (selectValue, currentValue) => {
      selectType.value = selectValue;
      value.value = currentValue;
    };
    watch(
      () => props.data,
      (newData) => {
        let list = [];
        if (newData && newData.length) {
          list = newData.map((item) => {
            const newItem = { ...item };
            if (newItem.showFormType === "select") {
              if (has(newItem, "options") && !newItem.noFirstAddAll) {
                newItem.options = [
                  {
                    label: "\u5168\u90E8",
                    value: -1
                  },
                  ...newItem.options
                ];
              } else if (has(newItem, "list") && !newItem.noFirstAddAll) {
                newItem.list = [
                  {
                    name: "\u5168\u90E8",
                    value: -1
                  },
                  ...newItem.list
                ];
              }
            }
            return newItem;
          });
          newList.value = list;
          if (newList.value.length && !props.selectValue) {
            selectType.value = newList.value[0].value;
          }
        }
      },
      {
        deep: true,
        immediate: true
      }
    );
    watch(
      () => [props.value, props.selectValue],
      ([currentValue, selectValue]) => {
        selectType.value = selectValue ? selectValue : newList.value.length ? newList.value[0].value : "";
        if (selectType.value) {
          const find = newList.value.find((e) => e.value === selectType.value);
          if (find && find.showFormType === "select" && !find.noFirstAddAll) {
            value.value = !hasValue(currentValue) ? -1 : currentValue;
          } else {
            value.value = currentValue;
          }
        }
      },
      {
        immediate: true
      }
    );
    __expose({ setSelectGroup, selectType, value });
    return (_ctx, _cache) => {
      const _component_a_select_option = resolveComponent("a-select-option");
      const _component_a_select = resolveComponent("a-select");
      const _component_ll_icon = resolveComponent("ll-icon");
      const _component_a_input = resolveComponent("a-input");
      const _component_a_range_picker = resolveComponent("a-range-picker");
      const _component_a_tree_select = resolveComponent("a-tree-select");
      const _component_a_input_group = resolveComponent("a-input-group");
      return openBlock(), createElementBlock(
        "div",
        {
          class: normalizeClass(unref(bem).b())
        },
        [
          createElementVNode(
            "div",
            {
              class: normalizeClass(unref(bemContainer).b())
            },
            [
              createVNode(_component_a_input_group, { compact: "" }, {
                default: withCtx(() => [
                  createCommentVNode(" \u5DE6\u4FA7select "),
                  createVNode(_component_a_select, {
                    value: selectType.value,
                    "onUpdate:value": _cache[0] || (_cache[0] = ($event) => selectType.value = $event),
                    class: "select-group-left-select-container",
                    style: normalizeStyle({ minWidth: leftSelectWidth.value }),
                    onChange: handleSelectChange
                  }, {
                    default: withCtx(() => [
                      (openBlock(true), createElementBlock(
                        Fragment,
                        null,
                        renderList(newList.value, (item) => {
                          return openBlock(), createBlock(_component_a_select_option, {
                            key: item.name,
                            value: item.value,
                            item
                          }, {
                            default: withCtx(() => [
                              createTextVNode(
                                toDisplayString(item.name),
                                1
                                /* TEXT */
                              )
                            ]),
                            _: 2
                            /* DYNAMIC */
                          }, 1032, ["value", "item"]);
                        }),
                        128
                        /* KEYED_FRAGMENT */
                      ))
                    ]),
                    _: 1
                    /* STABLE */
                  }, 8, ["value", "style"]),
                  createCommentVNode(" \u53F3\u4FA7\u7EC4\u5408\u8868\u5355 "),
                  createCommentVNode(" input\u8F93\u5165\u6846 "),
                  !selectItem.value?.showFormType || selectItem.value?.showFormType === "input" ? (openBlock(), createBlock(_component_a_input, {
                    key: 0,
                    value: value.value,
                    "onUpdate:value": _cache[1] || (_cache[1] = ($event) => value.value = $event),
                    class: normalizeClass(unref(bem).e("right-input")),
                    placeholder: selectItem.value?.placeholder,
                    "allow-clear": "",
                    onPressEnter: handleChange
                  }, {
                    suffix: withCtx(() => [
                      createVNode(_component_ll_icon, {
                        "icon-name": `icon-search`,
                        onClick: handleChange
                      })
                    ]),
                    _: 1
                    /* STABLE */
                  }, 8, ["value", "class", "placeholder"])) : selectItem.value?.showFormType === "select" ? (openBlock(), createElementBlock(
                    Fragment,
                    { key: 1 },
                    [
                      createCommentVNode(" select\u4E0B\u62C9\u6846 "),
                      createVNode(_component_a_select, mergeProps(selectItem.value, {
                        value: value.value,
                        "onUpdate:value": _cache[2] || (_cache[2] = ($event) => value.value = $event),
                        class: unref(bem).e("right-select"),
                        "popup-class-name": "select-group-right-select",
                        onChange: handleChange
                      }), {
                        default: withCtx(() => [
                          (openBlock(true), createElementBlock(
                            Fragment,
                            null,
                            renderList(selectItem.value.list, (item) => {
                              return openBlock(), createBlock(_component_a_select_option, {
                                key: item.value,
                                value: item.value
                              }, {
                                default: withCtx(() => [
                                  createTextVNode(
                                    toDisplayString(item.name),
                                    1
                                    /* TEXT */
                                  )
                                ]),
                                _: 2
                                /* DYNAMIC */
                              }, 1032, ["value"]);
                            }),
                            128
                            /* KEYED_FRAGMENT */
                          ))
                        ]),
                        _: 1
                        /* STABLE */
                      }, 16, ["value", "class"])
                    ],
                    64
                    /* STABLE_FRAGMENT */
                  )) : selectItem.value?.showFormType === "range-picker" ? (openBlock(), createElementBlock(
                    Fragment,
                    { key: 2 },
                    [
                      createCommentVNode(" \u65E5\u671F\u9009\u62E9 "),
                      createVNode(_component_a_range_picker, {
                        value: value.value,
                        "onUpdate:value": _cache[3] || (_cache[3] = ($event) => value.value = $event),
                        class: normalizeClass(unref(bem).e("right-range-picker")),
                        onChange: handleChange
                      }, null, 8, ["value", "class"])
                    ],
                    64
                    /* STABLE_FRAGMENT */
                  )) : selectItem.value?.showFormType === "treeSelect" ? (openBlock(), createElementBlock(
                    Fragment,
                    { key: 3 },
                    [
                      createCommentVNode(" \u6811\u9009\u62E9 "),
                      createVNode(_component_a_tree_select, {
                        value: value.value,
                        "onUpdate:value": _cache[4] || (_cache[4] = ($event) => value.value = $event),
                        class: normalizeClass(unref(bem).e("right-tree-select")),
                        "field-names": selectItem.value.fieldNames,
                        placeholder: selectItem.value?.placeholder,
                        "tree-data": selectItem.value.list,
                        onSelect: handleChange
                      }, null, 8, ["value", "class", "field-names", "placeholder", "tree-data"])
                    ],
                    64
                    /* STABLE_FRAGMENT */
                  )) : renderSlot(_ctx.$slots, "extra", normalizeProps(mergeProps({ key: 4 }, selectItem.value)))
                ]),
                _: 3
                /* FORWARDED */
              })
            ],
            2
            /* CLASS */
          )
        ],
        2
        /* CLASS */
      );
    };
  }
});

export { _sfc_main as default };
//# sourceMappingURL=select-group.vue2.mjs.map
