import { G as O, a0 as S, S as C, N as g, a2 as j, av as P, _ as I, a8 as D, a6 as f, a9 as E, aa as V, j as t, au as $, aw as k, ax as R } from "./index-UNZzl66r.js";
import { useMemo as y, useState as A, useRef as b, useCallback as T, useEffect as W } from "react";
import { l as w } from "./LexicalCollaborationContext.prod-CKLiaJe8.js";
function B(s) {
  return s.reduce((a, o) => a + o.votes.length, 0);
}
function F({
  option: s,
  index: a,
  options: o,
  totalVotes: c,
  withPollNode: p
}) {
  const { clientID: r } = w(), _ = b(null), m = s.votes, h = m.indexOf(r) !== -1, i = m.length, x = s.text;
  return /* @__PURE__ */ t.jsxs("div", { className: "PollNode__optionContainer", children: [
    /* @__PURE__ */ t.jsx(
      "div",
      {
        className: k(
          "PollNode__optionCheckboxWrapper",
          h && "PollNode__optionCheckboxChecked"
        ),
        children: /* @__PURE__ */ t.jsx(
          "input",
          {
            ref: _,
            className: "PollNode__optionCheckbox",
            type: "checkbox",
            onChange: () => {
              p((d) => {
                d.toggleVote(s, r);
              });
            },
            checked: h
          }
        )
      }
    ),
    /* @__PURE__ */ t.jsxs("div", { className: "PollNode__optionInputWrapper", children: [
      /* @__PURE__ */ t.jsx(
        "div",
        {
          className: "PollNode__optionInputVotes",
          style: { width: `${i === 0 ? 0 : i / c * 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: x,
          onChange: (d) => {
            const u = d.target, N = u.value, e = u.selectionStart, n = u.selectionEnd;
            p(
              (l) => {
                l.setOptionText(s, N);
              },
              () => {
                u.selectionStart = e, u.selectionEnd = n;
              }
            );
          },
          placeholder: `Option ${a + 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 U({
  question: s,
  options: a,
  nodeKey: o
}) {
  const [c] = O(), p = y(() => B(a), [a]), [r, _, m] = S(o), [v, h] = A(null), i = b(null), x = T(
    (e) => {
      if (r && C(g())) {
        e.preventDefault();
        const l = j(o);
        P(l) && l.remove();
      }
      return !1;
    },
    [r, o]
  );
  W(() => I(
    c.registerUpdateListener(({ editorState: e }) => {
      h(e.read(() => g()));
    }),
    c.registerCommand(
      V,
      (e) => {
        const n = e;
        return n.target === i.current ? (n.shiftKey || m(), _(!r), !0) : !1;
      },
      f
    ),
    c.registerCommand(E, x, f),
    c.registerCommand(D, x, f)
  ), [m, c, r, o, x, _]);
  const d = (e, n) => {
    c.update(
      () => {
        const l = j(o);
        P(l) && e(l);
      },
      { onUpdate: n }
    );
  }, u = () => {
    d((e) => {
      e.addOption(R());
    });
  }, N = C(v) && r;
  return /* @__PURE__ */ t.jsx("div", { className: `PollNode__container ${N ? "focused" : ""}`, ref: i, children: /* @__PURE__ */ t.jsxs("div", { className: "PollNode__inner", children: [
    /* @__PURE__ */ t.jsx("h2", { className: "PollNode__heading", children: s }),
    a.map((e, n) => {
      const l = e.uid;
      return /* @__PURE__ */ t.jsx(
        F,
        {
          withPollNode: d,
          option: e,
          index: n,
          options: a,
          totalVotes: p
        },
        l
      );
    }),
    /* @__PURE__ */ t.jsx("div", { className: "PollNode__footer", children: /* @__PURE__ */ t.jsx($, { onClick: u, small: !0, children: "Add Option" }) })
  ] }) });
}
export {
  U as default
};
