/**
 * Copyright IBM Corp. 2020, 2026
 *
 * This source code is licensed under the Apache-2.0 license found in the
 * LICENSE file in the root directory of this source tree.
 */

import { __toESM } from "../../_virtual/_rolldown/runtime.js";
import { require_classnames } from "../../node_modules/classnames/index.js";
import { pkg } from "../../settings.js";
import { useIsomorphicEffect } from "../../global/js/hooks/useIsomorphicEffect.js";
import { debounce } from "../../global/js/utils/debounce.js";
import { useEffect, useRef, useState } from "react";

//#region src/components/Datagrid/useStickyColumn.ts
/**
* Copyright IBM Corp. 2021, 2025
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
var import_classnames = /* @__PURE__ */ __toESM(require_classnames());
const blockClass = `${pkg.prefix}--datagrid`;
const styleClassPrefix = `${blockClass}__right-sticky-column`;
const leftStickyStyleClassPrefix = `${blockClass}__left-sticky-column`;
const OFFSET_SCROLL_CLASS = `${styleClassPrefix}-offset-scroll`;
const useStickyColumn = (hooks) => {
	const tableBodyRef = useRef(void 0);
	const stickyHeaderCellRef = useRef(void 0);
	const [windowSize, setWindowSize] = useState(void 0);
	useEffect(() => {
		setWindowSize(window?.innerWidth);
	}, []);
	useIsomorphicEffect(() => {
		/* istanbul ignore next */
		function updateSize() {
			setWindowSize(window.innerWidth);
		}
		/* istanbul ignore next */
		window.addEventListener("resize", updateSize);
		return () => window.removeEventListener("resize", updateSize);
	}, []);
	hooks.getCellProps.push(changeProps.bind(null, "cell", null, windowSize));
	hooks.getHeaderProps.push(changeProps.bind(null, "header", stickyHeaderCellRef, windowSize));
	hooks.getTableBodyProps.push(addTableBodyProps.bind(null, tableBodyRef));
	hooks.getHeaderGroupProps.push((props) => [props, { style: {
		...props.style,
		minWidth: "unset"
	} }]);
	const useEventListener = (instance) => {
		useEffect(() => {
			const tableBodyElement = tableBodyRef.current;
			const headerCellElement = stickyHeaderCellRef?.current;
			/* istanbul ignore next */
			if (hasVertScroll(tableBodyElement) && headerCellElement) headerCellElement?.classList?.add(OFFSET_SCROLL_CLASS);
			const boundListener = debounce(onBodyResize.bind(null, tableBodyElement, headerCellElement), 250);
			/* istanbul ignore next */
			if (typeof window !== "undefined") window.addEventListener("resize", boundListener);
			return () => {
				/* istanbul ignore next */
				if (typeof window !== "undefined") window.removeEventListener("resize", boundListener);
			};
		}, [instance.rows, instance.isFetching]);
		useEffect(() => {
			const tableBodyElement = tableBodyRef.current;
			const headerCellElement = stickyHeaderCellRef.current;
			const listener = (evt) => {
				toggleStickyShadow(evt.target, headerCellElement);
			};
			toggleStickyShadow(tableBodyElement, headerCellElement);
			if (tableBodyElement) tableBodyElement?.addEventListener("scroll", listener);
			return () => {
				if (tableBodyElement) tableBodyElement.removeEventListener("scroll", listener);
			};
		}, [instance.rows, instance.isFetching]);
	};
	const useCheckScroll = (instance) => {
		const tableBodyElement = tableBodyRef.current;
		const headerCellElement = stickyHeaderCellRef.current;
		useEffect(() => {
			onBodyResize(tableBodyElement, headerCellElement);
		}, [
			instance.rows,
			headerCellElement,
			tableBodyElement
		]);
	};
	hooks.useInstance.push(useEventListener);
	hooks.useInstance.push(useCheckScroll);
	hooks.useInstance.push((instance) => {
		Object.assign(instance, { withStickyColumn: true });
	});
	hooks.useInstance.push((instance) => {
		const newColumns = instance.visibleColumns;
		let spacerIdx = newColumns.findIndex((col) => col.id === "spacer");
		let stickyIdx = newColumns.findIndex((col) => col.sticky === "right");
		if (spacerIdx >= 0 && stickyIdx >= 0 && stickyIdx < spacerIdx) {
			const temp = newColumns[spacerIdx];
			newColumns[spacerIdx] = newColumns[stickyIdx];
			newColumns[stickyIdx] = temp;
		}
		const newHeaders = instance.headers;
		spacerIdx = newHeaders.findIndex((col) => col.id === "spacer");
		stickyIdx = newHeaders.findIndex((col) => col.sticky === "right");
		if (spacerIdx >= 0 && stickyIdx >= 0 && stickyIdx < spacerIdx) {
			const temp = newHeaders[spacerIdx];
			newHeaders[spacerIdx] = newHeaders[stickyIdx];
			newHeaders[spacerIdx].canResize = false;
			newHeaders[spacerIdx].disableResizing = true;
			delete newHeaders[spacerIdx].getResizerProps;
			newHeaders[stickyIdx] = temp;
		}
	});
};
const addTableBodyProps = (tableBodyRef, props) => [props, { ref: tableBodyRef }];
const changeProps = (elementName, headerCellRef, windowSize, props, data) => {
	const column = data.column || data.cell?.column;
	if (column?.sticky === "right") return [props, {
		className: (0, import_classnames.default)({
			[`${styleClassPrefix}-${elementName}`]: true,
			[`${blockClass}__resizableColumn`]: false,
			[`${blockClass}__sortableColumn`]: false
		}),
		...headerCellRef && { ref: headerCellRef }
	}];
	if (column?.sticky === "left") return [props, {
		className: (0, import_classnames.default)({
			[`${leftStickyStyleClassPrefix}-${elementName}`]: windowSize && windowSize > 671,
			[`${leftStickyStyleClassPrefix}-${elementName}--with-extra-select-column`]: data?.instance?.withSelectRows && windowSize && windowSize > 671
		}),
		...headerCellRef && { ref: headerCellRef }
	}];
	return [props];
};
const onBodyResize = (tableBodyEle, headerCellEle) => {
	if (headerCellEle) {
		/* istanbul ignore next */
		if (hasVertScroll(tableBodyEle)) headerCellEle.classList.add(OFFSET_SCROLL_CLASS);
		else headerCellEle.classList.remove(OFFSET_SCROLL_CLASS);
		toggleStickyShadow(tableBodyEle, headerCellEle);
	}
};
const toggleStickyShadow = (tableBodyEle, headerCellEle) => {
	/* istanbul ignore next */
	if (tableBodyEle && headerCellEle) if (tableBodyEle.scrollLeft + tableBodyEle.clientWidth === tableBodyEle.scrollWidth) {
		headerCellEle.classList.add(`${blockClass}__sticky-noShadow`);
		tableBodyEle.classList.add(`${blockClass}__sticky-column-noShadow`);
	} else {
		headerCellEle.classList.remove(`${blockClass}__sticky-noShadow`);
		tableBodyEle.classList.remove(`${blockClass}__sticky-column-noShadow`);
	}
};
const hasVertScroll = (element) => {
	/* istanbul ignore next */
	if (!element) return false;
	const { scrollHeight, clientHeight } = element;
	return scrollHeight > clientHeight;
};

//#endregion
export { useStickyColumn as default };