import { defineComponent as Ye, useSlots as Ze, getCurrentInstance as et, ref as w, computed as n, unref as t, toRef as tt, nextTick as re, watch as I, openBlock as i, createElementBlock as B, normalizeStyle as V, normalizeClass as g, createVNode as O, mergeProps as S, createSlots as D, withCtx as f, Fragment as ne, renderList as $, createBlock as u, resolveDynamicComponent as P, renderSlot as W, createCommentVNode as R, createElementVNode as b, normalizeProps as ot, guardReactiveProps as at } from "vue";
import { Spin as rt, Empty as le, Pagination as nt } from "@aplus-frontend/antdv";
import { useToken as lt } from "@aplus-frontend/antdv/es/theme/internal";
import { isUndefined as x, isFunction as G, isArray as st, omit as it } from "lodash-unified";
import { VxeUI as se, VxeTable as dt, VxeColumn as ie } from "vxe-table";
import "vxe-table/es/style.css";
import { noRenderAsFormItemValueList as ut } from "../ap-table/constants.mjs";
import { useProvideApTable as ct } from "../ap-table/context.mjs";
import { useTablePaging as ft } from "../ap-table/hooks/use-table-paging-ng.mjs";
import { getColumnOrder as de, updateFormProps as mt, getFieldProps as pt, getSearchFormItemRenderNode as gt, recursionApColumns as ue } from "../ap-table/utils.mjs";
import "../config-provider/index.mjs";
import "../scroll-bar/index.mjs";
import "../utils/index.mjs";
import { getValidVNodeList as ht } from "../utils/slot.mjs";
import { useDevWarning as Ct } from "../utils/warning.mjs";
import { ApForm as vt } from "../ap-form/index.mjs";
import { DEFAULT_ROW_SELECTION_CONFIG as ce, sizeReverseMap as yt, sizeMap as wt, ROW_SELECTION_FIELD as St } from "./constants.mjs";
import { useInnerParams as Rt } from "./hooks/use-inner-params.mjs";
import { useRenderColumns as bt } from "./hooks/use-render-columns.mjs";
import xt from "./hooks/use-row-selection.mjs";
import { useTableStyle as kt } from "./hooks/use-table-style.mjs";
import Ft from "./style/index.mjs";
import { toVxeProps as Et, getRowExpandProps as It, getRowSelectionProps as Bt } from "./utils/table.mjs";
import { useNamespace as Pt } from "../config-provider/hooks/use-namespace.mjs";
import { useGlobalConfig as fe } from "../config-provider/hooks/use-global-config.mjs";
import { useLocale as Tt } from "../config-provider/hooks/use-locale.mjs";
import { isDef as Nt } from "../utils/is.mjs";
import At from "../scroll-bar/index.vue.mjs";
const uo = /* @__PURE__ */ Ye({
  name: "ApGrid",
  __name: "index",
  props: {
    dropdownPrefixCls: {},
    bordered: { type: Boolean, default: !1 },
    locale: {},
    onChange: {},
    onResizeColumn: {},
    getPopupContainer: {},
    scroll: {},
    sortDirections: { default: () => ["ascend", "descend"] },
    showSorterTooltip: { type: [Boolean, Object], default: !1 },
    prefixCls: {},
    tableLayout: {},
    title: {},
    id: {},
    showHeader: { type: Boolean, default: !0 },
    components: {},
    customRow: {},
    customHeaderRow: {},
    direction: {},
    expandFixed: { type: [String, Boolean], default: !1 },
    expandColumnWidth: {},
    expandedRowKeys: {},
    defaultExpandedRowKeys: {},
    expandedRowRender: {},
    expandRowByClick: { type: Boolean, default: !1 },
    expandIcon: {},
    onExpand: {},
    onExpandedRowsChange: {},
    defaultExpandAllRows: { type: Boolean, default: !1 },
    indentSize: { default: 15 },
    expandIconColumnIndex: {},
    showExpandColumn: { type: Boolean, default: !0 },
    expandedRowClassName: {},
    childrenColumnName: { default: "children" },
    rowExpandable: {},
    sticky: { type: [Boolean, Object], default: !1 },
    transformCellText: {},
    rowHoverable: { type: Boolean },
    rowClassName: {},
    stripe: { type: Boolean },
    headerRowClassName: {},
    footerRowClassName: {},
    cellClassName: {},
    headerCellClassName: {},
    footerCellClassName: {},
    rowStyle: {},
    headerRowStyle: {},
    footerRowStyle: {},
    cellStyle: {},
    headerCellStyle: {},
    footerCellStyle: {},
    height: {},
    minHeight: {},
    maxHeight: {},
    mergeCells: {},
    mergeFooterItems: {},
    cellConfig: {},
    loading: { type: Boolean, default: void 0 },
    loadingConfig: {},
    headerCellConfig: {},
    footerCellConfig: {},
    round: { type: Boolean, default: void 0 },
    columns: {},
    rowSelection: { type: [Boolean, Object] },
    card: { type: Boolean, default: !1 },
    params: {},
    request: {},
    defaultData: {},
    dataSource: {},
    onLoadingChange: {},
    onShownColumnsChange: {},
    searchForm: { type: [Boolean, Object], default: void 0 },
    beforeSearchSubmit: {},
    pagination: { type: [Boolean, Object], default: void 0 },
    searchFormWrapperStyle: {},
    tableWrapperStyle: {},
    wrapperStyle: {},
    manual: { type: Boolean, default: void 0 },
    size: { default: "mini" },
    adaptive: { type: Boolean, default: !1 },
    columnResizable: { type: [Boolean, Object], default: !1 },
    rowKey: { default: "key" },
    footer: {},
    virtual: { type: [Boolean, Object], default: void 0 },
    expandable: {},
    value: {},
    rowHighlightable: { type: [Boolean, Function], default: !1 },
    treeConfig: {},
    onReady: {}
  },
  setup(l, { expose: me }) {
    const T = Ze(), o = l, k = et()?.appContext.app;
    k && !k.__VXE_PC_UI_INSTALLED__ && (k.use(se), k.__VXE_PC_UI_INSTALLED__ = !0), Ct("ApGrid")(
      !1,
      "breaking",
      "ApGrid has been EOL, Please use AgGrid(https://aplus.aplnk.com/aplus-ui/components/ag-grid/) instead."
    );
    const { e: F, b: c, be: pe } = Pt("ap-grid"), ge = Ft("ap-grid"), he = fe("valueTypeMap"), Ce = fe("apGrid"), d = w([]), M = w([]), s = w(), E = w(o.size), ve = kt(), [, ye] = lt(), { t: we } = Tt(), N = w(!0);
    se.setConfig({
      i18n: (e, a) => we(`ap.${e}`, a)
    });
    const { renderConfig: Se, updateSignal: Re } = bt(d);
    let _ = 0;
    const v = n(() => {
      let e = o.rowSelection || Ce.value?.rowSelection;
      if (e)
        return e === !0 ? ce : {
          ...ce,
          ...e
        };
    }), be = n(() => {
      if (s.value)
        return s.value.$el.querySelector(
          ".vxe-table--header-inner-wrapper"
        );
    }), j = n(() => (_++, o.columns?.filter(
      (e) => !e.hideInSearch && e.dataIndex && (e.valueType || e.customRenderFormItem) && !ut.includes(e.valueType)
    )?.sort((e, a) => {
      let r = de(e.order);
      return de(a.order) - r;
    })?.map((e) => {
      const a = mt(
        e,
        pt(e.fieldProps, {})
      ), r = {
        ...e,
        fieldProps: {
          label: e.title,
          name: e.dataIndex,
          // tips: 在表格中的查询表单项默认是带边框的
          bordered: !0,
          ...a || {},
          _signal: _
        },
        renderNode: void 0
      };
      return r.renderNode = gt(
        r,
        t(he)
      ), r;
    }) || [])), xe = n(() => Nt(o.manual) ? o.manual : ht(T.searchFormExtra?.() || []).some((a) => a.type?.name === "ApView")), ke = n(() => [
      c(),
      o.card ? null : c("wrapper"),
      o.adaptive ? c("adaptive") : null,
      o.sticky ? c("sticky") : null,
      ge.value
    ].filter(Boolean)), H = n(() => ue(t(d), (e) => {
      if (e.sorter === !0)
        return e.key || e.dataIndex;
    }).filter(Boolean)), q = n(() => ue(t(d), (e) => {
      if (e.filters && !e.onFilter)
        return e.key || e.dataIndex;
    }).filter(Boolean)), {
      formRef: h,
      submit: A,
      reset: z,
      refresh: U,
      data: m,
      tableProps: X,
      setDataSource: Fe,
      handleTableChangeOptional: Ee,
      getPaging: Ie,
      setPaging: Be,
      submitWith: J,
      getLastParams: Pe
    } = ft({
      async request(e) {
        if (v.value && !v.value.preserveSelectedRowKeys && x(o.dataSource) && !N.value && K(), N.value && (N.value = !1), !x(o.dataSource)) {
          const r = e.pageSize * (e.current - 1);
          return {
            data: (o.pagination === !1 ? o.dataSource : o.dataSource.slice(r, r + e.pageSize)) || [],
            total: o.dataSource.length || 0
          };
        }
        const a = await o.request?.(e);
        return {
          data: a?.data || [],
          total: a?.total || 0
        };
      },
      namespace: "ap-grid",
      filterFields: q,
      sortFields: H,
      params: tt(o, "params"),
      defaultData: o.defaultData,
      manual: t(xe),
      formatParams: o.beforeSearchSubmit,
      pagination: o.pagination,
      onClickReset: je,
      onAfterRequest: He
    }), { sortChangeEvent: Te, filterChangeEvent: Ne, paginationChangeEvent: Ae } = Rt(Ee);
    ct({
      columns: n(() => d.value),
      columnsBackup: n(() => M.value),
      size: n(() => wt[E.value]),
      updateColumns(e) {
        d.value = e, o.onShownColumnsChange?.(e);
      },
      updateSize(e) {
        E.value = yt[e];
      },
      renderConfig: n(() => ({
        className: pe("table-header", "title"),
        color: ye.value.colorTextTertiary
      })),
      dataSource: n(() => t(m).records),
      getSearchFormValues: Y,
      setSearchFormValues: Q,
      submit: A,
      submitWith: J,
      reset: z,
      refresh: U,
      getSearchFormSorterItems: Z,
      setSearchFormSorterItems: ee,
      resetSearchFormSorterItems: te,
      getLastParams: Pe
    });
    const ze = n(
      () => d.value.some(
        (e) => e.fixed === "left" || e.fixed === !0
      ) ? "left" : void 0
    ), Le = n(
      () => x(o.loading) ? m.loading : o.loading
    ), Ke = n(
      () => x(o.dataSource) ? m.records : o.dataSource
    ), { selectedRows: L, rowSelection: C, ...y } = xt({
      ...v.value,
      dataSource: Ke,
      rowKey: o.rowKey
    }), Ve = ({
      checked: e
    }) => {
      const a = s.value;
      a && a && y.selectChange(
        e,
        a.getCheckboxRecords(),
        a.getCheckboxReserveRecords()
      );
    }, Oe = ({
      checked: e
    }) => {
      const a = s.value;
      a && y.selectAllChange(e, [
        ...a.getCheckboxReserveRecords(),
        ...a.getCheckboxRecords()
      ]);
    }, De = () => {
      const e = s.value;
      e && y.selectRangeChange(e.getCheckboxRecords(), [
        ...e.getCheckboxReserveRecords(),
        ...e.getCheckboxRecords()
      ]);
    }, $e = () => {
      const e = s.value;
      e && (L.value = [e.getRadioRecord()]);
    }, We = ({
      row: e
    }) => {
      const a = o.rowHighlightable;
      G(a) && a(e);
    };
    function Ge() {
      return o.columns?.filter((e) => !e.hideInTable) || [];
    }
    function Q(e) {
      h.value?.apForm?.setFieldsValue?.(e);
    }
    function Y(e = !1) {
      const a = e ? "getFieldsValueTransformed" : "getFieldsValue";
      return h.value?.apForm?.[a]?.(!0);
    }
    function Z() {
      return h.value?.getSorterItems() || [];
    }
    function ee(e) {
      h.value?.setSorterItems(e);
    }
    function te() {
      h.value?.resetSorterItems();
    }
    function K() {
      y.clearAll();
      const e = s.value;
      e && (C.value.type === "radio" ? (e.clearRadioRow(), e.clearRadioReserve()) : (e.clearCheckboxRow(), e.clearCheckboxReserve()));
    }
    function Me(e) {
      G(o.rowKey) || (y.selectMultiByKeys(e), oe());
    }
    function _e(e) {
      const a = s.value;
      if (!a)
        return;
      const r = String(e[o.rowKey]), p = a.getRowById(r);
      a.setCurrentRow(p);
    }
    function je() {
      const e = s.value;
      e && (e.clearSort(), e.clearFilter());
    }
    function oe() {
      re(() => {
        C.value.type === "radio" ? s.value?.setRadioRowKey(C.value.selectedRowKeys[0]) : s.value?.setCheckboxRowKey(
          C.value.selectedRowKeys,
          !0
        );
      });
    }
    function He(e) {
      oe(), o.onReady && re(() => {
        o.onReady(e);
      });
    }
    async function qe(e, a) {
      const r = m.records[e];
      if (r)
        return s.value?.scrollToRow(r, a);
    }
    const Ue = (e) => G(o.rowKey) ? o.rowKey(e) : e[o.rowKey];
    async function Xe(e, a = !0) {
      const r = s.value;
      if (!r)
        return;
      const p = st(e) ? e : [e], Je = (r.getData() || []).filter(
        (Qe) => p.includes(Ue(Qe))
      );
      r.setRowExpand(Je, a);
    }
    return I(
      () => [o.columnResizable, o.columns],
      () => {
        const e = Ge();
        d.value = e, o.onShownColumnsChange?.(e), M.value = e;
      },
      {
        deep: !0,
        immediate: !0
      }
    ), I(
      () => o.dataSource,
      (e) => {
        Fe(e);
      },
      { immediate: !0, deep: !0 }
    ), I(
      () => t(m).loading,
      (e) => {
        o.onLoadingChange?.(e);
      }
    ), I(
      () => o.size,
      (e) => {
        E.value = e;
      }
    ), me({
      submit: A,
      reset: z,
      refresh: U,
      submitWith: J,
      setSearchFormValues: Q,
      getSearchFormValues: Y,
      getShowColumns: () => t(d),
      rowSelection: {
        selectedRows: n(() => L.value),
        setSelectedRowKeys: Me,
        clearAll: K
      },
      scrollToRow: qe,
      getDataSource: () => t(m.records),
      setCurrentRow: _e,
      getPaging: Ie,
      setPaging: Be,
      getSearchFormSorterItems: Z,
      setSearchFormSorterItems: ee,
      resetSearchFormSorterItems: te,
      setRowExpand: Xe
    }), (e, a) => (i(), B("div", {
      class: g(ke.value),
      style: V(l.wrapperStyle)
    }, [
      t(x)(l.dataSource) && l.searchForm !== !1 && j.value.length > 0 ? (i(), B("div", {
        key: 0,
        class: g(l.card ? t(F)("search-wrapper") : null),
        style: V(l.searchFormWrapperStyle)
      }, [
        O(t(vt).SearchForm, S(l.searchForm || {}, {
          ref_key: "formRef",
          ref: h,
          "custom-reset": "",
          "submit-loading": t(X).loading,
          onSubmit: t(A),
          onReset: t(z)
        }), D({
          default: f(() => [
            (i(!0), B(ne, null, $(j.value, (r) => (i(), u(P(r.renderNode), {
              key: r.dataIndex
            }))), 128))
          ]),
          _: 2
        }, [
          T.searchFormExtra ? {
            name: "extra",
            fn: f(() => [
              W(e.$slots, "searchFormExtra")
            ]),
            key: "0"
          } : void 0
        ]), 1040, ["submit-loading", "onSubmit", "onReset"])
      ], 6)) : R("", !0),
      b("div", {
        class: g([t(F)("table-wrapper"), l.card ? t(F)("table-wrapper-card") : null]),
        style: V(l.tableWrapperStyle)
      }, [
        b("div", {
          class: g(t(c)("header-wrapper"))
        }, [
          W(e.$slots, "title", {
            selectedRows: t(L),
            selectedRowKeys: t(C).selectedRowKeys,
            shownColumns: d.value,
            clearAll: K
          })
        ], 2),
        b("div", {
          class: g(t(c)("vxe-wrapper"))
        }, [
          (i(), u(t(dt), S(t(Et)(o, v.value), {
            ref_key: "tableRef",
            ref: s,
            key: t(Re),
            data: t(m).records,
            "sort-config": {
              remote: H.value.length > 0,
              trigger: "cell"
            },
            "filter-config": {
              remote: q.value.length > 0
            },
            loading: Le.value,
            style: t(ve),
            size: E.value,
            onSortChange: t(Te),
            onFilterChange: t(Ne),
            onCheckboxChange: Ve,
            onCheckboxRangeChange: De,
            onCheckboxAll: Oe,
            onRadioChange: $e,
            onCurrentRowChange: We
          }), D({
            empty: f(() => [
              O(t(le), {
                image: t(le).PRESENTED_IMAGE_SIMPLE
              }, null, 8, ["image"])
            ]),
            loading: f(() => [
              b("div", {
                class: g(t(F)("table-loading-wrapper"))
              }, [
                O(t(rt))
              ], 2)
            ]),
            default: f(() => [
              o.expandable && d.value.length ? (i(), u(t(ie), S({ key: 0 }, t(It)(l.expandable), { fixed: ze.value }), D({
                content: f((r) => [
                  (i(), u(P(o.expandable.renderContent(r))))
                ]),
                _: 2
              }, [
                l.expandable?.renderCell ? {
                  name: "default",
                  fn: f((r) => [
                    (i(), u(P(
                      l.expandable.renderCell(
                        r.$table.isRowExpandByRow(r.row),
                        r.row
                      )
                    )))
                  ]),
                  key: "0"
                } : void 0
              ]), 1040, ["fixed"])) : R("", !0),
              v.value && d.value.length ? (i(), u(t(ie), S(
                { key: 1 },
                t(Bt)(t(C), d.value, o.size),
                {
                  resizable: !1,
                  field: t(St)
                }
              ), null, 16, ["field"])) : R("", !0),
              (i(!0), B(ne, null, $(t(Se), (r) => (i(), u(P(r), {
                key: r.props.colId
              }))), 128))
            ]),
            _: 2
          }, [
            $(t(it)(T, ["searchFormExtra"]), (r, p) => ({
              name: p,
              fn: f((ae) => [
                W(e.$slots, p, ot(at(ae || {})))
              ])
            }))
          ]), 1040, ["data", "sort-config", "filter-config", "loading", "style", "size", "onSortChange", "onFilterChange"]))
        ], 2),
        b("div", {
          class: g(t(c)("pagination-wrapper"))
        }, [
          l.sticky ? (i(), u(t(At), {
            key: 0,
            direction: "horizontal",
            "scroll-container": be.value,
            "extra-size": -4.2,
            "bar-color": "var(--scrollbar-thumb-color-base)",
            "track-color": "var(--table-header-bg, #f2f6f9)"
          }, null, 8, ["scroll-container"])) : R("", !0),
          l.pagination !== !1 ? (i(), u(t(nt), S({
            key: 1,
            class: t(c)("pagination")
          }, t(X).pagination, { onChange: t(Ae) }), null, 16, ["class", "onChange"])) : R("", !0)
        ], 2)
      ], 6)
    ], 6));
  }
});
export {
  uo as default
};
