const require_runtime = require("../../_virtual/_rolldown/runtime.js");
const require_index = require("../../locales/index.js");
const require_objectSpread2 = require("../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_objectWithoutProperties = require("../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectWithoutProperties.js");
const require_useIsMobileTable = require("../table/hooks/useIsMobileTable.js");
let react = require("react");
react = require_runtime.__toESM(react);
let _pisell_utils = require("@pisell/utils");
let antd = require("antd");
let classnames = require("classnames");
classnames = require_runtime.__toESM(classnames);
let _pisell_icon_es_ArrowRight = require("@pisell/icon/es/ArrowRight");
_pisell_icon_es_ArrowRight = require_runtime.__toESM(_pisell_icon_es_ArrowRight);
let _pisell_icon_es_ArrowLeft = require("@pisell/icon/es/ArrowLeft");
_pisell_icon_es_ArrowLeft = require_runtime.__toESM(_pisell_icon_es_ArrowLeft);
require("./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 = _pisell_utils.locales.getText(fullKey);
	if (fromUtils !== fullKey) return fromUtils;
	return (_getText = require_index.getText(fullKey)) !== null && _getText !== void 0 ? _getText : require_index.getText(`table-pagination-${key}`);
}
const Pagination = (props) => {
	const show = (0, react.useMemo)(() => {
		return Object.keys(props).length;
	}, [props]);
	const isMiniPagination = require_useIsMobileTable.default();
	const effectiveConfig = (0, react.useMemo)(() => {
		const prevText = getPaginationText("previous");
		const nextText = getPaginationText("next");
		const showTotalFn = getPaginationText("showTotal");
		const itemRenderFn = (_, type, originalElement) => {
			if (type === "prev") return /* @__PURE__ */ react.default.createElement("div", { className: (0, classnames.default)("paginator-page-btn", "left") }, /* @__PURE__ */ react.default.createElement(_pisell_icon_es_ArrowLeft.default, { className: "paginator-page-btn-left-icon" }), /* @__PURE__ */ react.default.createElement("span", { className: "paginator-page-btn-text" }, prevText));
			if (type === "next") return /* @__PURE__ */ react.default.createElement("div", { className: (0, classnames.default)("paginator-page-btn", "right") }, /* @__PURE__ */ react.default.createElement("span", { className: "paginator-page-btn-text" }, nextText), /* @__PURE__ */ react.default.createElement(_pisell_icon_es_ArrowRight.default, { 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 (0, react.useMemo)(() => {
		if (!show) return null;
		let Com = antd.Pagination;
		if (isMiniPagination) Com = MiniPagination;
		return /* @__PURE__ */ react.default.createElement(Com, require_objectSpread2._objectSpread2(require_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 = (0, react.useMemo)(() => {
		return Math.ceil(total / pageSize);
	}, [total, pageSize]);
	const MIN = (0, react.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.default.createElement("div", { className: "materials-grid-paginator" }, /* @__PURE__ */ react.default.createElement("div", {
		className: "pisell-lowcode-pagination-prev",
		onClick: () => {
			if (current <= MIN) return;
			onChange("prev");
		}
	}, /* @__PURE__ */ react.default.createElement("div", { className: (0, classnames.default)("paginator-page-btn", "left") }, /* @__PURE__ */ react.default.createElement(_pisell_icon_es_ArrowLeft.default, { className: "paginator-page-btn-left-icon" }))), /* @__PURE__ */ react.default.createElement("div", { className: "materials-grid-paginator-center" }, /* @__PURE__ */ react.default.createElement("span", { className: "materials-grid-paginator-center-text" }, (() => {
		const totalFn = getPaginationText("total");
		return typeof totalFn === "function" ? totalFn(current, MAX) : `${current} / ${MAX}`;
	})())), /* @__PURE__ */ react.default.createElement("div", {
		className: "pisell-lowcode-pagination-next",
		onClick: () => {
			if (current >= MAX) return;
			onChange("next");
		}
	}, /* @__PURE__ */ react.default.createElement("div", { className: (0, classnames.default)("paginator-page-btn", "right") }, /* @__PURE__ */ react.default.createElement(_pisell_icon_es_ArrowRight.default, { className: "paginator-page-btn-right-icon" }))));
};
const FormPagination = (_ref) => {
	let { value = {}, onChange, page, pageSize } = _ref, other = require_objectWithoutProperties._objectWithoutProperties(_ref, _excluded);
	const [_p, _setP] = (0, react.useState)(page || 1);
	const [_ps, _setPs] = (0, react.useState)(pageSize || 10);
	const _onChange = (p, ps) => {
		onChange && onChange({
			page: ps === _ps ? p : 1,
			size: ps
		});
	};
	(0, react.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.default.createElement(react.default.Fragment, null, /* @__PURE__ */ react.default.createElement(Pagination, require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, other), {}, {
		pageSize: _ps,
		current: _p,
		onChange: _onChange,
		onShowSizeChange: _onChange
	})));
};
//#endregion
exports.FormPagination = FormPagination;
exports.default = Pagination;
exports.paginationConfig = paginationConfig;
