import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
import { Button, Modal } from "antd";
import { CrossCircle } from "../../../../Components/Icons/CrossCircle";
import { Form, Input } from "antd-latest";
import { NewcoinRecept } from "../../../../Components/Recepts";
import { ProgressButton } from "../../../../Components/ProgressButton";
import { VOTE_STEPS } from "../../../../overmind/flows/vote/state";
import { pad } from "../../../../utils/pad";
import { useActions, useAppState } from "../../../../overmind";
import { useCachedPool, useCachedUser } from "../../../../hooks/useCached";
import { useEffect, useState } from "react";
export const VoteModal = ({ quantity, option, proposalId, proposal_type, daoOwner: _do, visible }) => {
    const actions = useActions();
    const state = useAppState();
    const daoOwner = _do || state.config.settings.newcoin.daoDomain || "";
    const owner = useCachedUser({ username: daoOwner });
    const pool = useCachedPool({ owner: daoOwner });
    const ticker = owner.newcoinTicker?.toUpperCase();
    const normalizeQuantity = (q) => {
        const [whole, decimal] = q.split(/\./);
        const padded = pad(decimal || "", 4, "right");
        return [whole, padded].join(".");
    };
    const [form] = Form.useForm();
    const voteVal = Form.useWatch("option", form);
    const [tx, setTx] = useState("");
    const [voteValState, setVoteValState] = useState("");
    const [quantityState, setQuantityState] = useState("");
    const [_mode, setMode] = useState(VOTE_STEPS.DISABLED);
    const startVoting = () => setMode(VOTE_STEPS.SELECT);
    const endVoting = () => {
        setMode(VOTE_STEPS.DISABLED);
        actions.newcoin[proposal_type === "whitelist" ? "daoGetWhitelistProposals" : "daoGetProposals"]({ daoOwner });
    };
    useEffect(() => setVoteValState(voteVal), [voteVal]);
    const vote = async () => {
        const res = await actions.newcoin.daoVoteProposal({
            quantity: normalizeQuantity(quantityState || quantity) + " " + ticker,
            option: voteValState || option,
            proposal_id: proposalId?.toString(),
            dao_owner: daoOwner,
            proposal_type: proposal_type,
        });
        const success = !!res.TxID_voteDaoProposal;
        if (success) {
            setTx(res.TxID_voteDaoProposal);
            setMode(VOTE_STEPS.DONE);
        }
    };
    return (_jsxs(_Fragment, { children: [_jsxs(NewcoinRecept, { tx: tx, visible: _mode == VOTE_STEPS.DONE, onDone: endVoting, children: [" ", _jsx("h1", { className: "view-proposal-vote-h1", children: "Congratulations! " }), _jsxs("p", { className: "view-proposal-vote-p", children: ["You locked ", quantityState, " $", `${ticker}`, " to vote ", voteValState, " on this proposal!"] })] }), visible && (_jsxs(_Fragment, { children: [_jsx(Button, { className: "power-up-btn", onClick: startVoting, children: "Vote" }), _jsxs(Form, { form: form, name: "vote-proposal-form", children: [_jsxs(Modal, { closeIcon: _jsx(CrossCircle, {}), visible: _mode == VOTE_STEPS.SELECT, onOk: () => setMode(VOTE_STEPS.LOCK), onCancel: () => setMode(VOTE_STEPS.DISABLED), className: "modal-ctn", footer: [], children: [_jsx("h2", { children: "Give your vote!" }), _jsx("p", { children: "Vote on this proposal! \uD83D\uDC9A \uD83D\uDD2E" }), _jsx(Button, { className: "confirm-btn", onClick: () => {
                                            setVoteValState("YES");
                                            setMode(VOTE_STEPS.LOCK);
                                        }, children: "YES" }), _jsx(Button, { className: "cancel-btn", onClick: () => {
                                            setVoteValState("NO");
                                            setMode(VOTE_STEPS.LOCK);
                                        }, children: "NO" }), _jsxs("p", { children: ["This is only on Testnet! Need help? Join our telegram group!", _jsx("a", { href: "https://t.me/joinchat/Ezz_sQzaOK2j977siawwGQ", children: "Join our telegram group!" })] })] }), _jsxs(Modal, { closeIcon: _jsx(CrossCircle, {}), visible: _mode == VOTE_STEPS.LOCK, onOk: () => setMode(VOTE_STEPS.CONFIRM), onCancel: () => setMode(VOTE_STEPS.DISABLED), className: "modal-ctn", footer: [], children: [_jsx("h2", { children: "Give your vote!" }), _jsxs("p", { children: ["Lock a minimum of 1 $", pool.code, " to confirm your vote!"] }), _jsx(Form.Item, { name: "quantity", children: _jsx(Input, { placeholder: "100", suffix: `$${ticker}`, onChange: (e) => setQuantityState(e.target.value) }) }), _jsx(Button, { id: "vote-lock-btn", className: "confirm-btn", onClick: () => {
                                            setMode(VOTE_STEPS.CONFIRM);
                                        }, children: "Lock" }), _jsx(Button, { className: "cancel-btn", onClick: () => {
                                            setVoteValState("NO");
                                            setMode(VOTE_STEPS.DISABLED);
                                        }, children: "Cancel" }), _jsxs("p", { children: ["This is only on Testnet! Need help? Join our telegram group!", _jsx("a", { href: "https://t.me/joinchat/Ezz_sQzaOK2j977siawwGQ", children: "Join our telegram group!" })] })] }), _jsxs(Modal, { closeIcon: _jsx(CrossCircle, {}), visible: _mode == VOTE_STEPS.CONFIRM, onOk: () => setMode(VOTE_STEPS.VOTE), onCancel: () => setMode(VOTE_STEPS.DISABLED), className: "modal-ctn", footer: [], children: [_jsx("h2", { children: "Confirm your vote! " }), _jsxs("p", { children: ["Are you sure you want to lock ", quantityState, " $", ticker, " to vote ", voteValState] }), _jsx(Button, { id: "vote-confirm-btn", className: "confirm-btn", onClick: () => {
                                            setMode(VOTE_STEPS.VOTE);
                                        }, children: "Confirm" }), _jsx(Button, { className: "cancel-btn", onClick: () => {
                                            setVoteValState("NO");
                                            setMode(VOTE_STEPS.DISABLED);
                                        }, children: "Cancel" }), _jsxs("p", { children: ["This is only on Testnet! Need help? Join our telegram group!", _jsx("a", { href: "https://t.me/joinchat/Ezz_sQzaOK2j977siawwGQ", children: "Join our telegram group!" })] })] }), _jsxs(Modal, { closeIcon: _jsx(CrossCircle, {}), visible: _mode == VOTE_STEPS.VOTE, onOk: () => setMode(VOTE_STEPS.DONE), onCancel: () => setMode(VOTE_STEPS.DISABLED), className: "modal-ctn", footer: [], children: [_jsxs("h2", { children: [" You Selected ", voteValState, "! "] }), _jsxs("p", { children: ["You are locking ", _jsx("br", {}), _jsx("br", {}), _jsxs("span", { className: "header-1r", children: [quantityState, " $", `${ticker}`] })] }), _jsxs(ProgressButton, { id: "vote-submit-btn", actionName: "newcoin.daoVoteProposal", progressText: "Voting...", onClick: vote, className: "confirm-btn", children: [" ", "Vote"] })] })] })] }))] }));
};
//# sourceMappingURL=VoteModal.js.map