import { _objectSpread2 } from "../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import { _objectWithoutProperties } from "../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectWithoutProperties.js";
import useContainerContext from "../hooks/useContainerContext.js";
import React, { forwardRef, useEffect, useImperativeHandle, useMemo, useRef } from "react";
//#region src/components/pisellDataSourceContainer/utils/withContainer.tsx
const _excluded = ["refId"];
/**
* 支持自定义 ID 的组件 Props
*/
function withContainer(WrappedComponent, displayName) {
	return forwardRef((props, forwardedRef) => {
		const { refId } = props, otherProps = _objectWithoutProperties(props, _excluded);
		const containerContext = useContainerContext(displayName);
		const innerRef = useRef(null);
		useImperativeHandle(forwardedRef, () => {
			return innerRef.current;
		}, []);
		useEffect(() => {
			if (refId && innerRef.current) containerContext.refManager.registerRef(refId, innerRef.current);
			return () => {
				if (refId) containerContext.refManager.unregisterRef(refId);
			};
		}, [refId, containerContext.refManager]);
		const mergedProps = useMemo(() => {
			return _objectSpread2(_objectSpread2({}, containerContext), otherProps);
		}, [otherProps, containerContext]);
		return /* @__PURE__ */ React.createElement(WrappedComponent, _objectSpread2({ ref: innerRef }, mergedProps));
	});
}
//#endregion
export { withContainer as default };
