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