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/ResponsiveEditWrapper.tsx
var ResponsiveEditWrapper_exports = {};
__export(ResponsiveEditWrapper_exports, {
  ResponsiveEditWrapper: () => ResponsiveEditWrapper,
  default: () => ResponsiveEditWrapper_default
});
module.exports = __toCommonJS(ResponsiveEditWrapper_exports);
var import_react = __toESM(require("react"));
var import_useEngineContext = __toESM(require("../hooks/useEngineContext"));
var LONG_PRESS_MS = 600;
var MOVE_CANCEL_PX = 8;
var FALLBACK_EDITABLE_FIELDS = ["variant", "className", "id", "style"];
var ResponsiveEditWrapper = ({
  children,
  node,
  finalProps,
  editableFields,
  as,
  className,
  style
}) => {
  var _a, _b, _c;
  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 = (_c = getApp == null ? void 0 : getApp()) == null ? void 0 : _c.app;
  const timerRef = (0, import_react.useRef)(null);
  const startPointRef = (0, import_react.useRef)(null);
  const cancel = (0, import_react.useCallback)(() => {
    if (timerRef.current != null) {
      clearTimeout(timerRef.current);
      timerRef.current = null;
    }
    startPointRef.current = null;
  }, []);
  (0, import_react.useEffect)(() => () => cancel(), [cancel]);
  const handlePointerDown = (0, import_react.useCallback)(
    (e) => {
      cancel();
      startPointRef.current = { x: e.clientX, y: e.clientY };
      const ev = e.nativeEvent;
      timerRef.current = setTimeout(() => {
        var _a2, _b2;
        timerRef.current = null;
        try {
          (_a2 = ev.preventDefault) == null ? void 0 : _a2.call(ev);
        } catch {
        }
        const plugin = app == null ? void 0 : app.responsive;
        const currentBreakpoint = typeof (plugin == null ? void 0 : plugin.getInfo) === "function" ? ((_b2 = plugin.getInfo()) == null ? void 0 : _b2.currentBreakpoint) ?? "" : "";
        const fields = (typeof (plugin == null ? void 0 : plugin.getEditableFieldsFor) === "function" ? plugin.getEditableFieldsFor(node, { extra: editableFields }) : void 0) || (editableFields && editableFields.length ? [...editableFields] : [...FALLBACK_EDITABLE_FIELDS]);
        const payload = {
          componentName: node.componentName,
          id: node.id,
          className: node.className,
          props: finalProps,
          currentBreakpoint,
          editableFields: fields
        };
        if (typeof (plugin == null ? void 0 : plugin.publishEditOpen) === "function") {
          plugin.publishEditOpen(payload);
        }
      }, LONG_PRESS_MS);
    },
    [app, cancel, node, finalProps, editableFields]
  );
  const handlePointerMove = (0, import_react.useCallback)(
    (e) => {
      const start = startPointRef.current;
      if (!start) return;
      const dx = e.clientX - start.x;
      const dy = e.clientY - start.y;
      if (Math.hypot(dx, dy) > MOVE_CANCEL_PX) cancel();
    },
    [cancel]
  );
  const wrapperStyle = (0, import_react.useMemo)(
    () => ({
      outline: "1px dashed rgba(24, 144, 255, 0.6)",
      outlineOffset: -1,
      cursor: "pointer",
      WebkitUserSelect: "none",
      userSelect: "none",
      WebkitTouchCallout: "none",
      ...style
    }),
    [style]
  );
  const Tag = as || "div";
  return /* @__PURE__ */ import_react.default.createElement(
    Tag,
    {
      className,
      style: wrapperStyle,
      onPointerDown: handlePointerDown,
      onPointerMove: handlePointerMove,
      onPointerUp: cancel,
      onPointerCancel: cancel,
      onPointerLeave: cancel,
      "data-responsive-edit": "1",
      "data-component-name": node.componentName || void 0,
      "data-component-id": node.id || void 0
    },
    children
  );
};
var ResponsiveEditWrapper_default = ResponsiveEditWrapper;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  ResponsiveEditWrapper
});
