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