import { ref as v, nextTick as I, unref as a, onMounted as ee, computed as b, createVNode as S } from "vue";
import { isArray as R, isEqualWith as te } from "lodash-unified";
import "../../config-provider/index.mjs";
import { parseFieldValue as ae, dataIndexToStr as re, isEqualCustom as oe } from "../utils.mjs";
import { useTableSortedAndFiltered as ne } from "./use-sorted-filtered.mjs";
import { useLocale as se } from "../../config-provider/hooks/use-locale.mjs";
import { useNamespace as le } from "../../config-provider/hooks/use-namespace.mjs";
const me = ({
  manual: j = !1,
  defaultCurrent: q = 1,
  defaultPageSize: x = 10,
  request: B,
  defaultParams: K = {},
  defaultData: M = [],
  formatParams: P,
  paramsValueTypeMap: z,
  resetFieldsIgnores: C,
  filterFields: N,
  sortFields: _,
  showLessItems: T = !1,
  showTotal: $ = !0,
  pagination: f = !0
}) => {
  let F = 0, s = {};
  const k = (e) => P ? P(e) : e, c = v(), u = v(f ? q : void 0), d = v(f ? x : void 0), {
    getColumnSFConfig: J,
    setSF: L,
    clearAll: Q
  } = ne(), {
    t: g
  } = se(), {
    b: m
  } = le("ap-table"), o = v({
    total: 0,
    records: M,
    loading: !1
  }), V = async (e) => {
    const t = k(e), r = Date.now();
    F = r, o.value.loading = !0;
    try {
      const n = await B({
        ...K,
        ...t
      });
      F === r && (o.value.total = n.total || n.data?.length || 0, o.value.records = [...n.data || []]);
    } catch {
      F === r && (o.value.records = [], o.value.total = 0);
    } finally {
      o.value.loading = !1;
    }
  }, y = (e) => {
    const t = {};
    return Object.entries(e).forEach(([r, n]) => {
      if (t[r] = n, z && n) {
        const i = z[r];
        i && (t[r] = ae(i, n));
      }
    }), t;
  }, W = async (e) => {
    const t = c.value?.apForm?.getFieldsValueTransformed(!0) || {}, r = {};
    Object.entries(t).forEach(([n, i]) => {
      C && C.indexOf(n) > -1 && (r[n] = i);
    }), await c.value?.apForm?.resetFields(), c.value?.apForm?.setFieldsValue?.(r), I(() => {
      e?.();
    });
  }, h = (e) => {
    s = e;
    const t = y(e);
    V(t);
  }, G = (e) => {
    h({
      ...s,
      ...e
    });
  }, w = () => {
    const e = c.value?.apForm?.getFieldsValueTransformed(!0) || {};
    f && (u.value = 1), h({
      ...e,
      filter: s.filter,
      sort: s.sort,
      current: f ? 1 : void 0,
      pageSize: f ? s.pageSize || x : void 0
    });
  }, H = () => {
    Q(), s.sort = {}, s.filter = {}, W(w);
  }, A = () => {
    h(s);
  }, U = (e) => {
    if (!f)
      A();
    else {
      const t = a(u), r = a(d), n = a(o).total - e, i = Math.ceil(n / r), p = t > i ? i : t;
      G({
        current: p > 0 ? p : 1
      });
    }
  }, D = (e, t, r) => {
    const n = c.value?.apForm?.getFieldsValueTransformed(!0) || {};
    u.value = e.current, d.value = e.pageSize, L(t, r);
    const i = R(r) ? [...r] : [r], p = {};
    for (const l of i)
      a(_)?.includes(l.columnKey || l.field) && l.order && (p[re(l.columnKey || l.field)] = l.order);
    const E = {};
    for (const l of Object.keys(t))
      a(N)?.includes(l) && t[l] && (E[l] = t[l]);
    const O = {
      ...n,
      filter: E,
      sort: p,
      current: e.current,
      pageSize: e.pageSize
    };
    te(s, O, oe) || h(O);
  };
  ee(() => {
    const e = c.value?.apForm?.getFieldsValueTransformed(!0) || {};
    if (s = {
      current: a(u),
      pageSize: a(d),
      filter: {},
      sort: {},
      ...e
    }, !j) {
      const t = y(s);
      V(t);
    }
  });
  const X = b(() => a(o).total === 0 ? "-" : `${a(u.value)}/${Math.ceil(a(o).total / a(d))}`);
  function Y(e) {
    R(e) && (o.value.records = e, o.value.total = e.length);
  }
  const Z = b(() => ({
    loading: a(o).loading,
    pagination: {
      current: a(u),
      pageSize: a(d),
      total: a(o).total,
      showSizeChanger: !T,
      showQuickJumper: !T,
      showLessItems: T,
      size: "default",
      showTotal: $ ? (e) => S("span", {
        class: m("pagination-total-wrapper")
      }, [g("ap.apTable.pagination.totalLeftPrefix"), " ", S("span", {
        class: m("pagination-count-text")
      }, [e]), " ", g("ap.apTable.pagination.totalLeftSuffix"), S("span", {
        class: m("pagination--total-right")
      }, [g("ap.apTable.pagination.totalRightPrefix"), " ", S("span", {
        class: m("pagination-count-text")
      }, [a(X)]), " ", g("ap.apTable.pagination.totalRightSuffix")])]) : void 0
    },
    onChange: D,
    dataSource: a(o).records
  }));
  return {
    formRef: c,
    submit: w,
    reset: H,
    refresh: A,
    refreshByDelete: U,
    data: o,
    current: u,
    pageSize: d,
    handleTableChange: D,
    tableProps: Z,
    dataSource: b(() => a(o).records),
    setDataSource: Y,
    getColumnSFConfig: J
  };
};
export {
  me as useTablePaging
};
