import { defineComponent as E, ref as d, unref as r, computed as G, h as H, openBlock as g, createBlock as k, mergeProps as y, withCtx as h, resolveDynamicComponent as U, createCommentVNode as q, createVNode as R } from "vue";
import { message as T, Modal as V } from "@aplus-frontend/antdv";
import { transformTableParams as z } from "@aplus-frontend/utils";
import { cloneDeep as W, omit as S } from "lodash-unified";
import { ApTable as J } from "../../ap-table/index.mjs";
import "../../config-provider/index.mjs";
import { AsyncOpenResultError as v } from "./interface.mjs";
import "./modal-title.vue.mjs";
import Q from "./style/index.mjs";
import "./table-layout/index.vue.mjs";
import { useNamespace as X } from "../../config-provider/hooks/use-namespace.mjs";
import { useLocale as Y } from "../../config-provider/hooks/use-locale.mjs";
import Z from "./modal-title.vue2.mjs";
import ee from "./table-layout/index.vue2.mjs";
const ye = /* @__PURE__ */ E({
  __name: "table-modal",
  props: {
    columns: {},
    getRowKey: {},
    api: { type: Function, default: async () => ({ records: [], total: 0 }) },
    apTableProps: {},
    maxCount: { default: 1 / 0 },
    isRenderModalTitleSuffix: { type: Boolean, default: !0 },
    renderModalTitleSuffix: {},
    disabledCheckbox: { type: [Boolean, Function] },
    rowSelection: {},
    tableLayoutConfig: {},
    renderHeader: {},
    onOk: {},
    prefixCls: {},
    confirmLoading: { type: Boolean, default: void 0 },
    title: {},
    closable: { type: Boolean, default: void 0 },
    closeIcon: {},
    onCancel: {},
    "onUpdate:visible": {},
    "onUpdate:open": {},
    onChange: {},
    afterClose: {},
    afterOpenChange: {},
    centered: { type: Boolean, default: void 0 },
    width: {},
    footer: {},
    okText: {},
    okType: {},
    cancelText: {},
    icon: {},
    maskClosable: { type: Boolean, default: void 0 },
    forceRender: { type: Boolean, default: void 0 },
    okButtonProps: {},
    cancelButtonProps: {},
    destroyOnClose: { type: Boolean, default: void 0 },
    wrapClassName: {},
    maskTransitionName: {},
    transitionName: {},
    getContainer: { type: [String, Boolean, Function], default: void 0 },
    zIndex: {},
    bodyStyle: {},
    maskStyle: {},
    mask: { type: [Object, Boolean], default: void 0 },
    keyboard: { type: Boolean, default: void 0 },
    wrapProps: {},
    focusTriggerAfterClose: { type: Boolean, default: void 0 },
    modalRender: {},
    mousePosition: {},
    loading: { type: Boolean }
  },
  emits: ["finish"],
  setup(f, { expose: B, emit: P }) {
    const e = f, M = P, i = d(!1), b = d({}), u = d(null), a = d([]), x = d();
    let m = null, c = null;
    const { b: K } = X("ap-table-modal"), I = Q("ap-table-modal"), { t: C } = Y(), O = async (o = [], t = {}) => {
      switch (a.value = [...o], i.value = !0, b.value = t?.extraParams, x.value = t.apTableDataSources || e.apTableProps?.dataSource, t.refreshMode || "reset") {
        case "reset":
          u.value?.reset();
          break;
        case "submit":
          u.value?.submit();
          break;
        case "none":
          break;
      }
      return new Promise((n, l) => {
        m = n, c = (s) => l(`Ignored Reject: ${s}`);
      });
    }, w = () => {
      i.value = !1, m = null, c?.(v.ModalCancel), c = null;
    }, N = async (o) => {
      o = z({
        ...o,
        ...b.value
      });
      const t = e.api, { records: n, total: l } = await t(o);
      return {
        data: n,
        total: l
      };
    }, _ = async () => {
      if (a.value.length > e.maxCount) {
        c?.(v.ExceedMaxCount);
        return;
      }
      if (e.getRowKey) {
        const o = W(r(a)), t = o.map(e.getRowKey);
        if (e.onOk && await e.onOk({
          keys: t,
          rows: o
        }) === !1)
          return;
        M("finish", t, o), m?.({ keys: t, rows: o });
      } else {
        c?.(v.NotFineGetRowKey);
        return;
      }
      i.value = !1;
    }, D = G(() => {
      if (!e.getRowKey) return;
      const o = e.getRowKey;
      return {
        fixed: !0,
        selectedRowKeys: a.value.map(o),
        columnWidth: 50,
        preserveSelectedRowKeys: !0,
        onSelectAll(t, n) {
          if (n.length > e.maxCount) {
            T.warning(
              C("ap.apTableModal.messageMaxCount", {
                maxCount: e.maxCount
              })
            );
            return;
          }
          const l = u.value?.dataSource;
          if (t) {
            const s = r(l).filter((p) => !a.value.some((j) => o(j) === o(p)));
            a.value = a.value.concat(s);
          } else
            a.value = a.value?.filter((s) => !l.some((p) => o(p) === o(s)));
        },
        onSelect(t, n) {
          if (n) {
            if (a.value.length >= e.maxCount) {
              T.warning(
                C("ap.apTableModal.messageMaxCount", {
                  maxCount: e.maxCount
                })
              );
              return;
            }
            e.rowSelection?.type === "radio" ? a.value = [t] : a.value.push(t);
          } else {
            const l = a.value.findIndex(
              (s) => o(t) === o(s)
            );
            a.value.splice(l, 1);
          }
        },
        getCheckboxProps: (t) => ({
          disabled: typeof e.disabledCheckbox == "function" ? e.disabledCheckbox(t, a.value) : e.disabledCheckbox
          // Column configuration not to be checked
        }),
        ...e.rowSelection || {}
      };
    }), L = () => e.isRenderModalTitleSuffix ? H(
      Z,
      {
        title: e.title,
        count: a.value.length || 0,
        maxCount: e.maxCount
      },
      {
        default: e.renderModalTitleSuffix ?? void 0
      }
    ) : e.title, A = () => u.value, F = () => {
      a.value = [];
    }, $ = (o) => {
      const t = e.getRowKey;
      if (!t) return;
      const n = a.value.findIndex(
        (l) => t(o) === t(l)
      );
      a.value.splice(n, 1);
    };
    return B({
      open: O,
      close: w,
      getApTableInstance: A
    }), (o, t) => (g(), k(r(V), y(
      {
        open: i.value,
        "onUpdate:open": t[0] || (t[0] = (n) => i.value = n)
      },
      r(S)(e, [
        "api",
        "getRowKey",
        "centered",
        "apTableProps",
        "onOk",
        "maxCount",
        "title",
        "renderModalTitleSuffix",
        "isRenderModalTitleSuffix",
        "rowSelection",
        "disabledCheckbox",
        "tableLayoutConfig"
      ]),
      {
        title: L(),
        centered: e.centered ?? !0,
        "wrap-class-name": `${r(K)()} ${r(I)}`,
        onOk: _,
        onCancel: w
      }
    ), {
      default: h(() => [
        e.renderHeader ? (g(), k(U(e.renderHeader), { key: 0 })) : q("", !0),
        R(ee, y(e.tableLayoutConfig || {}, {
          "selected-list": a.value,
          onClear: F,
          onDeleteItem: $
        }), {
          default: h(() => [
            R(r(J), y({
              ref_key: "apTableRef",
              ref: u,
              scroll: { y: 450 },
              style: { padding: "0px" },
              columns: f.columns,
              request: N,
              "row-key": f.getRowKey,
              "row-selection": D.value,
              "data-source": x.value,
              "search-form": {
                sortable: !1,
                countPerRow: 2
              }
            }, r(S)(e.apTableProps, "dataSource")), null, 16, ["columns", "row-key", "row-selection", "data-source"])
          ]),
          _: 1
        }, 16, ["selected-list"])
      ]),
      _: 1
    }, 16, ["open", "title", "centered", "wrap-class-name"]));
  }
});
export {
  ye as default
};
