import { jsx as n, jsxs as k, Fragment as U } from "react/jsx-runtime";
import { useRef as M, useContext as W, useMemo as K, useEffect as $, useState as L, useCallback as y, Suspense as q } from "react";
import { r as A, e as z, l as G, f as H, g as J, o as Q, h as P, N as R, b as j, i as V, d as B, j as X, O as Y, I, k as Z, A as ee, D as te, B as ne, n as ae, p as oe, L as re, F as se, q as le, s as ie, v as ce } from "./layers.wyaZ1wNd.js";
import { Button as de, TextInput as ue } from "@mantine/core";
function F(e) {
  const a = e.transform();
  return new Set(a ? [a] : []);
}
function me({ initialEditor: e, children: a, initialNodes: o, initialTheme: f, skipCollabChecks: r, skipEditableListener: l }) {
  const s = M(!1), E = W(A);
  E == null && (function(m, ...t) {
    const v = new URL("https://lexical.dev/docs/error"), b = new URLSearchParams();
    b.append("code", m);
    for (const i of t) b.append("v", i);
    throw v.search = b.toString(), Error(`Minified Lexical error #${m}; visit ${v.toString()} for the full message or use the non-minified dev environment for full errors and additional helpful warnings.`);
  })(9);
  const [u, { getTheme: w }] = E, S = K((() => {
    const m = f || w() || void 0, t = z(E, m);
    m !== void 0 && (e._config.theme = m), e._parentEditor = e._parentEditor || u;
    const v = e._createEditorArgs, b = v && v.namespace;
    if (o) {
      b || (e._config.namespace = u._config.namespace);
      for (let i of o) {
        let C = null, g = null;
        if (typeof i != "function") {
          const D = i;
          i = D.replace, C = D.with, g = D.withKlass || null;
        }
        const N = e._nodes.get(i.getType());
        e._nodes.set(i.getType(), { exportDOM: N ? N.exportDOM : void 0, klass: i, replace: C, replaceWithKlass: g, transforms: F(i) });
      }
    } else if (v && v.nodes) b || (e._config.namespace = u._config.namespace);
    else {
      const i = e._nodes = new Map(u._nodes);
      b || (e._config.namespace = u._config.namespace);
      for (const [C, g] of i) e._nodes.set(C, { exportDOM: g.exportDOM, klass: g.klass, replace: g.replace, replaceWithKlass: g.replaceWithKlass, transforms: F(g.klass) });
    }
    return [e, t];
  }), []), { isCollabActive: x, yjsDocMap: p } = G(), d = r || s.current || p.has(e.getKey());
  return $((() => {
    d && (s.current = !0);
  }), [d]), $((() => {
    if (!l) {
      const m = (t) => e.setEditable(t);
      return m(u.isEditable()), u.registerEditableListener(m);
    }
  }), [e, u, l]), n(A.Provider, { value: S, children: !x || d ? a : null });
}
function fe({
  "data-test-id": e,
  children: a,
  className: o,
  onClick: f,
  disabled: r,
  small: l,
  title: s
}) {
  return /* @__PURE__ */ n(de, { onClick: f, disabled: r, variant: "filled", children: a });
}
function pe({
  children: e,
  className: a
}) {
  return /* @__PURE__ */ n("div", { className: a || "Placeholder__root", children: e });
}
function ge({
  label: e,
  value: a,
  onChange: o,
  placeholder: f = "",
  "data-test-id": r,
  type: l = "text"
}) {
  return /* @__PURE__ */ n(
    ue,
    {
      type: l,
      placeholder: f,
      value: a,
      onChange: (s) => {
        o(s.target.value);
      },
      label: e,
      "data-test-id": r,
      mb: 10
    }
  );
}
const O = /* @__PURE__ */ new Set();
function he(e) {
  if (!O.has(e))
    throw new Promise((a) => {
      const o = new Image();
      o.src = e, o.onload = () => {
        O.add(e), a(null);
      };
    });
}
function ve({
  altText: e,
  className: a,
  imageRef: o,
  src: f,
  width: r,
  height: l,
  position: s
}) {
  return he(f), /* @__PURE__ */ n(
    "img",
    {
      className: a || void 0,
      src: f,
      alt: e,
      ref: o,
      "data-position": s,
      style: {
        display: "block",
        height: l,
        width: r
      },
      draggable: "false"
    }
  );
}
function be({
  activeEditor: e,
  nodeKey: a,
  onClose: o
}) {
  const r = e.getEditorState().read(
    () => j(a)
  ), [l, s] = L(r.getAltText()), [E, u] = L(r.getShowCaption()), [w, S] = L(r.getPosition()), x = (d) => {
    u(d.target.checked);
  }, p = () => {
    const d = { altText: l, position: w, showCaption: E };
    r && e.update(() => {
      r.update(d);
    }), o();
  };
  return /* @__PURE__ */ k(U, { children: [
    /* @__PURE__ */ n("div", { style: { marginBottom: "1em" }, children: /* @__PURE__ */ n(
      ge,
      {
        label: "Alt-текст",
        placeholder: "Альтернативный текст",
        onChange: s,
        value: l,
        "data-test-id": "image-modal-alt-text-input"
      }
    ) }),
    /* @__PURE__ */ k("div", { className: "Input__wrapper", children: [
      /* @__PURE__ */ n(
        "input",
        {
          id: "caption",
          type: "checkbox",
          checked: E,
          onChange: x
        }
      ),
      /* @__PURE__ */ n("label", { htmlFor: "caption", children: "Show Caption" })
    ] }),
    /* @__PURE__ */ n(
      fe,
      {
        "data-test-id": "image-modal-file-upload-btn",
        onClick: () => p(),
        children: "Применить"
      }
    )
  ] });
}
function Ie({
  src: e,
  altText: a,
  nodeKey: o,
  width: f,
  height: r,
  showCaption: l,
  caption: s,
  position: E
}) {
  const [u, w] = H(), S = M(null), x = M(null), [p, d, m] = J(o), [t] = Q(), [v, b] = L(null), i = M(null), C = y(
    (h) => {
      if (p && P(R())) {
        h.preventDefault();
        const c = j(o);
        V(c) && c.remove();
      }
      return !1;
    },
    [p, o]
  ), g = y(
    (h) => {
      const _ = R(), c = x.current;
      if (p && P(_) && _.getNodes().length === 1) {
        if (l)
          return B(null), h.preventDefault(), s.focus(), !0;
        if (c !== null && c !== document.activeElement)
          return h.preventDefault(), c.focus(), !0;
      }
      return !1;
    },
    [s, p, l]
  ), N = y(
    (h) => i.current === s || x.current === h.target ? (B(null), t.update(() => {
      d(!0);
      const _ = t.getRootElement();
      _ !== null && _.focus();
    }), !0) : !1,
    [s, t, d]
  );
  return $(() => {
    let h = !0;
    const _ = X(
      t.registerUpdateListener(({ editorState: c }) => {
        h && b(c.read(() => R()));
      }),
      t.registerCommand(
        oe,
        (c, T) => (i.current = T, !1),
        I
      ),
      t.registerCommand(
        ae,
        (c) => {
          const T = c;
          return T.target === S.current ? (T.shiftKey ? d(!p) : (m(), d(!0)), !0) : !1;
        },
        I
      ),
      t.registerCommand(
        ne,
        (c) => c.target === S.current ? (c.preventDefault(), !0) : !1,
        I
      ),
      t.registerCommand(
        te,
        C,
        I
      ),
      t.registerCommand(
        ee,
        C,
        I
      ),
      t.registerCommand(Z, g, I),
      t.registerCommand(
        Y,
        N,
        I
      )
    );
    return () => {
      h = !1, _();
    };
  }, [
    m,
    t,
    p,
    o,
    C,
    g,
    N,
    d
  ]), /* @__PURE__ */ k(q, { fallback: null, children: [
    /* @__PURE__ */ k(U, { children: [
      /* @__PURE__ */ k("div", { draggable: !1, children: [
        /* @__PURE__ */ n(
          "button",
          {
            className: "image-edit-button",
            ref: x,
            onClick: () => {
              w("Update Inline Image", (h) => /* @__PURE__ */ n(
                be,
                {
                  activeEditor: t,
                  nodeKey: o,
                  onClose: h
                }
              ));
            },
            children: "Edit"
          }
        ),
        /* @__PURE__ */ n(
          ve,
          {
            className: p ? `focused ${P(v) ? "draggable" : ""}` : null,
            src: e,
            altText: a,
            imageRef: S,
            width: f,
            height: r,
            position: E
          }
        )
      ] }),
      l && /* @__PURE__ */ n("div", { className: "image-caption-container", children: /* @__PURE__ */ k(me, { initialEditor: s, children: [
        /* @__PURE__ */ n(re, {}),
        /* @__PURE__ */ n(se, {}),
        /* @__PURE__ */ n(
          le,
          {
            contentEditable: /* @__PURE__ */ n(ce, { className: "InlineImageNode__contentEditable" }),
            placeholder: /* @__PURE__ */ n(pe, { className: "InlineImageNode__placeholder", children: "Enter a caption..." }),
            ErrorBoundary: ie
          }
        )
      ] }) })
    ] }),
    u
  ] });
}
export {
  be as UpdateInlineImageDialog,
  Ie as default
};
