/**
 * 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.
 */

const require_runtime = require("../../_virtual/_rolldown/runtime.js");
const require_index = require("../../node_modules/classnames/index.js");
const require_settings = require("../../settings.js");
let react = require("react");
//#region src/components/Datagrid/useFloatingScroll.js
/**
* Copyright IBM Corp. 2024
*
* 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__ */ require_runtime.__toESM(require_index.default);
const blockClass = `${require_settings.pkg.prefix}--datagrid`;
const useFloatingScroll = (hooks) => {
	const useInstance = (instance) => {
		const { tableId, withVirtualScroll, ExpandedRowContentComponent } = instance;
		(0, react.useEffect)(() => {
			if (withVirtualScroll || ExpandedRowContentComponent) return;
			document.getElementById(tableId)?.classList.add(`${blockClass}__sticky`);
		}, [
			ExpandedRowContentComponent,
			tableId,
			withVirtualScroll
		]);
	};
	const useAddStickyClass = (instance) => {
		const { withVirtualScroll, ExpandedRowContentComponent } = instance;
		if (withVirtualScroll || ExpandedRowContentComponent) return;
		const addStickyClass = (props) => [props, { className: (0, import_classnames.default)(props.className, `${blockClass}__sticky`) }];
		hooks.getTableProps.push(addStickyClass);
		hooks.getTableBodyProps.push(addStickyClass);
		hooks.getHeaderGroupProps.push(addStickyClass);
	};
	const useAddScrollListener = (instance) => {
		const { withVirtualScroll, ExpandedRowContentComponent, tableId } = instance;
		if (withVirtualScroll || ExpandedRowContentComponent) return;
		let memoHead;
		const addOnScroll = (props) => [props, { onScroll: (e) => {
			let simpleThead;
			if (memoHead) simpleThead = memoHead;
			else {
				simpleThead = document.getElementById(tableId)?.querySelector(`.${blockClass}__table-simple thead > div`);
				memoHead = simpleThead;
			}
			if (simpleThead) simpleThead.scrollLeft = e.target.scrollLeft;
		} }];
		hooks.getTableBodyProps.push(addOnScroll);
	};
	hooks.useInstance.push(useAddStickyClass);
	hooks.useInstance.push(useAddScrollListener);
	hooks.useInstance.push(useInstance);
};
//#endregion
exports.default = useFloatingScroll;
