const require_runtime = require("../../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_asyncToGenerator = require("../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js");
let react = require("react");
react = require_runtime.__toESM(react);
let react_dom = require("react-dom");
react_dom = require_runtime.__toESM(react_dom);
//#region src/components/pisellToast/squareToast/renderImperatively.tsx
const MARK = "__pisell_react_root__";
function render(node, container) {
	const root = container[MARK];
	if (root) {
		root.render(node);
		return;
	}
	const ReactMajorVersion = parseInt(react.default.version.split(".")[0], 10);
	const isReact18OrNewer = ReactMajorVersion >= 18;
	const fallbackCreateRoot = react_dom.createRoot;
	if (isReact18OrNewer && fallbackCreateRoot) {
		const root = fallbackCreateRoot(container);
		root.render(node);
		container[MARK] = root;
		return;
	}
	if (ReactMajorVersion >= 19) console.error("[pisellToast] React 19+ requires `createRoot`. Please call `setCreateRoot(createRoot)` at your app entry.");
	react_dom.render(node, container);
}
function unmount(_x) {
	return _unmount.apply(this, arguments);
}
function _unmount() {
	_unmount = require_asyncToGenerator._asyncToGenerator(function* (container) {
		return Promise.resolve().then(() => {
			if (container[MARK]) {
				var _container$MARK;
				(_container$MARK = container[MARK]) === null || _container$MARK === void 0 || _container$MARK.unmount();
				delete container[MARK];
			} else react_dom.unmountComponentAtNode(container);
		});
	});
	return _unmount.apply(this, arguments);
}
const defaultReactRender = (node, container) => {
	render(node, container);
	return () => {
		return unmount(container);
	};
};
let unstableRender = defaultReactRender;
/**
* @deprecated 设置 React 渲染函数以兼容使用。
*/
function unstableSetRender(render) {
	if (render) unstableRender = render;
	return unstableRender;
}
let sharedContainer = null;
function renderToBody(element) {
	if (!sharedContainer) {
		sharedContainer = document.createElement("div");
		document.body.appendChild(sharedContainer);
	}
	const container = sharedContainer;
	return unstableSetRender()(element, container);
}
function renderImperatively(element) {
	const Wrapper = react.default.forwardRef((_, ref) => {
		const [open, setOpen] = (0, react.useState)(false);
		const closedRef = (0, react.useRef)(false);
		const [elementToRender, setElementToRender] = (0, react.useState)(element);
		const keyRef = (0, react.useRef)(0);
		(0, react.useEffect)(() => {
			if (!closedRef.current) setOpen(true);
			else afterClose();
		}, []);
		function onCancel() {
			var _elementToRender$prop, _elementToRender$prop2;
			closedRef.current = true;
			setOpen(false);
			(_elementToRender$prop = (_elementToRender$prop2 = elementToRender.props).onCancel) === null || _elementToRender$prop === void 0 || _elementToRender$prop.call(_elementToRender$prop2);
		}
		function afterClose() {
			var _elementToRender$prop3, _elementToRender$prop4;
			unmount();
			(_elementToRender$prop3 = (_elementToRender$prop4 = elementToRender.props).afterClose) === null || _elementToRender$prop3 === void 0 || _elementToRender$prop3.call(_elementToRender$prop4);
		}
		(0, react.useImperativeHandle)(ref, () => ({
			close: onCancel,
			replace: (element) => {
				var _elementToRender$prop5, _elementToRender$prop6;
				keyRef.current++;
				(_elementToRender$prop5 = (_elementToRender$prop6 = elementToRender.props).afterClose) === null || _elementToRender$prop5 === void 0 || _elementToRender$prop5.call(_elementToRender$prop6);
				setElementToRender(element);
			}
		}));
		return react.default.cloneElement(elementToRender, require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, elementToRender.props), {}, {
			key: keyRef.current,
			open,
			onCancel,
			afterClose
		}));
	});
	const wrapperRef = react.default.createRef();
	const unmount = renderToBody(/* @__PURE__ */ react.default.createElement(Wrapper, { ref: wrapperRef }));
	return {
		close: function() {
			var _ref = require_asyncToGenerator._asyncToGenerator(function* () {
				if (!wrapperRef.current) {
					var _element$props$afterC, _element$props;
					unmount();
					(_element$props$afterC = (_element$props = element.props).afterClose) === null || _element$props$afterC === void 0 || _element$props$afterC.call(_element$props);
				} else {
					var _wrapperRef$current;
					(_wrapperRef$current = wrapperRef.current) === null || _wrapperRef$current === void 0 || _wrapperRef$current.close();
				}
			});
			return function close() {
				return _ref.apply(this, arguments);
			};
		}(),
		replace: (element) => {
			var _wrapperRef$current2;
			(_wrapperRef$current2 = wrapperRef.current) === null || _wrapperRef$current2 === void 0 || _wrapperRef$current2.replace(element);
		},
		isRendered: () => !!wrapperRef.current
	};
}
//#endregion
exports.renderImperatively = renderImperatively;
