import { defineComponent as O, ref as _, createVNode as s, mergeProps as G, onUnmounted as j } from "vue";
import { message as q } from "@aplus-frontend/antdv";
import { isFunction as u, cloneDeep as T, isEmpty as b, omitBy as U, isUndefined as F, omit as x } from "lodash-unified";
import "../../../ag-grid/index.mjs";
import { ApModal as L } from "../../../ap-modal/index.mjs";
import "../../../config-provider/index.mjs";
import { ROW_KEY as D } from "../constants.mjs";
import "../modal-layout/index.vue.mjs";
import { AsyncCallback as E, genRequest as W, delRowKeyField as $, addRowKeyField as z, getModalLayoutConfig as B } from "../utils.mjs";
import "../wrapper/index.vue.mjs";
import { useAgGridInstance as N } from "./use-ag-grid-instance.mjs";
import { useModalTitle as V } from "./use-modal-title.mjs";
import { useLocale as Y } from "../../../config-provider/hooks/use-locale.mjs";
import J from "../wrapper/index.vue2.mjs";
import Q from "../modal-layout/index.vue2.mjs";
import X from "../../../ag-grid/index.vue.mjs";
function we(o) {
  let d, f = null, g = null;
  const c = new E(), [p, K] = V(), {
    t: S
  } = Y(), [i, k] = N({
    onSelectedRows(e) {
      K.setCount(e?.length || 0);
    }
  }), w = W(o.api, o.getRowKey), y = (e) => {
    const {
      title: t,
      width: r,
      modalProps: n,
      ...a
    } = u(e) ? e(T(o)) : e;
    b(a) || g?.(a);
    const l = U({
      title: t,
      width: r,
      modalProps: n
    }, F);
    b(l) || f?.update(l);
  }, m = u(o.getRowKey) ? o.getRowKey : (e) => e[o.getRowKey], P = () => {
    const e = o.mergeRule || "old", t = i.getSelectedRows() || [];
    return {
      keys: H(t),
      rows: t.map((a) => {
        const l = m(a);
        return e === "old" && d?.[l] ? d[l] : $(a);
      })
    };
  }, h = async () => {
    const e = P(), t = o.maxCount || 1 / 0;
    return e.keys.length > t ? (q.warning(S("ap.apTableModal.messageMaxCount", {
      maxCount: t
    })), Promise.reject()) : (await o.beforeOk?.(e), o.finishCallback?.(e), c.resolve(e), e);
  }, M = () => {
    c.reject("modal is closed!");
  }, v = /* @__PURE__ */ O({
    name: "AgGridModalSelectContent",
    props: {
      modalConfig: {
        type: Object,
        required: !0
      },
      selectedRows: {
        type: Array,
        default: () => []
      }
    },
    setup(e) {
      const t = _(e.modalConfig);
      return g = (r) => {
        t.value = {
          ...t.value,
          ...r
        };
      }, () => {
        const r = e.selectedRows?.map((l) => z(l, t.value.getRowKey)), n = t.value, a = B(n.modalLayout);
        return s(k, null, {
          default: () => [s(J, {
            height: n.height,
            rowHeight: n.rowHeight,
            minHeight: t.value.minHeight,
            maxHeight: t.value.maxHeight
          }, {
            default: () => [o.renderHeader?.(x(i, "setInstance"), {
              updateProps: y
            }), s(Q, a.props, {
              default: () => [s(X, G({
                ref: i.setInstance,
                searchForm: {
                  sortable: !1,
                  resize: {
                    target: "form"
                  }
                },
                rowKey: u(n.getRowKey) ? D : n.getRowKey,
                request: w.request,
                style: {
                  padding: "0px"
                },
                rowHeight: n.rowHeight,
                columns: n.columns,
                rowSelection: {
                  defaultSelectedRows: r,
                  preserveSelectedRowKeys: !0,
                  type: n.mode || "checkbox",
                  fixed: "left",
                  columnWidth: 38,
                  disabled(l) {
                    return u(n.disabledCheckbox) ? n.disabledCheckbox(l, x(i, "setInstance")) : n.disabledCheckbox ?? !1;
                  },
                  ...o.rowSelection
                },
                autoHeight: t.value.height === "auto",
                onModelUpdated: i.onAgGridUpdate
              }, o.gridProps), n.gridSlots)],
              ...a.slots
            })]
          })]
        });
      };
    }
  });
  function C(e) {
    return s(v, {
      modalConfig: o,
      selectedRows: e
    }, null);
  }
  function A() {
    const e = Object.values(d || {}), t = C(e);
    f = L.createModal({
      title: s(p, {
        title: o.title,
        maxCount: o.maxCount || 1 / 0,
        isShowDefaultSuffix: o.mode !== "radio"
      }, o.modalTitleSlots),
      width: o.width,
      destroyOnClose: !0,
      maskClosable: !1,
      onOk: h,
      onCancel: M,
      afterOpenChange(r) {
        r === !1 && i.setInstance(void 0);
      },
      content: t,
      ...o.modalProps || {}
    });
  }
  function H(e) {
    return e?.map(m) || [];
  }
  function R(e, t, r = !1) {
    if (d = e?.reduce((n, a) => {
      const l = m(a);
      return n[l] = a, n;
    }, {}), w.setExtraParams(t?.extraParams), A(), r)
      return c.buildPromise();
  }
  function I(e, t) {
    return R(e, t, !0);
  }
  return j(() => {
    c.reject("component is Unmounted!");
  }), {
    open: R,
    asyncOpen: I,
    refresh: i.refresh,
    getAgGridInstance: i.getInstance,
    genContent: C,
    validate: h,
    updateProps: y
  };
}
export {
  we as useAgGridSelectModal
};
