import { getText } from "../../locales/index.js";
import { _objectSpread2 } from "../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import { _objectWithoutProperties } from "../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectWithoutProperties.js";
import useIsMobileTable from "../table/hooks/useIsMobileTable.js";
import React, { useEffect, useMemo, useState } from "react";
import { locales } from "@pisell/utils";
import { Pagination } from "antd";
import classNames from "classnames";
import ArrowRight from "@pisell/icon/es/ArrowRight";
import ArrowLeft from "@pisell/icon/es/ArrowLeft";
import "./index.less";
//#region src/components/Pagination/index.tsx
const _excluded = [
	"value",
	"onChange",
	"page",
	"pageSize"
];
/** 优先 utils；若 utils 返回 key（未命中）则用 materials RecordBoard 再回退 table-pagination-* */
function getPaginationText(key) {
	var _getText;
	const fullKey = `pisell2.salesManagement.pagination.${key}`;
	const fromUtils = locales.getText(fullKey);
	if (fromUtils !== fullKey) return fromUtils;
	return (_getText = getText(fullKey)) !== null && _getText !== void 0 ? _getText : getText(`table-pagination-${key}`);
}
const Pagination$1 = (props) => {
	const show = useMemo(() => {
		return Object.keys(props).length;
	}, [props]);
	const isMiniPagination = useIsMobileTable();
	const effectiveConfig = useMemo(() => {
		const prevText = getPaginationText("previous");
		const nextText = getPaginationText("next");
		const showTotalFn = getPaginationText("showTotal");
		const itemRenderFn = (_, type, originalElement) => {
			if (type === "prev") return /* @__PURE__ */ React.createElement("div", { className: classNames("paginator-page-btn", "left") }, /* @__PURE__ */ React.createElement(ArrowLeft, { className: "paginator-page-btn-left-icon" }), /* @__PURE__ */ React.createElement("span", { className: "paginator-page-btn-text" }, prevText));
			if (type === "next") return /* @__PURE__ */ React.createElement("div", { className: classNames("paginator-page-btn", "right") }, /* @__PURE__ */ React.createElement("span", { className: "paginator-page-btn-text" }, nextText), /* @__PURE__ */ React.createElement(ArrowRight, { className: "paginator-page-btn-right-icon" }));
			return originalElement;
		};
		return {
			className: "materials-grid-paginator",
			defaultCurrent: 1,
			showTotal: typeof showTotalFn === "function" ? (total, range) => showTotalFn(range[0], range[1]) : (total, range) => `Page ${range[0]} of ${range[1]}`,
			itemRender: itemRenderFn,
			responsive: true,
			size: "small"
		};
	}, []);
	return useMemo(() => {
		if (!show) return null;
		let Com = Pagination;
		if (isMiniPagination) Com = MiniPagination;
		return /* @__PURE__ */ React.createElement(Com, _objectSpread2(_objectSpread2({}, effectiveConfig), props));
	}, [
		show,
		effectiveConfig,
		props,
		isMiniPagination
	]);
};
const paginationConfig = {
	className: "materials-grid-paginator",
	defaultCurrent: 1,
	showTotal: (total, range) => `Page ${range[0]} of ${range[1]}`,
	responsive: true,
	size: "small"
};
const MiniPagination = (props) => {
	const { current = 1, pageSize = 10, total = 0 } = props;
	const MAX = useMemo(() => {
		return Math.ceil(total / pageSize);
	}, [total, pageSize]);
	const MIN = useMemo(() => {
		return 1;
	}, []);
	/**
	* @title: 翻页回调
	* @description:
	* @param {*} type
	* @return {*}
	* @Author: zhiwei.Wang
	* @Date: 2023-07-20 17:02
	*/
	const onChange = (type) => {
		let currentPage = current;
		if (type === "prev") currentPage = current - 1;
		if (type === "next") currentPage = current + 1;
		props.onChange && props.onChange(currentPage, pageSize || 10);
	};
	return /* @__PURE__ */ React.createElement("div", { className: "materials-grid-paginator" }, /* @__PURE__ */ React.createElement("div", {
		className: "pisell-lowcode-pagination-prev",
		onClick: () => {
			if (current <= MIN) return;
			onChange("prev");
		}
	}, /* @__PURE__ */ React.createElement("div", { className: classNames("paginator-page-btn", "left") }, /* @__PURE__ */ React.createElement(ArrowLeft, { className: "paginator-page-btn-left-icon" }))), /* @__PURE__ */ React.createElement("div", { className: "materials-grid-paginator-center" }, /* @__PURE__ */ React.createElement("span", { className: "materials-grid-paginator-center-text" }, (() => {
		const totalFn = getPaginationText("total");
		return typeof totalFn === "function" ? totalFn(current, MAX) : `${current} / ${MAX}`;
	})())), /* @__PURE__ */ React.createElement("div", {
		className: "pisell-lowcode-pagination-next",
		onClick: () => {
			if (current >= MAX) return;
			onChange("next");
		}
	}, /* @__PURE__ */ React.createElement("div", { className: classNames("paginator-page-btn", "right") }, /* @__PURE__ */ React.createElement(ArrowRight, { className: "paginator-page-btn-right-icon" }))));
};
const FormPagination = (_ref) => {
	let { value = {}, onChange, page, pageSize } = _ref, other = _objectWithoutProperties(_ref, _excluded);
	const [_p, _setP] = useState(page || 1);
	const [_ps, _setPs] = useState(pageSize || 10);
	const _onChange = (p, ps) => {
		onChange && onChange({
			page: ps === _ps ? p : 1,
			size: ps
		});
	};
	useEffect(() => {
		const _page = (value === null || value === void 0 ? void 0 : value.page) || page || 1;
		const _size = (value === null || value === void 0 ? void 0 : value.size) || pageSize || 10;
		_setP(_page);
		_setPs(_size);
	}, [
		value,
		page,
		pageSize
	]);
	return /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement(Pagination$1, _objectSpread2(_objectSpread2({}, other), {}, {
		pageSize: _ps,
		current: _p,
		onChange: _onChange,
		onShowSizeChange: _onChange
	})));
};
//#endregion
export { FormPagination, Pagination$1 as default, paginationConfig };
