'use strict';

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

var vue = require('vue');
require('../../../utils/index.js');
var lodashEs = require('lodash-es');
var config = require('./config.js');
var customSearcher = require('./custom-searcher.js');
require('./scheme-name.vue.js');
var createNamespace = require('../../../utils/create-namespace.js');
var randomCode = require('../../../utils/random-code.js');
var schemeName_vue_vue_type_script_setup_true_lang = require('./scheme-name.vue2.js');

"use strict";
const _hoisted_1 = {
  key: 1,
  style: { minWidth: "250px", flex: 1 }
};
var _sfc_main = /* @__PURE__ */ vue.defineComponent({
  ...{ name: "LlCustomSearcher" },
  __name: "custom-searcher",
  props: customSearcher.customSearcherProps,
  emits: ["update:open", "field-select"],
  setup(__props, { expose: __expose, emit: __emit }) {
    const props = __props;
    const emits = __emit;
    const bem = createNamespace.createNamespace("custom-searcher");
    const attrs = vue.useAttrs();
    const searchList = vue.reactive([
      { key: null, operator: null, value: void 0 }
    ]);
    const localList = vue.computed(() => [...props.fieldList]);
    const storageVersion = vue.ref(0);
    const nameOpen = vue.ref(false);
    const localScheme = vue.computed({
      get: () => {
        storageVersion.value;
        if (props.useLocalStorage && props.localKey) {
          try {
            const item = localStorage.getItem(props.localKey);
            return item ? JSON.parse(item) : [];
          } catch {
            return [];
          }
        } else {
          return [];
        }
      },
      set: (value) => {
        storageVersion.value;
        if (props.useLocalStorage && props.localKey) {
          try {
            return localStorage.setItem(props.localKey, JSON.stringify(value));
          } catch (error) {
            return console.error("\u4FDD\u5B58\u5230\u672C\u5730\u5B58\u50A8\u5931\u8D25:", error);
          }
        }
      }
    });
    const newOpen = vue.computed({
      get() {
        return nameOpen.value || props.open;
      },
      set(o) {
        if (!nameOpen.value) {
          emits("update:open", o);
        }
      }
    });
    const disabledAdd = vue.computed(() => {
      return localList.value.length === searchList.length || props.maxSearchCount > 0 && props.maxSearchCount <= searchList.length;
    });
    const configMap = vue.computed(() => {
      const map = {};
      localList.value.forEach((fieldConfig) => {
        map[fieldConfig.key] = fieldConfig;
      });
      return map;
    });
    const handleAdd = () => {
      searchList.push({ key: null, operator: null, value: void 0 });
    };
    const handleCancel = () => {
      setTimeout(() => {
        nameOpen.value = false;
      }, 100);
    };
    const handleOk = (name) => {
      setTimeout(() => {
        nameOpen.value = false;
      }, 100);
      localScheme.value = [
        ...localScheme.value,
        {
          key: randomCode.getRandomCode(),
          name,
          value: searchList.filter((item) => item.key)
        }
      ];
      storageVersion.value++;
    };
    const useScheme = (idx) => {
      searchList.length = 0;
      searchList.push(...lodashEs.cloneDeep(localScheme.value[idx].value));
      filterSearchList();
    };
    const clearScheme = (idx) => {
      const copyArr = lodashEs.cloneDeep(localScheme.value);
      copyArr.splice(idx, 1);
      localScheme.value = [...copyArr];
      storageVersion.value++;
    };
    const handleRemove = (idx) => {
      searchList.splice(idx, 1);
    };
    const handleField = (value, option) => {
      const target = searchList.find((item) => item.key === value);
      if (target) {
        if (!props.operatorSelectable) {
          target.operator = option.conditionType || null;
        }
        if (option?.props?.value) {
          target.value = option.props.value;
        } else {
          target.value = void 0;
        }
      }
      emits("field-select", value, option);
    };
    const handleOperator = (value, item) => {
      if (item.operator === "isEmpty" || item.operator === "isNotEmpty" || item.operator === "isNull" || item.operator === "isNotNull" || item.operator === "isNullOrEmpty") {
        item.value = void 0;
      }
    };
    const getValue = () => {
      return searchList;
    };
    const clearValue = (range, fields) => {
      if (range === "field") {
        if (fields && fields?.length > 0) {
          for (let i = searchList.length - 1; i >= 0; i--) {
            if (fields.includes(searchList[i].key)) {
              searchList.splice(i, 1);
            }
          }
        }
      } else {
        searchList.length = 0;
      }
      if (searchList.length === 0) {
        searchList.push({ key: null, operator: null, value: void 0 });
      }
    };
    const setValue = (config) => {
      Object.entries(config).forEach(([key, updates]) => {
        const targetItem = searchList.find((item) => item.key === key);
        if (targetItem && updates) {
          Object.assign(targetItem, updates);
        }
      });
    };
    const handleConfig = (config) => {
      Object.entries(config).forEach(([key, updates]) => {
        const targetObj = localList.value.find((i) => i.key === key);
        if (targetObj && updates) {
          Object.assign(targetObj.props, updates);
        }
      });
    };
    const filterSearchList = () => {
      const validFields = localList.value.map((item) => item.key).filter(Boolean);
      for (let i = searchList.length - 1; i >= 0; i--) {
        if (!validFields.includes(searchList[i].key && searchList[i].key)) {
          searchList.splice(i, 1);
        }
      }
      if (searchList.length === 0) {
        searchList.push({ key: null, operator: null, value: void 0 });
      }
    };
    vue.watch(() => localList.value, filterSearchList, { deep: true });
    vue.watch(
      () => props.operatorSelectable,
      () => {
        searchList.length = 0;
        searchList.push({ key: null, operator: null, value: void 0 });
      }
    );
    __expose({ getValue, clearValue, setValue, handleConfig });
    vue.onMounted(() => {
      nameOpen.value = false;
    });
    vue.onUnmounted(() => {
      if (props.localKey && localScheme.value.length === 0) {
        localStorage.removeItem(props.localKey);
      }
    });
    return (_ctx, _cache) => {
      const _component_ll_icon_base_pro = vue.resolveComponent("ll-icon-base-pro");
      const _component_a_badge = vue.resolveComponent("a-badge");
      const _component_ll_select = vue.resolveComponent("ll-select");
      const _component_ll_input = vue.resolveComponent("ll-input");
      const _component_ll_icon = vue.resolveComponent("ll-icon");
      const _component_ll_button = vue.resolveComponent("ll-button");
      const _component_ll_tag = vue.resolveComponent("ll-tag");
      const _component_a_form_item_rest = vue.resolveComponent("a-form-item-rest");
      const _component_a_popover = vue.resolveComponent("a-popover");
      return vue.openBlock(), vue.createElementBlock(
        vue.Fragment,
        null,
        [
          vue.createVNode(_component_a_popover, vue.mergeProps(vue.unref(attrs), {
            open: newOpen.value,
            "onUpdate:open": _cache[1] || (_cache[1] = ($event) => newOpen.value = $event),
            arrow: false,
            trigger: "click",
            placement: _ctx.placement,
            "overlay-class-name": `${vue.unref(bem).b()}`
          }), {
            content: vue.withCtx(() => [
              vue.createVNode(_component_a_form_item_rest, null, {
                default: vue.withCtx(() => [
                  vue.createElementVNode(
                    "div",
                    {
                      class: vue.normalizeClass(vue.unref(bem).e("content"))
                    },
                    [
                      (vue.openBlock(true), vue.createElementBlock(
                        vue.Fragment,
                        null,
                        vue.renderList(searchList, (item, idx) => {
                          return vue.openBlock(), vue.createElementBlock(
                            "div",
                            {
                              key: idx,
                              class: vue.normalizeClass(vue.unref(bem).e("list-item"))
                            },
                            [
                              vue.createCommentVNode(" \u5B57\u6BB5\u540D "),
                              vue.createVNode(_component_ll_select, {
                                value: item.key,
                                "onUpdate:value": ($event) => item.key = $event,
                                style: { "width": "150px" },
                                placeholder: "\u8BF7\u9009\u62E9\u5B57\u6BB5",
                                size: "small",
                                "field-names": {
                                  label: "title",
                                  value: "key",
                                  options: "options"
                                },
                                options: localList.value.map((i) => ({
                                  ...i,
                                  disabled: searchList.some((y) => y.key === i.key)
                                })),
                                onSelect: handleField
                              }, null, 8, ["value", "onUpdate:value", "options"]),
                              vue.createCommentVNode(" \u8FD0\u7B97\u7B26 "),
                              vue.createVNode(_component_ll_select, {
                                value: item.operator,
                                "onUpdate:value": ($event) => item.operator = $event,
                                style: { "width": "150px" },
                                disabled: !_ctx.operatorSelectable,
                                placeholder: "\u8BF7\u9009\u62E9\u8FD0\u7B97\u7B26",
                                options: item.key ? [...vue.unref(config.ops).base, ...vue.unref(config.ops).text, ...vue.unref(config.ops).compare] : [],
                                size: "small",
                                onSelect: (value) => handleOperator(value, item)
                              }, null, 8, ["value", "onUpdate:value", "disabled", "options", "onSelect"]),
                              vue.createCommentVNode(" \u503C "),
                              item.operator !== "isEmpty" && item.operator !== "isNotEmpty" && item.operator !== "isNull" && item.operator !== "isNotNull" && item.operator !== "isNullOrEmpty" ? (vue.openBlock(), vue.createElementBlock(
                                vue.Fragment,
                                { key: 0 },
                                [
                                  !item.key ? (vue.openBlock(), vue.createBlock(_component_ll_input, {
                                    key: 0,
                                    value: item.value,
                                    "onUpdate:value": ($event) => item.value = $event,
                                    placeholder: "\u8BF7\u8F93\u5165",
                                    style: { minWidth: "250px", flex: 1 },
                                    size: "small"
                                  }, null, 8, ["value", "onUpdate:value"])) : (vue.openBlock(), vue.createBlock(vue.resolveDynamicComponent(configMap.value[item.key].type), vue.mergeProps(
                                    {
                                      key: 1,
                                      ref_for: true
                                    },
                                    Object.fromEntries(
                                      Object.entries(configMap.value[item.key].props).filter(
                                        ([key]) => !key.toLowerCase().includes("formcreate")
                                      )
                                    ),
                                    {
                                      value: item.value,
                                      "onUpdate:value": ($event) => item.value = $event,
                                      style: { minWidth: "250px", flex: 1 },
                                      size: "small"
                                    }
                                  ), null, 16, ["value", "onUpdate:value"]))
                                ],
                                64
                                /* STABLE_FRAGMENT */
                              )) : (vue.openBlock(), vue.createElementBlock("div", _hoisted_1)),
                              vue.createCommentVNode(" \u79FB\u9664\u56FE\u6807 "),
                              searchList.length > 1 ? (vue.openBlock(), vue.createBlock(_component_ll_icon, {
                                key: 2,
                                "icon-name": "icon-remove",
                                class: vue.normalizeClass(vue.unref(bem).e("remove-icon")),
                                size: 14,
                                onClick: ($event) => handleRemove(idx)
                              }, null, 8, ["class", "onClick"])) : vue.createCommentVNode("v-if", true)
                            ],
                            2
                            /* CLASS */
                          );
                        }),
                        128
                        /* KEYED_FRAGMENT */
                      ))
                    ],
                    2
                    /* CLASS */
                  ),
                  vue.createElementVNode(
                    "div",
                    {
                      class: vue.normalizeClass(vue.unref(bem).e("operate"))
                    },
                    [
                      vue.createElementVNode(
                        "div",
                        {
                          class: vue.normalizeClass(vue.unref(bem).e("operate-btn"))
                        },
                        [
                          vue.createVNode(_component_ll_button, {
                            disabled: disabledAdd.value,
                            type: "text",
                            size: "small",
                            "icon-space": 8,
                            onClick: handleAdd
                          }, {
                            default: vue.withCtx(() => [
                              vue.createVNode(_component_ll_icon, { "icon-name": "icon-list-add" }),
                              vue.createTextVNode(
                                " " + vue.toDisplayString(_ctx.addSearchText || "\u6DFB\u52A0\u7B5B\u9009\u6761\u4EF6"),
                                1
                                /* TEXT */
                              )
                            ]),
                            _: 1
                            /* STABLE */
                          }, 8, ["disabled"]),
                          _ctx.useLocalStorage && _ctx.localKey ? (vue.openBlock(), vue.createBlock(_component_ll_button, {
                            key: 0,
                            disabled: !searchList.some((item) => item.key),
                            class: vue.normalizeClass(vue.unref(bem).e("operate-btn")),
                            type: "text",
                            size: "small",
                            "icon-space": 8,
                            onClick: _cache[0] || (_cache[0] = ($event) => nameOpen.value = true)
                          }, {
                            default: vue.withCtx(() => [
                              vue.createVNode(_component_ll_icon, { "icon-name": "icon-add" }),
                              vue.createTextVNode(
                                " " + vue.toDisplayString(_ctx.saveSchemeText || "\u4FDD\u5B58\u65B9\u6848"),
                                1
                                /* TEXT */
                              )
                            ]),
                            _: 1
                            /* STABLE */
                          }, 8, ["disabled", "class"])) : vue.createCommentVNode("v-if", true)
                        ],
                        2
                        /* CLASS */
                      ),
                      vue.createCommentVNode(" \u64CD\u4F5C\u533A\u63D2\u69FD "),
                      vue.createElementVNode(
                        "div",
                        {
                          class: vue.normalizeClass(vue.unref(bem).e("operate-slot"))
                        },
                        [
                          vue.renderSlot(_ctx.$slots, "operate")
                        ],
                        2
                        /* CLASS */
                      )
                    ],
                    2
                    /* CLASS */
                  ),
                  vue.createCommentVNode(" \u672C\u5730\u65B9\u6848\u5C55\u793A\u533A "),
                  _ctx.useLocalStorage && _ctx.localKey && localScheme.value.length > 0 ? (vue.openBlock(), vue.createElementBlock(
                    "div",
                    {
                      key: 0,
                      class: vue.normalizeClass(vue.unref(bem).e("local-scheme"))
                    },
                    [
                      (vue.openBlock(true), vue.createElementBlock(
                        vue.Fragment,
                        null,
                        vue.renderList(localScheme.value, (item, idx) => {
                          return vue.openBlock(), vue.createBlock(_component_ll_tag, {
                            key: idx,
                            shape: "round",
                            onClick: ($event) => useScheme(idx)
                          }, {
                            default: vue.withCtx(() => [
                              vue.createTextVNode(
                                vue.toDisplayString(item.name) + " ",
                                1
                                /* TEXT */
                              ),
                              vue.createVNode(_component_ll_icon, {
                                "icon-name": "icon-close2",
                                onClick: vue.withModifiers(($event) => clearScheme(idx), ["stop"])
                              }, null, 8, ["onClick"])
                            ]),
                            _: 2
                            /* DYNAMIC */
                          }, 1032, ["onClick"]);
                        }),
                        128
                        /* KEYED_FRAGMENT */
                      ))
                    ],
                    2
                    /* CLASS */
                  )) : vue.createCommentVNode("v-if", true)
                ]),
                _: 3
                /* FORWARDED */
              })
            ]),
            default: vue.withCtx(() => [
              vue.createVNode(_component_a_badge, {
                dot: _ctx.useBadge && _ctx.activeState,
                offset: [-2, 2]
              }, {
                default: vue.withCtx(() => [
                  vue.createVNode(_component_ll_icon_base_pro, {
                    class: vue.normalizeClass(`${vue.unref(bem).e("icon")} ${vue.unref(bem).is(
                      newOpen.value || _ctx.activeState ? "active" : ""
                    )}`),
                    "show-tip": false,
                    "icon-name": _ctx.searchIcon || "icon-screen",
                    "bg-color": "var(--color-fill-tertiary)",
                    "hover-bg-color": "var(--color-fill-secondary)",
                    size: _ctx.size,
                    label: _ctx.label,
                    "icon-size": 16
                  }, null, 8, ["class", "icon-name", "size", "label"])
                ]),
                _: 1
                /* STABLE */
              }, 8, ["dot"])
            ]),
            _: 3
            /* FORWARDED */
          }, 16, ["open", "placement", "overlay-class-name"]),
          nameOpen.value ? (vue.openBlock(), vue.createBlock(schemeName_vue_vue_type_script_setup_true_lang.default, {
            key: 0,
            open: nameOpen.value,
            "onUpdate:open": _cache[2] || (_cache[2] = ($event) => nameOpen.value = $event),
            onCancel: handleCancel,
            onOk: handleOk
          }, null, 8, ["open"])) : vue.createCommentVNode("v-if", true)
        ],
        64
        /* STABLE_FRAGMENT */
      );
    };
  }
});

exports.default = _sfc_main;
//# sourceMappingURL=custom-searcher.vue2.js.map
