import { defineComponent, useAttrs, ref, computed, watch, resolveComponent, createBlock, openBlock, mergeProps, unref, withCtx, createVNode, normalizeClass, createCommentVNode, createElementVNode, createTextVNode, toDisplayString, renderSlot } from 'vue';
import '../../../utils/index.mjs';
import { customSorterProps } from './custom-sorter.mjs';
import draggable from 'vuedraggable';
import { createNamespace } from '../../../utils/create-namespace.mjs';

"use strict";
var _sfc_main = /* @__PURE__ */ defineComponent({
  ...{ name: "LlCustomSorter" },
  __name: "custom-sorter",
  props: customSorterProps,
  emits: ["update:open", "field-select"],
  setup(__props, { expose: __expose, emit: __emit }) {
    const props = __props;
    const emits = __emit;
    const bem = createNamespace("custom-sorter");
    const attrs = useAttrs();
    const sortList = ref([]);
    const localList = computed(() => [...props.fieldList]);
    const steps = computed(() => [
      {
        title: props.ascText || "\u5347\u5E8F",
        key: "asc",
        value: "asc"
      },
      {
        title: props.descText || "\u964D\u5E8F",
        key: "desc",
        value: "desc"
      }
    ]);
    const newOpen = computed({
      get() {
        return props.open;
      },
      set(o) {
        emits("update:open", o);
      }
    });
    const disabledAdd = computed(() => {
      return localList.value.length === sortList.value.length || props.maxSearchCount > 0 && props.maxSearchCount <= sortList.value.length;
    });
    const handleAdd = () => {
      sortList.value.push({ key: null, orderBy: "asc" });
    };
    const handleRemove = (idx) => {
      sortList.value.splice(idx, 1);
    };
    const handleField = (value, option) => {
      emits("field-select", value, option);
    };
    const getValue = () => {
      return sortList.value;
    };
    const clearValue = (range, fields) => {
      if (range === "field") {
        if (fields && fields?.length > 0) {
          for (let i = sortList.value.length - 1; i >= 0; i--) {
            if (fields.includes(sortList.value[i].key)) {
              sortList.value.splice(i, 1);
            }
          }
        }
      } else {
        sortList.value.length = 0;
      }
    };
    watch(
      () => props.fieldList,
      () => {
        clearValue("all");
      },
      { deep: true }
    );
    __expose({ getValue, clearValue });
    return (_ctx, _cache) => {
      const _component_ll_icon_base_pro = resolveComponent("ll-icon-base-pro");
      const _component_a_badge = resolveComponent("a-badge");
      const _component_ll_icon = resolveComponent("ll-icon");
      const _component_ll_select = resolveComponent("ll-select");
      const _component_ll_segmented = resolveComponent("ll-segmented");
      const _component_ll_empty = resolveComponent("ll-empty");
      const _component_ll_button = resolveComponent("ll-button");
      const _component_a_form_item_rest = resolveComponent("a-form-item-rest");
      const _component_a_popover = resolveComponent("a-popover");
      return openBlock(), createBlock(_component_a_popover, mergeProps(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": `${unref(bem).b()}`
      }), {
        title: withCtx(() => [..._cache[2] || (_cache[2] = [
          createTextVNode(
            "\u8BBE\u7F6E\u6392\u5E8F\u6761\u4EF6",
            -1
            /* CACHED */
          )
        ])]),
        content: withCtx(() => [
          createVNode(_component_a_form_item_rest, null, {
            default: withCtx(() => [
              createVNode(unref(draggable), {
                modelValue: sortList.value,
                "onUpdate:modelValue": _cache[0] || (_cache[0] = ($event) => sortList.value = $event),
                animation: 200,
                class: normalizeClass(unref(bem).e("content")),
                style: { "min-width": "350px" },
                "item-key": "key",
                handle: ".handle"
              }, {
                item: withCtx(({ element, index }) => [
                  createElementVNode(
                    "div",
                    {
                      class: normalizeClass(unref(bem).e("list-item"))
                    },
                    [
                      createCommentVNode(" \u62D6\u62FD\u56FE\u6807 "),
                      createElementVNode(
                        "div",
                        {
                          class: normalizeClass(["handle", unref(bem).e("drag-icon")])
                        },
                        [
                          createVNode(_component_ll_icon, { "icon-name": "icon-drag" })
                        ],
                        2
                        /* CLASS */
                      ),
                      createCommentVNode(" \u5B57\u6BB5\u540D "),
                      createVNode(_component_ll_select, {
                        value: element.key,
                        "onUpdate:value": ($event) => element.key = $event,
                        style: { "width": "180px" },
                        placeholder: "\u8BF7\u9009\u62E9\u5B57\u6BB5",
                        size: "small",
                        "field-names": {
                          label: "title",
                          value: "key",
                          options: "options"
                        },
                        options: localList.value.map((i) => ({
                          ...i,
                          disabled: sortList.value.some((y) => y.key === i.key)
                        })),
                        onSelect: handleField
                      }, null, 8, ["value", "onUpdate:value", "options"]),
                      createCommentVNode(" \u6392\u5E8F "),
                      createVNode(_component_ll_segmented, {
                        value: element.orderBy,
                        "onUpdate:value": ($event) => element.orderBy = $event,
                        class: normalizeClass(unref(bem).e("segmented")),
                        size: "small",
                        options: steps.value
                      }, null, 8, ["value", "onUpdate:value", "class", "options"]),
                      createCommentVNode(" \u79FB\u9664\u56FE\u6807 "),
                      createVNode(_component_ll_icon, {
                        "icon-name": "icon-remove",
                        class: normalizeClass(unref(bem).e("remove-icon")),
                        size: 14,
                        onClick: ($event) => handleRemove(index)
                      }, null, 8, ["class", "onClick"])
                    ],
                    2
                    /* CLASS */
                  )
                ]),
                _: 1
                /* STABLE */
              }, 8, ["modelValue", "class"]),
              sortList.value.length === 0 ? (openBlock(), createBlock(_component_ll_empty, {
                key: 0,
                style: { margin: "12px 0" },
                "image-width": "120px"
              })) : createCommentVNode("v-if", true),
              createElementVNode(
                "div",
                {
                  class: normalizeClass(unref(bem).e("operate"))
                },
                [
                  createVNode(_component_ll_button, {
                    class: normalizeClass(unref(bem).e("operate-btn")),
                    disabled: disabledAdd.value,
                    type: "text",
                    size: "small",
                    "icon-space": 8,
                    onClick: handleAdd
                  }, {
                    default: withCtx(() => [
                      createVNode(_component_ll_icon, { "icon-name": "icon-list-add" }),
                      createTextVNode(
                        " " + toDisplayString(_ctx.addSortText || "\u6DFB\u52A0\u6392\u5E8F\u6761\u4EF6 "),
                        1
                        /* TEXT */
                      )
                    ]),
                    _: 1
                    /* STABLE */
                  }, 8, ["class", "disabled"]),
                  createCommentVNode(" \u64CD\u4F5C\u533A\u63D2\u69FD "),
                  createElementVNode(
                    "div",
                    {
                      class: normalizeClass(unref(bem).e("operate-slot"))
                    },
                    [
                      renderSlot(_ctx.$slots, "operate")
                    ],
                    2
                    /* CLASS */
                  )
                ],
                2
                /* CLASS */
              )
            ]),
            _: 3
            /* FORWARDED */
          })
        ]),
        default: withCtx(() => [
          createVNode(_component_a_badge, {
            dot: _ctx.useBadge && _ctx.activeState,
            offset: [-2, 2]
          }, {
            default: withCtx(() => [
              createVNode(_component_ll_icon_base_pro, {
                class: normalizeClass(`${unref(bem).e("icon")} ${unref(bem).is(
                  newOpen.value || _ctx.activeState ? "active" : ""
                )}`),
                "show-tip": false,
                "icon-name": _ctx.sortIcon || "icon-sort",
                "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"]);
    };
  }
});

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