var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/responsive/withResponsive.tsx
var withResponsive_exports = {};
__export(withResponsive_exports, {
  default: () => withResponsive_default,
  withResponsive: () => withResponsive
});
module.exports = __toCommonJS(withResponsive_exports);
var import_react = __toESM(require("react"));
var import_useEngineContext = __toESM(require("../hooks/useEngineContext"));
var import_ResponsiveEditWrapper = __toESM(require("./ResponsiveEditWrapper"));
var FALLBACK_RESPONSIVE_INFO = {
  currentBreakpoint: "",
  deviceType: ""
};
var FallbackResponsiveOverrideContext = import_react.default.createContext(null);
function useFallbackResponsive() {
  return FALLBACK_RESPONSIVE_INFO;
}
function withResponsive(Component, options) {
  const inferredName = (options == null ? void 0 : options.name) || Component.displayName || Component.name || "ResponsiveComponent";
  const ComponentWithRef = Component;
  const Wrapped = import_react.default.forwardRef((rawProps, ref) => {
    var _a, _b, _c, _d;
    const context = (0, import_useEngineContext.default)();
    const getApp = (_b = (_a = context == null ? void 0 : context.appHelper) == null ? void 0 : _a.utils) == null ? void 0 : _b.getApp;
    const app = getApp == null ? void 0 : getApp();
    const responsive = app == null ? void 0 : app.responsive;
    const useResponsiveHook = typeof ((_c = responsive == null ? void 0 : responsive.hooks) == null ? void 0 : _c.useResponsive) === "function" ? responsive.hooks.useResponsive : useFallbackResponsive;
    const ResponsiveOverrideContext = (_d = responsive == null ? void 0 : responsive.components) == null ? void 0 : _d.ResponsiveOverrideContext;
    const ResponsiveOverrideCtx = ResponsiveOverrideContext && typeof ResponsiveOverrideContext === "object" ? ResponsiveOverrideContext : FallbackResponsiveOverrideContext;
    const info = useResponsiveHook() || FALLBACK_RESPONSIVE_INFO;
    const overrideCtx = (0, import_react.useContext)(ResponsiveOverrideCtx);
    const node = (0, import_react.useMemo)(
      () => ({
        componentName: inferredName,
        id: rawProps.id,
        className: rawProps.className
      }),
      [rawProps.id, rawProps.className]
    );
    const finalProps = (0, import_react.useMemo)(() => {
      if (overrideCtx == null ? void 0 : overrideCtx.resolveProps) {
        return overrideCtx.resolveProps(node, rawProps);
      }
      if (typeof (responsive == null ? void 0 : responsive.resolveProps) === "function") {
        return responsive.resolveProps(node, rawProps);
      }
      const next = { ...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]);
    const enableEdit = (options == null ? void 0 : options.editable) !== false && !!(overrideCtx == null ? void 0 : overrideCtx.editMode);
    if (enableEdit) {
      return /* @__PURE__ */ import_react.default.createElement(
        import_ResponsiveEditWrapper.default,
        {
          node,
          finalProps,
          editableFields: options == null ? void 0 : options.editableFields
        },
        /* @__PURE__ */ import_react.default.createElement(ComponentWithRef, { ...finalProps, ref })
      );
    }
    return /* @__PURE__ */ import_react.default.createElement(ComponentWithRef, { ...finalProps, ref });
  });
  Wrapped.displayName = `withResponsive(${inferredName})`;
  return Wrapped;
}
var withResponsive_default = withResponsive;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  withResponsive
});
