import { s as $, r, i as w, a as z, b as J, c as Q, T as U, C as Z, m as ee } from "./index-CQdU3574.js";
const te = () => (t) => t.targetX, re = () => (t) => t.targetY, ne = () => (t) => t.targetWidth, ie = () => (t) => t.targetHeight, se = () => (t) => t.targetY + 10, ae = () => (t) => Math.max(0, (t.targetHeight - 28) / 2), oe = /* @__PURE__ */ $("div")({
  name: "DataGridOverlayEditorStyle",
  class: "gdg-d19meir1",
  propsAsIs: !1,
  vars: {
    "d19meir1-0": [re(), "px"],
    "d19meir1-1": [te(), "px"],
    "d19meir1-2": [ne(), "px"],
    "d19meir1-3": [ie(), "px"],
    "d19meir1-4": [se(), "px"],
    "d19meir1-5": [ae(), "px"]
  }
});
function de() {
  const [t, s] = r.useState();
  return [t ?? void 0, s];
}
function le() {
  const [t, s] = de(), [i, y] = r.useState(0), [m, x] = r.useState(!0);
  r.useLayoutEffect(() => {
    if (t === void 0 || !("IntersectionObserver" in window))
      return;
    const a = new IntersectionObserver((o) => {
      o.length !== 0 && x(o[0].isIntersecting);
    }, { threshold: 1 });
    return a.observe(t), () => a.disconnect();
  }, [t]), r.useEffect(() => {
    if (m || t === void 0)
      return;
    let a;
    const o = () => {
      const { right: S } = t.getBoundingClientRect();
      y((p) => Math.min(p + window.innerWidth - S - 10, 0)), a = requestAnimationFrame(o);
    };
    return a = requestAnimationFrame(o), () => {
      a !== void 0 && cancelAnimationFrame(a);
    };
  }, [t, m]);
  const O = r.useMemo(() => ({ transform: `translateX(${i}px)` }), [i]);
  return {
    ref: s,
    style: O
  };
}
const ce = (t) => {
  const { target: s, content: i, onFinishEditing: y, forceEditMode: m, initialValue: x, imageEditorOverride: O, markdownDivCreateNode: a, highlight: o, className: S, theme: p, id: H, cell: v, bloom: c, validateCell: d, getCellRenderer: F, provideEditor: h, isOutsideClick: X } = t, [l, A] = r.useState(m ? i : void 0), k = r.useRef(l ?? i);
  k.current = l ?? i;
  const [E, R] = r.useState(() => d === void 0 ? !0 : !(w(i) && (d == null ? void 0 : d(v, i, k.current)) === !1)), f = r.useCallback((e, n) => {
    y(E ? e : void 0, n);
  }, [E, y]), K = r.useCallback((e) => {
    if (d !== void 0 && e !== void 0 && w(e)) {
      const n = d(v, e, k.current);
      n === !1 ? R(!1) : (typeof n == "object" && (e = n), R(!0));
    }
    A(e);
  }, [v, d]), C = r.useRef(!1), g = r.useRef(void 0), W = r.useCallback(() => {
    f(l, [0, 0]), C.current = !0;
  }, [l, f]), Y = r.useCallback((e, n) => {
    f(e, n ?? g.current ?? [0, 0]), C.current = !0;
  }, [f]), j = r.useCallback(async (e) => {
    let n = !1;
    e.key === "Escape" ? (e.stopPropagation(), e.preventDefault(), g.current = [0, 0]) : e.key === "Enter" && !e.shiftKey ? (e.stopPropagation(), e.preventDefault(), g.current = [0, 1], n = !0) : e.key === "Tab" && (e.stopPropagation(), e.preventDefault(), g.current = [e.shiftKey ? -1 : 1, 0], n = !0), window.setTimeout(() => {
      !C.current && g.current !== void 0 && (f(n ? l : void 0, g.current), C.current = !0);
    }, 0);
  }, [f, l]), D = l ?? i, [u, q] = r.useMemo(() => {
    var n, G;
    if (z(i))
      return [];
    const e = h == null ? void 0 : h(i);
    return e !== void 0 ? [e, !1] : [(G = (n = F(i)) == null ? void 0 : n.provideEditor) == null ? void 0 : G.call(n, i), !1];
  }, [i, F, h]), { ref: B, style: L } = le();
  let P = !0, M, _ = !0, b;
  if (u !== void 0) {
    P = u.disablePadding !== !0, _ = u.disableStyling !== !0;
    const e = J(u);
    e && (b = u.styleOverride);
    const n = e ? u.editor : u;
    M = r.createElement(n, { isHighlighted: o, onChange: K, value: D, initialValue: x, onFinishedEditing: Y, validatedSelection: w(D) ? D.selectionRange : void 0, forceEditMode: m, target: s, imageEditorOverride: O, markdownDivCreateNode: a, isValid: E, theme: p });
  }
  b = { ...b, ...L };
  const N = document.getElementById("portal");
  if (N === null)
    return console.error('Cannot open Data Grid overlay editor, because portal not found.  Please add `<div id="portal" />` as the last child of your `<body>`.'), null;
  let I = _ ? "gdg-style" : "gdg-unstyle";
  E || (I += " gdg-invalid"), P && (I += " gdg-pad");
  const T = (c == null ? void 0 : c[0]) ?? 1, V = (c == null ? void 0 : c[1]) ?? 1;
  return Q.createPortal(r.createElement(
    U.Provider,
    { value: p },
    r.createElement(
      Z,
      { style: ee(p), className: S, onClickOutside: W, isOutsideClick: X },
      r.createElement(
        oe,
        { ref: B, id: H, className: I, style: b, as: q === !0 ? "label" : void 0, targetX: s.x - T, targetY: s.y - V, targetWidth: s.width + T * 2, targetHeight: s.height + V * 2 },
        r.createElement("div", { className: "gdg-clip-region", onKeyDown: j }, M)
      )
    )
  ), N);
};
export {
  ce as default
};
//# sourceMappingURL=data-grid-overlay-editor-yBT-frRQ.js.map
