import { o as O, h as S, n as C, p as g, k as j, Y as P, e as I, C as D, r as N, N as E, s as V, j as t, X as $, Z as k, _ as R } from "./index-Buqt2Jsq.js";
import { useMemo as y, useState as A, useRef as b, useCallback as T, useEffect as W } from "react";
import { l as B } from "./LexicalCollaborationContext.prod-CKLiaJe8.js";
function F(s) {
  return s.reduce((c, o) => c + o.votes.length, 0);
}
function L({
  option: s,
  index: c,
  options: o,
  totalVotes: a,
  withPollNode: p
}) {
  const { clientID: r } = B(), x = b(null), m = s.votes, _ = m.indexOf(r) !== -1, i = m.length, h = s.text;
  return /* @__PURE__ */ t.jsxs("div", { className: "PollNode__optionContainer", children: [
    /* @__PURE__ */ t.jsx(
      "div",
      {
        className: k(
          "PollNode__optionCheckboxWrapper",
          _ && "PollNode__optionCheckboxChecked"
        ),
        children: /* @__PURE__ */ t.jsx(
          "input",
          {
            ref: x,
            className: "PollNode__optionCheckbox",
            type: "checkbox",
            onChange: () => {
              p((d) => {
                d.toggleVote(s, r);
              });
            },
            checked: _
          }
        )
      }
    ),
    /* @__PURE__ */ t.jsxs("div", { className: "PollNode__optionInputWrapper", children: [
      /* @__PURE__ */ t.jsx(
        "div",
        {
          className: "PollNode__optionInputVotes",
          style: { width: `${i === 0 ? 0 : i / a * 100}%` }
        }
      ),
      /* @__PURE__ */ t.jsx("span", { className: "PollNode__optionInputVotesCount", children: i > 0 && (i === 1 ? "1 vote" : `${i} votes`) }),
      /* @__PURE__ */ t.jsx(
        "input",
        {
          className: "PollNode__optionInput",
          type: "text",
          value: h,
          onChange: (d) => {
            const u = d.target, f = u.value, e = u.selectionStart, n = u.selectionEnd;
            p(
              (l) => {
                l.setOptionText(s, f);
              },
              () => {
                u.selectionStart = e, u.selectionEnd = n;
              }
            );
          },
          placeholder: `Option ${c + 1}`
        }
      )
    ] }),
    /* @__PURE__ */ t.jsx(
      "button",
      {
        disabled: o.length < 3,
        className: k(
          "PollNode__optionDelete",
          o.length < 3 && "PollNode__optionDeleteDisabled"
        ),
        "aria-label": "Remove",
        onClick: () => {
          p((d) => {
            d.deleteOption(s);
          });
        }
      }
    )
  ] });
}
function Y({
  question: s,
  options: c,
  nodeKey: o
}) {
  const [a] = O(), p = y(() => F(c), [c]), [r, x, m] = S(o), [v, _] = A(null), i = b(null), h = T(
    (e) => {
      if (r && C(g())) {
        e.preventDefault();
        const l = j(o);
        P(l) && l.remove();
      }
      return !1;
    },
    [r, o]
  );
  W(() => I(
    a.registerUpdateListener(({ editorState: e }) => {
      _(e.read(() => g()));
    }),
    a.registerCommand(
      V,
      (e) => {
        const n = e;
        return n.target === i.current ? (n.shiftKey || m(), x(!r), !0) : !1;
      },
      N
    ),
    a.registerCommand(E, h, N),
    a.registerCommand(D, h, N)
  ), [m, a, r, o, h, x]);
  const d = (e, n) => {
    a.update(
      () => {
        const l = j(o);
        P(l) && e(l);
      },
      { onUpdate: n }
    );
  }, u = () => {
    d((e) => {
      e.addOption(R());
    });
  }, f = C(v) && r;
  return /* @__PURE__ */ t.jsx("div", { className: `PollNode__container ${f ? "focused" : ""}`, ref: i, children: /* @__PURE__ */ t.jsxs("div", { className: "PollNode__inner", children: [
    /* @__PURE__ */ t.jsx("h2", { className: "PollNode__heading", children: s }),
    c.map((e, n) => {
      const l = e.uid;
      return /* @__PURE__ */ t.jsx(
        L,
        {
          withPollNode: d,
          option: e,
          index: n,
          options: c,
          totalVotes: p
        },
        l
      );
    }),
    /* @__PURE__ */ t.jsx("div", { className: "PollNode__footer", children: /* @__PURE__ */ t.jsx($, { onClick: u, small: !0, children: "Add Option" }) })
  ] }) });
}
export {
  Y as default
};
