const require_runtime = require("../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_useScrollState = require("./hooks/useScrollState.js");
const require_useScrollEvents = require("./hooks/useScrollEvents.js");
const require_useIntersection = require("./hooks/useIntersection.js");
const require_index = require("./components/Actions/index.js");
let react = require("react");
react = require_runtime.__toESM(react);
let classnames = require("classnames");
classnames = require_runtime.__toESM(classnames);
require("./PisellScrollView.less");
//#region src/components/PisellScrollView/PisellScrollView.tsx
const snapScrollPaddings = {
	rArrows: {
		start: 0,
		end: 127
	},
	lrArrows: {
		start: 57,
		end: 57
	},
	tbArrows: {
		start: 57,
		end: 57
	}
};
const PisellScrollView = (0, react.forwardRef)((props, ref) => {
	const { children, className, style, overflow = "auto", scrollbarStyle = "default", scrollbarClassName, width, height, maxWidth, maxHeight, minWidth, minHeight, onScroll, onScrollStart, onScrollEnd, onReachTop, onReachBottom, onReachLeft, onReachRight, reachThreshold, scrollEndDelay, intersection, scrollActionsConfig = {
		show: true,
		type: "rArrows"
	}, snapScroll = {
		enabled: true,
		type: "mandatory"
	}, __designMode } = props;
	const containerRef = (0, react.useRef)(null);
	const { scrollState, updateScrollState } = require_useScrollState.useScrollState({
		containerRef,
		overflow
	});
	const { handleScroll } = require_useScrollEvents.useScrollEvents({
		containerRef,
		onScroll,
		onScrollStart,
		onScrollEnd,
		onReachTop,
		onReachBottom,
		onReachLeft,
		onReachRight,
		reachThreshold,
		scrollEndDelay,
		updateScrollState,
		scrollState
	});
	(0, react.useEffect)(() => {
		document.body.id = "body";
	}, []);
	require_useIntersection.useIntersection({
		containerRef,
		config: intersection
	});
	const scrollTo = (position) => {
		const container = containerRef.current;
		if (!container) return;
		const { scrollTop, scrollLeft, behavior = "auto" } = position;
		const options = { behavior };
		if (scrollTop !== void 0) options.top = scrollTop;
		if (scrollLeft !== void 0) options.left = scrollLeft;
		container.scrollTo(options);
	};
	const scrollToElement = (config) => {
		const container = containerRef.current;
		if (!container) return;
		const { targetId, align = "start", behavior = "auto", offset = 0 } = config;
		const target = container.querySelector(`#${targetId}`);
		if (!target) return;
		const containerRect = container.getBoundingClientRect();
		const targetRect = target.getBoundingClientRect();
		let top = container.scrollTop;
		let left = container.scrollLeft;
		if (align === "start") top += targetRect.top - containerRect.top;
		else if (align === "center") top += targetRect.top - containerRect.top - (container.clientHeight / 2 - targetRect.height / 2);
		else if (align === "end") top += targetRect.bottom - containerRect.bottom;
		top += offset;
		container.scrollTo({
			top,
			left,
			behavior
		});
	};
	const scrollToPercentage = (percentage) => {
		const container = containerRef.current;
		if (!container) return;
		const { percentX, percentY, behavior = "auto" } = percentage;
		const maxScrollTop = container.scrollHeight - container.clientHeight;
		const maxScrollLeft = container.scrollWidth - container.clientWidth;
		const options = { behavior };
		if (percentY !== void 0) options.top = percentY / 100 * maxScrollTop;
		if (percentX !== void 0) options.left = percentX / 100 * maxScrollLeft;
		container.scrollTo(options);
	};
	const scrollToTop = (behavior = "auto") => {
		var _containerRef$current;
		(_containerRef$current = containerRef.current) === null || _containerRef$current === void 0 || _containerRef$current.scrollTo({
			top: 0,
			behavior
		});
	};
	const scrollToBottom = (behavior = "auto") => {
		const container = containerRef.current;
		if (container) container.scrollTo({
			top: container.scrollHeight,
			behavior
		});
	};
	const scrollToLeft = (behavior = "auto") => {
		var _containerRef$current2;
		(_containerRef$current2 = containerRef.current) === null || _containerRef$current2 === void 0 || _containerRef$current2.scrollTo({
			left: 0,
			behavior
		});
	};
	const scrollToRight = (behavior = "auto") => {
		const container = containerRef.current;
		if (container) container.scrollTo({
			left: container.scrollWidth,
			behavior
		});
	};
	const getScrollData = () => {
		return scrollState;
	};
	const getScrollContainer = () => {
		return containerRef.current;
	};
	(0, react.useImperativeHandle)(ref, () => {
		if (__designMode) return containerRef.current;
		return {
			scrollTo,
			scrollToElement,
			scrollToPercentage,
			scrollToTop,
			scrollToBottom,
			scrollToLeft,
			scrollToRight,
			getScrollData,
			getScrollContainer,
			scrollToSnapPoint: (index, behavior = "auto") => {},
			getSnapPoints: () => [],
			getCurrentSnapIndex: () => 0,
			scrollToNextSnap: () => {},
			scrollToPrevSnap: () => {}
		};
	});
	const handleButtonClick = (direction) => {
		const container = containerRef.current;
		if (!container) return;
		if ((scrollActionsConfig === null || scrollActionsConfig === void 0 ? void 0 : scrollActionsConfig.type) === "indicator") {
			const indicatorTargets = {
				top: { top: 0 },
				bottom: { top: container.scrollHeight },
				left: { left: 0 },
				right: { left: container.scrollWidth }
			};
			container.scrollTo(require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, indicatorTargets[direction]), {}, { behavior: "smooth" }));
			return;
		}
		const distance = typeof (scrollActionsConfig === null || scrollActionsConfig === void 0 ? void 0 : scrollActionsConfig.scrollDistance) === "number" ? scrollActionsConfig.scrollDistance : 300;
		let top = container.scrollTop;
		let left = container.scrollLeft;
		if (direction === "top") top -= distance;
		if (direction === "bottom") top += distance;
		if (direction === "left") left -= distance;
		if (direction === "right") left += distance;
		container.scrollTo({
			top,
			left,
			behavior: "smooth"
		});
	};
	const containerStyle = require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, style), {}, {
		width,
		height,
		maxWidth,
		maxHeight,
		minWidth,
		minHeight
	});
	const viewportStyle = (0, react.useMemo)(() => {
		let _viewportStyle = {};
		if (overflow === "x") _viewportStyle.overflowX = "auto";
		if (overflow === "y") _viewportStyle.overflowY = "auto";
		if (overflow === "both") {
			_viewportStyle.overflowX = "auto";
			_viewportStyle.overflowY = "auto";
		}
		if (overflow === "hidden") {
			_viewportStyle.overflowX = "hidden";
			_viewportStyle.overflowY = "hidden";
		}
		if ((snapScroll === null || snapScroll === void 0 ? void 0 : snapScroll.enabled) && (scrollActionsConfig === null || scrollActionsConfig === void 0 ? void 0 : scrollActionsConfig.show)) {
			_viewportStyle.scrollSnapType = `${overflow === "x" ? "x" : "y"} ${snapScroll.type}`;
			if (overflow === "x") {
				var _snapScrollPaddings, _snapScrollPaddings2;
				_viewportStyle.scrollPaddingLeft = ((_snapScrollPaddings = snapScrollPaddings[scrollActionsConfig.type]) === null || _snapScrollPaddings === void 0 ? void 0 : _snapScrollPaddings.start) || 0;
				_viewportStyle.scrollPaddingRight = ((_snapScrollPaddings2 = snapScrollPaddings[scrollActionsConfig.type]) === null || _snapScrollPaddings2 === void 0 ? void 0 : _snapScrollPaddings2.end) || 0;
			}
			if (overflow === "y") {
				var _snapScrollPaddings3, _snapScrollPaddings4;
				_viewportStyle.scrollPaddingTop = ((_snapScrollPaddings3 = snapScrollPaddings[scrollActionsConfig.type]) === null || _snapScrollPaddings3 === void 0 ? void 0 : _snapScrollPaddings3.start) || 0;
				_viewportStyle.scrollPaddingBottom = ((_snapScrollPaddings4 = snapScrollPaddings[scrollActionsConfig.type]) === null || _snapScrollPaddings4 === void 0 ? void 0 : _snapScrollPaddings4.end) || 0;
			}
		}
		return _viewportStyle;
	}, [
		overflow,
		scrollActionsConfig === null || scrollActionsConfig === void 0 ? void 0 : scrollActionsConfig.show,
		scrollActionsConfig === null || scrollActionsConfig === void 0 ? void 0 : scrollActionsConfig.type,
		snapScroll
	]);
	const viewportClasses = (0, classnames.default)("pisell-scroll-view-viewport", {
		"pisell-scroll-view-viewport--hide-scrollbar": scrollbarStyle === "none" || !scrollState.isOverflows,
		[`pisell-scroll-view-viewport--${scrollbarStyle}`]: scrollbarStyle !== "default" && scrollbarStyle !== "custom",
		[scrollbarClassName || ""]: scrollbarStyle === "custom"
	});
	return /* @__PURE__ */ react.default.createElement("div", {
		className: (0, classnames.default)("pisell-scroll-view", className),
		style: require_objectSpread2._objectSpread2({}, containerStyle)
	}, /* @__PURE__ */ react.default.createElement("div", {
		className: (0, classnames.default)(viewportClasses),
		style: require_objectSpread2._objectSpread2({}, viewportStyle),
		ref: containerRef,
		"data-scrollbar-style": scrollbarStyle,
		onScroll: handleScroll
	}, children), /* @__PURE__ */ react.default.createElement(require_index.default, {
		containerRef,
		scrollState,
		overflow,
		scrollActionsConfig,
		onClick: handleButtonClick
	}));
});
PisellScrollView.displayName = "PisellScrollView";
//#endregion
exports.default = PisellScrollView;
