'use strict';

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

var vue = require('vue');
var lodashEs = require('lodash-es');
require('../../../utils/index.js');
var selectGroup = require('./select-group.js');
var createNamespace = require('../../../utils/create-namespace.js');
var data = require('../../../utils/data.js');

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

exports.default = _sfc_main;
//# sourceMappingURL=select-group.vue2.js.map
