const require_runtime = require("../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_useEngineContext = require("../hooks/useEngineContext.js");
const require_ResponsiveEditWrapper = require("./ResponsiveEditWrapper.js");
let react = require("react");
react = require_runtime.__toESM(react);
//#region src/responsive/withResponsive.tsx
const FALLBACK_RESPONSIVE_INFO = {
	currentBreakpoint: "",
	deviceType: ""
};
const FallbackResponsiveOverrideContext = react.default.createContext(null);
function useFallbackResponsive() {
	return FALLBACK_RESPONSIVE_INFO;
}
/**
* 用 `withResponsive` 包裹任意 React 组件，使其自动获得：
*  - 响应式 variant 注入：默认 `variant = currentBreakpoint`，规则可覆盖；
*  - 全局 / 本地覆写规则注入：按 nodeType+nodeId+when 命中后浅合并 props；
*  - 编辑模式长按编辑：`editMode === true` 时自动套 `ResponsiveEditWrapper`。
*
* **运行时引用规则**：所有 React 资产（`useResponsive` / `ResponsiveOverrideContext`）
* 都通过 `app.responsive.{hooks,components}` 命名空间访问，**不直接 import @terra 实体**；
* 仅类型走 `import type` 走 @terra（编译期擦除）。
*
* 由于 `withResponsive` 是纯函数工厂，模块顶层调用 0 副作用；hook/Context 取值发生在
* 组件渲染期，那时 `app.responsive` 必已就绪（terra/bootstrap 同步注册）。
*
* @example
* interface OrderItemProps extends BaseResponsiveProps {
*   title: string;
* }
* const OrderItem: React.FC<OrderItemProps> = ({ title, variant, className }) => (
*   <div className={className}>
*     {variant === 'phone' ? <span>{title}</span> : <strong>{title}</strong>}
*   </div>
* );
* OrderItem.displayName = 'OrderItem';
* export default withResponsive(OrderItem);
*/
function withResponsive(Component, options) {
	const inferredName = (options === null || options === void 0 ? void 0 : options.name) || Component.displayName || Component.name || "ResponsiveComponent";
	const ComponentWithRef = Component;
	const Wrapped = react.default.forwardRef((rawProps, ref) => {
		var _appHelper, _responsive$hooks, _responsive$component;
		const context = require_useEngineContext.default();
		const getApp = context === null || context === void 0 || (_appHelper = context.appHelper) === null || _appHelper === void 0 || (_appHelper = _appHelper.utils) === null || _appHelper === void 0 ? void 0 : _appHelper.getApp;
		const app = getApp === null || getApp === void 0 ? void 0 : getApp();
		const responsive = app === null || app === void 0 ? void 0 : app.responsive;
		const useResponsiveHook = typeof (responsive === null || responsive === void 0 || (_responsive$hooks = responsive.hooks) === null || _responsive$hooks === void 0 ? void 0 : _responsive$hooks.useResponsive) === "function" ? responsive.hooks.useResponsive : useFallbackResponsive;
		const ResponsiveOverrideContext = responsive === null || responsive === void 0 || (_responsive$component = responsive.components) === null || _responsive$component === void 0 ? void 0 : _responsive$component.ResponsiveOverrideContext;
		const ResponsiveOverrideCtx = ResponsiveOverrideContext && typeof ResponsiveOverrideContext === "object" ? ResponsiveOverrideContext : FallbackResponsiveOverrideContext;
		const info = useResponsiveHook() || FALLBACK_RESPONSIVE_INFO;
		const overrideCtx = (0, react.useContext)(ResponsiveOverrideCtx);
		const node = (0, react.useMemo)(() => ({
			componentName: inferredName,
			id: rawProps.id,
			className: rawProps.className
		}), [rawProps.id, rawProps.className]);
		const finalProps = (0, react.useMemo)(() => {
			if (overrideCtx === null || overrideCtx === void 0 ? void 0 : overrideCtx.resolveProps) return overrideCtx.resolveProps(node, rawProps);
			if (typeof (responsive === null || responsive === void 0 ? void 0 : responsive.resolveProps) === "function") return responsive.resolveProps(node, rawProps);
			const next = require_objectSpread2._objectSpread2({}, rawProps);
			if (typeof next.variant !== "string" || next.variant.length === 0) next.variant = info.currentBreakpoint || info.deviceType;
			return next;
		}, [
			overrideCtx,
			responsive,
			node,
			rawProps,
			info.currentBreakpoint,
			info.deviceType
		]);
		if ((options === null || options === void 0 ? void 0 : options.editable) !== false && !!(overrideCtx === null || overrideCtx === void 0 ? void 0 : overrideCtx.editMode)) return /* @__PURE__ */ react.default.createElement(require_ResponsiveEditWrapper.default, {
			node,
			finalProps,
			editableFields: options === null || options === void 0 ? void 0 : options.editableFields
		}, /* @__PURE__ */ react.default.createElement(ComponentWithRef, require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, finalProps), {}, { ref })));
		return /* @__PURE__ */ react.default.createElement(ComponentWithRef, require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, finalProps), {}, { ref }));
	});
	Wrapped.displayName = `withResponsive(${inferredName})`;
	return Wrapped;
}
//#endregion
exports.default = withResponsive;
