const require_runtime = require("../../_virtual/_rolldown/runtime.js");
let react = require("react");
react = require_runtime.__toESM(react);
let classnames = require("classnames");
classnames = require_runtime.__toESM(classnames);
let ahooks = require("ahooks");
let _pisell_icon_es_ChevronLeft = require("@pisell/icon/es/ChevronLeft");
_pisell_icon_es_ChevronLeft = require_runtime.__toESM(_pisell_icon_es_ChevronLeft);
let _pisell_icon_es_ChevronRight = require("@pisell/icon/es/ChevronRight");
_pisell_icon_es_ChevronRight = require_runtime.__toESM(_pisell_icon_es_ChevronRight);
require("./index.less");
//#region src/components/pisellAnchor/index.tsx
const PisellAnchor = ({ options = [], tab, className, style, onChange, onTabClick, containerRef, platform = "pc" }) => {
	const contentRef = (0, react.useRef)(null);
	const isClickScrolling = (0, react.useRef)(false);
	const [leftWidth, setLeftWidth] = (0, react.useState)(0);
	const [rightWidth, setRightWidth] = (0, react.useState)(40);
	const [showButtons, setShowButtons] = (0, react.useState)(false);
	(0, react.useEffect)(() => {
		if (contentRef.current) {
			const { scrollWidth, clientWidth } = contentRef.current;
			setShowButtons(scrollWidth > clientWidth);
		}
	}, [options]);
	const handleHorizontalScroll = (0, ahooks.useDebounceFn)((e) => {
		const { scrollLeft, scrollWidth, clientWidth } = e.target;
		setLeftWidth(scrollLeft > 0 ? 40 : 0);
		setRightWidth(scrollLeft + clientWidth >= scrollWidth ? 0 : 40);
	}, { wait: 200 }).run;
	const handleContentScroll = (0, ahooks.useDebounceFn)(() => {
		if (isClickScrolling.current || !(containerRef === null || containerRef === void 0 ? void 0 : containerRef.current)) return;
		const container = containerRef.current;
		const scrollTop = container.scrollTop;
		const containerHeight = container.clientHeight;
		const scrollHeight = container.scrollHeight;
		if (Math.abs(scrollTop + containerHeight - scrollHeight) < 1) {
			const lastOption = options[options.length - 1];
			if (lastOption && tab !== lastOption.id) onChange === null || onChange === void 0 || onChange(lastOption.id);
			return;
		}
		let maxVisibleRatio = 0;
		let activeOption = null;
		for (const option of options) {
			const element = document.querySelector(`[data-section="${option.id}"]`);
			if (element instanceof HTMLElement) {
				const elementTop = element.offsetTop;
				const elementHeight = element.offsetHeight;
				const elementBottom = elementTop + elementHeight;
				const visibleTop = Math.max(elementTop, scrollTop);
				const visibleBottom = Math.min(elementBottom, scrollTop + containerHeight);
				const visibleRatio = Math.max(0, visibleBottom - visibleTop) / elementHeight;
				if (visibleRatio > maxVisibleRatio) {
					maxVisibleRatio = visibleRatio;
					activeOption = option;
				}
			}
		}
		if (activeOption && tab !== activeOption.id) onChange === null || onChange === void 0 || onChange(activeOption.id);
	}, { wait: 100 }).run;
	(0, react.useEffect)(() => {
		const container = containerRef === null || containerRef === void 0 ? void 0 : containerRef.current;
		if (!container) return;
		container.addEventListener("scroll", handleContentScroll);
		return () => container.removeEventListener("scroll", handleContentScroll);
	}, [handleContentScroll]);
	const handleOptionClick = (item) => {
		onChange === null || onChange === void 0 || onChange(item.id);
		onTabClick === null || onTabClick === void 0 || onTabClick();
		if (document.querySelector(`[data-section="${item.id}"]`) instanceof HTMLElement && (containerRef === null || containerRef === void 0 ? void 0 : containerRef.current)) {
			isClickScrolling.current = true;
			let scrollDistance = 0;
			for (const option of options) {
				if (option.id === item.id) break;
				const element = document.querySelector(`[data-section="${option.id}"]`);
				if (element instanceof HTMLElement) scrollDistance += element.offsetHeight;
			}
			containerRef.current.scrollTo({
				top: scrollDistance,
				behavior: "smooth"
			});
			setTimeout(() => {
				isClickScrolling.current = false;
			}, 1e3);
		}
	};
	const handlePageChange = (type) => {
		if (contentRef.current) {
			const { scrollLeft, clientWidth } = contentRef.current;
			contentRef.current.scrollTo({
				left: scrollLeft + (type === "left" ? -clientWidth : clientWidth),
				behavior: "smooth"
			});
		}
	};
	return /* @__PURE__ */ react.default.createElement("div", {
		className: (0, classnames.default)("pisell-anchor-tabs", className),
		style
	}, showButtons && /* @__PURE__ */ react.default.createElement("div", {
		className: "pisell-anchor-tabs-btn-left",
		style: { width: leftWidth },
		onClick: () => handlePageChange("left")
	}, /* @__PURE__ */ react.default.createElement(_pisell_icon_es_ChevronLeft.default, { className: "pisell-anchor-tabs-btn-icon" })), /* @__PURE__ */ react.default.createElement("div", {
		ref: contentRef,
		className: "pisell-anchor-tabs-content",
		onScroll: handleHorizontalScroll
	}, options.map((item) => /* @__PURE__ */ react.default.createElement("div", {
		key: item.id,
		className: (0, classnames.default)("pisell-anchor-tabs-tab", { "pisell-anchor-tabs-tab-active": tab === item.id }),
		onClick: () => handleOptionClick(item)
	}, /* @__PURE__ */ react.default.createElement("span", { className: "pisell-anchor-tabs-tab-content" }, item.name)))), showButtons && /* @__PURE__ */ react.default.createElement("div", {
		className: "pisell-anchor-tabs-btn-right",
		style: { width: rightWidth },
		onClick: () => handlePageChange("right")
	}, /* @__PURE__ */ react.default.createElement(_pisell_icon_es_ChevronRight.default, { className: "pisell-anchor-tabs-btn-icon" })));
};
//#endregion
exports.default = PisellAnchor;
