const require_runtime = require("../../../../_virtual/_rolldown/runtime.js");
const require_useEngineContext = require("../../../../hooks/useEngineContext.js");
const require_locales = require("../../locales.js");
let react = require("react");
react = require_runtime.__toESM(react);
let antd = require("antd");
let _pisell_materials = require("@pisell/materials");
require("./Pagination.less");
//#region src/components/list/components/Pagination/Pagination.tsx
const LeftArrowIcon = ({ disabled = false }) => /* @__PURE__ */ react.default.createElement(_pisell_materials.Iconfont, {
	type: "pisell2-arrow-left",
	style: {
		fontSize: "20px",
		color: disabled ? "#98A2B3" : "#344054"
	}
});
const RightArrowIcon = ({ disabled = false }) => /* @__PURE__ */ react.default.createElement(_pisell_materials.Iconfont, {
	type: "pisell2-arrow-right",
	style: {
		fontSize: "20px",
		color: disabled ? "#98A2B3" : "#344054"
	}
});
const DropdownIcon = () => /* @__PURE__ */ react.default.createElement("div", { style: {
	width: 20,
	height: 20,
	position: "relative",
	overflow: "hidden"
} }, /* @__PURE__ */ react.default.createElement("svg", {
	width: "20",
	height: "20",
	viewBox: "0 0 20 20",
	fill: "none"
}, /* @__PURE__ */ react.default.createElement("path", {
	d: "M5 7.5L10 12.5L15 7.5",
	stroke: "#344054",
	strokeWidth: "1.67",
	strokeLinecap: "round",
	strokeLinejoin: "round"
})));
const Pagination = ({ current = 1, pageSize = 10, total = 0, totalPages: propTotalPages, onChange, onShowSizeChange, showSizeChanger = true, pageSizeOptions = [
	"10",
	"20",
	"50",
	"100"
], showQuickJumper = false, showTotal, className = "", disabled = false }) => {
	const context = require_useEngineContext.default();
	const getCurrentLanguage = () => {
		var _context$appHelper, _context$appHelper$ge, _context$appHelper$ge2, _context$appHelper2;
		const appLocale = (_context$appHelper = context.appHelper) === null || _context$appHelper === void 0 || (_context$appHelper = _context$appHelper.utils) === null || _context$appHelper === void 0 || (_context$appHelper$ge = _context$appHelper.getApp) === null || _context$appHelper$ge === void 0 || (_context$appHelper$ge = _context$appHelper$ge.call(_context$appHelper)) === null || _context$appHelper$ge === void 0 || (_context$appHelper$ge = _context$appHelper$ge.locales) === null || _context$appHelper$ge === void 0 || (_context$appHelper$ge2 = _context$appHelper$ge.getLocale) === null || _context$appHelper$ge2 === void 0 ? void 0 : _context$appHelper$ge2.call(_context$appHelper$ge);
		const storageLocale = (_context$appHelper2 = context.appHelper) === null || _context$appHelper2 === void 0 || (_context$appHelper2 = _context$appHelper2.utils) === null || _context$appHelper2 === void 0 || (_context$appHelper2 = _context$appHelper2.storage) === null || _context$appHelper2 === void 0 ? void 0 : _context$appHelper2.get("umi_locale");
		let finalLang = appLocale || storageLocale;
		if (!finalLang && typeof window !== "undefined") finalLang = localStorage.getItem("umi_locale");
		return finalLang || "en";
	};
	const getLocalText = (key) => {
		var _listLocales;
		const localText = (_listLocales = require_locales.default[getCurrentLanguage()]) === null || _listLocales === void 0 ? void 0 : _listLocales[key];
		if (typeof localText === "function" || typeof localText === "string") return localText;
		return key;
	};
	const totalPages = propTotalPages !== null && propTotalPages !== void 0 ? propTotalPages : Math.ceil((total || 0) / pageSize);
	(current - 1) * pageSize + 1;
	propTotalPages ? current * pageSize : Math.min(current * pageSize, total || 0);
	const handlePageChange = (page) => {
		if (page < 1 || page > totalPages || disabled) return;
		onChange === null || onChange === void 0 || onChange(page, pageSize);
	};
	const handleSizeChange = (size) => {
		const newCurrent = propTotalPages ? 1 : Math.ceil((current - 1) * pageSize / size) + 1;
		onShowSizeChange === null || onShowSizeChange === void 0 || onShowSizeChange(newCurrent, size);
		onChange === null || onChange === void 0 || onChange(newCurrent, size);
	};
	const getPageNumbers = () => {
		const pages = [];
		const showPages = 7;
		if (totalPages <= showPages) for (let i = 1; i <= totalPages; i++) pages.push(i);
		else {
			const half = Math.floor(showPages / 2);
			let start = Math.max(1, current - half);
			let end = Math.min(totalPages, current + half);
			if (current <= half + 1) end = showPages - 1;
			else if (current >= totalPages - half) start = totalPages - showPages + 2;
			if (start > 1) {
				pages.push(1);
				if (start > 2) pages.push("...");
			}
			for (let i = start; i <= end; i++) pages.push(i);
			if (end < totalPages) {
				if (end < totalPages - 1) pages.push("...");
				pages.push(totalPages);
			}
		}
		return pages;
	};
	const pageNumbers = getPageNumbers();
	return /* @__PURE__ */ react.default.createElement("div", { className: `pisell-pagination ${className} ${disabled ? "disabled" : ""}` }, /* @__PURE__ */ react.default.createElement("div", { className: "pagination-info" }, (() => {
		const pageOfFunc = getLocalText("pisell-list-pagination-page-of");
		if (typeof pageOfFunc === "function") return pageOfFunc(current, totalPages);
		return `Page ${current} of ${totalPages}`;
	})()), /* @__PURE__ */ react.default.createElement("div", { className: "pagination-controls" }, /* @__PURE__ */ react.default.createElement("div", { className: "pagination-group" }, /* @__PURE__ */ react.default.createElement("button", {
		className: `pagination-btn pagination-prev ${current <= 1 ? "disabled" : ""}`,
		onClick: () => handlePageChange(current - 1),
		disabled: current <= 1 || disabled
	}, /* @__PURE__ */ react.default.createElement(LeftArrowIcon, { disabled: current <= 1 }), /* @__PURE__ */ react.default.createElement("span", null, getLocalText("pisell-list-pagination-previous") || "Previous")), pageNumbers.map((page, index) => {
		if (page === "...") return /* @__PURE__ */ react.default.createElement("div", {
			key: `ellipsis-${index}`,
			className: "pagination-ellipsis"
		}, /* @__PURE__ */ react.default.createElement("span", null, "..."));
		return /* @__PURE__ */ react.default.createElement("button", {
			key: page,
			className: `pagination-page ${current === page ? "active" : ""}`,
			onClick: () => handlePageChange(page),
			disabled
		}, page);
	}), /* @__PURE__ */ react.default.createElement("button", {
		className: `pagination-btn pagination-next ${current >= totalPages ? "disabled" : ""}`,
		onClick: () => handlePageChange(current + 1),
		disabled: current >= totalPages || disabled
	}, /* @__PURE__ */ react.default.createElement("span", null, getLocalText("pisell-list-pagination-next") || "Next"), /* @__PURE__ */ react.default.createElement(RightArrowIcon, { disabled: current >= totalPages }))), showSizeChanger && /* @__PURE__ */ react.default.createElement("div", { className: "pagination-size-selector" }, /* @__PURE__ */ react.default.createElement(antd.Select, {
		value: pageSize,
		onChange: handleSizeChange,
		disabled,
		style: { width: 88 },
		suffixIcon: /* @__PURE__ */ react.default.createElement(DropdownIcon, null),
		options: pageSizeOptions.map((option) => ({
			label: option,
			value: parseInt(option, 10)
		}))
	}))));
};
//#endregion
exports.default = Pagination;
