import { defineComponent as ye, computed as d, ref as Q, watch as ge, createVNode as m, openBlock as c, createElementBlock as X, Fragment as Y, createBlock as g, unref as t, mergeProps as V, withCtx as f, createTextVNode as _, toDisplayString as P, normalizeClass as K, createCommentVNode as G } from "vue";
import { DownOutlined as xe } from "@ant-design/icons-vue";
import { message as q, Flex as ve, Tooltip as j, Dropdown as he, Menu as ke, MenuItem as Z } from "@aplus-frontend/antdv";
import { IconApAdLineAnnotation as B } from "@aplus-frontend/icon";
import { transformTableParamsNoTimezone as be, isObjectWithContent as A } from "@aplus-frontend/utils";
import { merge as N } from "lodash-unified";
import "../../ap-button/index.mjs";
import { useInjectApTable as Ee } from "../../ap-table/context.mjs";
import "../../config-provider/index.mjs";
import { ApBatchActionGroup as we } from "../ap-batch-action-group/index.mjs";
import { convertExportField as De } from "./convertExportField.mjs";
import { handleExportDownload as Le } from "./handleExportDownload.mjs";
import { ApExportGroupActionType as x } from "./interface.mjs";
import Re from "./style/index.mjs";
import { useNamespace as Se } from "../../config-provider/hooks/use-namespace.mjs";
import { useLocale as Ce } from "../../config-provider/hooks/use-locale.mjs";
import { useGlobalConfig as Ge } from "../../config-provider/hooks/use-global-config.mjs";
import $ from "../../ap-button/ap-button.vue.mjs";
const Je = /* @__PURE__ */ ye({
  __name: "ApExportGroup",
  props: {
    type: {
      default: "button"
    },
    selectedRowKeys: {},
    groupList: {},
    buttonProps: {},
    disabled: {
      type: Boolean
    },
    auth: {},
    ifShow: {},
    text: {},
    idKey: {},
    request: {},
    getRequestParams: {},
    getInitialParams: {},
    beforeRequest: {},
    successMessage: {
      type: [Boolean, String],
      default: !1
    },
    exportField: {
      type: [Boolean, Object],
      default: void 0
    },
    dateValidate: {
      type: [Object, Boolean],
      default: void 0
    },
    handleExported: {}
  },
  setup(ee) {
    const l = ee, {
      columns: z,
      dataSource: U,
      getSearchFormValues: te,
      getLastParams: ae
    } = Ee(), {
      b: v
    } = Se("ap-export-group"), h = Re("ap-export-group"), {
      t: i
    } = Ce(), T = Ge("apExportGroup"), W = d(() => T.value?.hasPermission), H = d(() => T?.value?.exportField), J = d(() => T?.value?.dateValidate), oe = d(() => T?.value?.handleExported), k = d(() => J.value === !0 ? {
      maxDayRange: 90,
      smartIdentify: !0,
      enableMessage: !0
    } : J.value), b = d(() => l?.dateValidate === !0 ? {
      maxDayRange: 90,
      smartIdentify: !0,
      enableMessage: !0
    } : l?.dateValidate), S = Q(!1), I = Q(!1), C = d(() => !!l?.disabled || (U !== void 0 ? !U?.value?.length : !1) || S.value), ne = (e) => e === void 0 ? !1 : typeof e == "object" ? N({}, e) : e === !0 ? {} : e, E = (...e) => {
      let a = !1;
      for (const o of e)
        if (o !== void 0)
          if (o === !1)
            a = !1;
          else {
            const s = ne(o);
            s !== !1 && (a = a === !1 ? s : N({}, a, s));
          }
      return a;
    };
    ge(() => C.value, () => {
      C.value && (I.value = !1);
    }, {
      immediate: !0
    });
    const le = d(() => l?.successMessage === !0 ? i("ap.apExportGroup.exportSuccess") : l?.successMessage ? l.successMessage : ""), se = (e = l?.exportField) => {
      const a = "exportFieldList", o = e ?? (H.value === "" ? !0 : H.value) ?? !1;
      return o === !0 ? {
        key: a
      } : typeof o == "object" ? {
        ...o,
        key: o.key ?? a
      } : o;
    }, re = {
      textAlign: "center"
    }, F = d(() => l?.selectedRowKeys || []), O = d(() => {
      let e = l?.ifShow;
      return !l?.ifShow && l?.auth && (e = () => W.value?.(l.auth)), e?.() ?? !0;
    });
    async function ue() {
      F.value.length > 0 ? M({
        key: x.SELECT
      }) : M({
        key: x.ALL
      });
    }
    async function M({
      key: e,
      option: a
    }) {
      const o = N({}, l, a || {}), {
        getRequestParams: s,
        request: D
      } = o || {}, n = ae?.()?.sort, r = n ? be({
        sort: n
      }) : {}, u = se(o?.exportField);
      let p = {};
      e === x.SELECT ? p = {
        [o?.idKey || "ids"]: F.value
      } : e === x.ALL && (p = s?.() || te?.(!0) || {});
      const y = o?.getInitialParams?.() || {};
      try {
        S.value = !0;
        let L = Object.assign({
          ...p,
          ...y,
          ...r
        }, u && z?.value ? {
          [u.key]: De(z.value, u?.convertField)
        } : {});
        L = o?.beforeRequest?.(L) || L;
        const R = E(k.value, b.value, a?.dateValidate);
        A(R) && e === x.ALL && fe({
          params: L,
          dateValidate: R
        });
        const me = await Le(async () => await D?.(L));
        o?.successMessage && q.success(le.value), (o?.handleExported || oe.value)?.(me, L);
      } finally {
        S.value = !1;
      }
    }
    const w = d(() => l?.groupList?.filter((e) => {
      let a = e?.ifShow;
      return !e?.ifShow && e?.auth && (a = () => W.value?.(e.auth)), a?.() ?? !0;
    })?.map((e, a) => ({
      key: a,
      content: e?.text,
      auth: e?.auth,
      ifShow: e?.ifShow,
      disabled: e?.disabled,
      dateValidate: e?.dateValidate,
      onClick: () => {
        F.value.length > 0 ? M({
          key: x.SELECT,
          option: e
        }) : M({
          key: x.ALL,
          option: e
        });
      }
    })) || []), ie = () => {
      const e = E(k.value, b.value, ...w.value.map((a) => a.dateValidate));
      return m(ve, {
        align: "center",
        gap: 4
      }, {
        default: () => [m("span", null, [l?.buttonProps?.content ?? i("ap.apExportGroup.export")]), e !== !1 && A(e) ? m(j, {
          title: i("ap.apExportGroup.maxMessage", {
            maxDayRange: e.maxDayRange ?? 90
          })
        }, {
          default: () => [m(B, {
            class: [v("icon"), h.value]
          }, null)]
        }) : null]
      });
    }, pe = (e) => Object.keys(e).reduce((a, o) => typeof e?.[o] == "number" && e?.[o] > 1e4 ? {
      ...a,
      [o]: e?.[o]
    } : a, {}), de = (e) => {
      const a = Object.keys(e), o = [], s = /* @__PURE__ */ new Set(), D = [];
      for (let n = 0; n < a.length; n++)
        for (let r = n + 1; r < a.length; r++) {
          const u = a[n], p = a[r];
          let y = "";
          const L = Math.min(u.length, p.length);
          for (let R = 0; R < L && u[R] === p[R]; R++)
            y += u[R];
          y.length >= 3 && D.push({
            prefix: y,
            length: y.length,
            key1: u,
            key2: p,
            diff: Math.abs(e[u] - e[p])
          });
        }
      D.sort((n, r) => r.length !== n.length ? r.length - n.length : n.diff - r.diff);
      for (const n of D)
        if (!s.has(n.key1) && !s.has(n.key2)) {
          const r = e[n.key1], u = e[n.key2];
          o.push({
            startKey: r < u ? n.key1 : n.key2,
            endKey: r < u ? n.key2 : n.key1
          }), s.add(n.key1), s.add(n.key2);
        }
      return o;
    }, ce = ({
      startTime: e,
      endTime: a,
      maxDayRange: o
    }) => Math.abs(a - e) <= (o * 24 + 1) * 60 * 60 * 1e3, fe = ({
      params: e,
      dateValidate: a
    }) => {
      if (a.enableMessage === !1)
        return;
      const o = a.maxDayRange ?? 90, s = pe(e), D = [...a.smartIdentify ?? !0 ? de(s) : [], ...a.dateKeyList?.filter((n) => s[n.startKey || ""] && s[n.endKey || ""]) ?? []];
      if (D.length === 0) {
        q.warn(a?.emptyTooltip ?? i("ap.apExportGroup.emptyTooltip", {
          maxDayRange: o
        }));
        return;
      } else if (D.some(({
        startKey: r,
        endKey: u
      }) => {
        const p = s[r || ""], y = s[u || ""];
        return p !== void 0 && y !== void 0 && !ce({
          startTime: p,
          endTime: y,
          maxDayRange: o
        });
      })) {
        q.warn(a?.maxDayRangeTooltip ?? i("ap.apExportGroup.maxDayRangeTooltip", {
          maxDayRange: o
        }));
        return;
      }
    };
    return (e, a) => (c(), X(Y, null, [O.value && l.type === "button" ? (c(), g(t($), V({
      key: 0
    }, {
      ...e.$attrs
    }, {
      class: [t(v)(), t(h)],
      disabled: C.value,
      loading: S.value,
      "min-width": "",
      onClick: ue
    }), {
      default: f(() => [_(P(l?.text ?? t(i)("ap.apExportGroup.export")) + " ", 1), t(A)(E(k.value, b.value)) ? (c(), g(t(j), {
        key: 0,
        title: t(i)("ap.apExportGroup.maxMessage", {
          maxDayRange: E(k.value, b.value).maxDayRange ?? 90
        })
      }, {
        default: f(() => [m(t(B), {
          class: K([t(v)("icon"), t(h)])
        }, null, 8, ["class"])]),
        _: 1
      }, 8, ["title"])) : G("", !0)]),
      _: 1
    }, 16, ["class", "disabled", "loading"])) : G("", !0), O.value && l.type === "dropdown" ? (c(), g(t(he), {
      key: 1,
      open: I.value,
      "onUpdate:open": a[0] || (a[0] = (o) => I.value = o),
      class: K([t(v)(), t(h)]),
      "overlay-style": re,
      disabled: C.value
    }, {
      overlay: f(() => [m(t(ke), {
        onClick: M
      }, {
        default: f(() => [(c(), g(t(Z), {
          key: t(x).SELECT,
          disabled: F.value?.length === 0
        }, {
          default: f(() => [_(P(t(i)("ap.apExportGroup.exportSelected")), 1)]),
          _: 1
        }, 8, ["disabled"])), (c(), g(t(Z), {
          key: t(x).ALL
        }, {
          default: f(() => [_(P(t(i)("ap.apExportGroup.exportAll")), 1)]),
          _: 1
        }))]),
        _: 1
      })]),
      default: f(() => [m(t($), V({
        ...e.$attrs
      }, {
        loading: S.value
      }), {
        default: f(() => [_(P(l?.text ?? t(i)("ap.apExportGroup.export")) + " ", 1), t(A)(E(k.value, b.value)) ? (c(), g(t(j), {
          key: 0,
          title: t(i)("ap.apExportGroup.maxMessage", {
            maxDayRange: E(k.value, b.value).maxDayRange ?? 90
          })
        }, {
          default: f(() => [m(t(B), {
            class: K([t(v)("icon"), t(h)])
          }, null, 8, ["class"])]),
          _: 1
        }, 8, ["title"])) : G("", !0), m(t(xe))]),
        _: 1
      }, 16, ["loading"])]),
      _: 1
    }, 8, ["open", "class", "disabled"])) : G("", !0), O.value && l.type === "group" ? (c(), X(Y, {
      key: 2
    }, [w.value.length === 1 ? (c(), g(t($), V({
      key: 0
    }, {
      ...e.$attrs
    }, {
      class: [t(v)(), t(h)],
      disabled: C.value || w.value?.[0]?.disabled,
      loading: S.value,
      "min-width": "",
      onClick: w.value?.[0]?.onClick
    }), {
      default: f(() => [_(P(w.value?.[0]?.content ?? l?.text ?? t(i)("ap.apExportGroup.export")) + " ", 1), t(A)(E(k.value, b.value, w.value?.[0]?.dateValidate)) ? (c(), g(t(j), {
        key: 0,
        title: t(i)("ap.apExportGroup.maxMessage", {
          maxDayRange: E(k.value, b.value, w.value?.[0]?.dateValidate).maxDayRange ?? 90
        })
      }, {
        default: f(() => [m(t(B), {
          class: K([t(v)("icon"), t(h)])
        }, null, 8, ["class"])]),
        _: 1
      }, 8, ["title"])) : G("", !0)]),
      _: 1
    }, 16, ["class", "disabled", "loading", "onClick"])) : (c(), g(t(we), V({
      key: 1
    }, {
      ...e.$attrs
    }, {
      class: [t(v)(), t(h)],
      disabled: C.value,
      loading: S.value,
      "button-props": {
        ...l.buttonProps,
        content: ie(),
        type: l?.buttonProps?.type ?? "default"
      },
      "menu-list": w.value
    }), null, 16, ["class", "disabled", "loading", "button-props", "menu-list"]))], 64)) : G("", !0)], 64));
  }
});
export {
  Je as default
};
