import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { Button, Modal, Row } from "antd";
import { Col, Form, Input, Select } from "antd-latest";
import { CrossCircle } from "../../../../Components/Icons/CrossCircle";
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);
        }
    };
    const exForm = _jsx(_Fragment, {});
    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: "u-dao-view-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: "nl-white-box-modal view-proposal-vote-ctn", footer: [], children: [_jsxs(Col, { className: "view-proposal-vote-ctn", children: [_jsx("h1", { className: "view-proposal-vote-h1", children: "Give your vote " }), _jsx("p", { className: "view-proposal-vote-p", children: "Vote on this proposal! \uD83D\uDC9A \uD83D\uDD2E" }), _jsx(Form.Item, { name: "option", children: _jsxs(Select, { style: { height: 55 }, bordered: false, placeholder: "topic", defaultOpen: true, children: [_jsx(Select.Option, { value: "YES", children: _jsx(Button, { className: "view-proposal-vote-btn-yes", type: "primary", onClick: () => setMode(VOTE_STEPS.LOCK), children: "Yes" }) }), _jsx(Select.Option, { value: "NO", children: _jsx(Button, { className: "view-proposal-vote-btn-no", type: "primary", onClick: () => setMode(VOTE_STEPS.LOCK), children: "No" }) })] }) })] }), _jsx("p", { className: "paragraph-2r", children: "This is only on Testnet! Need help? Join our telegram group!" })] }), _jsx(Modal, { closeIcon: _jsx(CrossCircle, {}), visible: _mode == VOTE_STEPS.LOCK, onOk: () => setMode(VOTE_STEPS.CONFIRM), onCancel: () => setMode(VOTE_STEPS.DISABLED), className: "nl-white-box-modal view-proposal-vote-ctn", footer: [], children: _jsxs(Col, { className: "view-proposal-vote-ctn", children: [_jsx("h1", { className: "view-proposal-vote-h1", children: "Give your vote " }), _jsxs("p", { className: "view-proposal-vote-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("div", { className: "text-center", children: _jsxs(Button, { disabled: !quantityState, type: "primary", onClick: () => {
                                                    setMode(VOTE_STEPS.CONFIRM);
                                                }, children: [" ", "Lock"] }) }), _jsx("p", { className: "paragraph-2r nl-vertical-space-self", children: "This is only on Testnet! Need help? Join our telegram group!" })] }) }), _jsx(Modal, { closeIcon: _jsx(CrossCircle, {}), visible: _mode == VOTE_STEPS.CONFIRM, onOk: () => setMode(VOTE_STEPS.VOTE), onCancel: () => setMode(VOTE_STEPS.DISABLED), className: "nl-white-box-modal view-proposal-vote-ctn", footer: [], children: _jsxs(Col, { className: "view-proposal-vote-ctn", children: [_jsx("h1", { className: "view-proposal-vote-h1", children: "Confirm your vote " }), _jsxs("p", { className: "view-proposal-vote-p", children: ["Are you sure you want to lock ", quantityState, " $", ticker, " to vote ", voteValState, "?"] }), _jsx(Row, { justify: "center", children: _jsx(Button, { className: "view-proposal-vote-btn-yes", type: "primary", htmlType: "submit", onClick: () => setMode(VOTE_STEPS.VOTE), children: "Confirm" }) }), _jsx(Row, { justify: "center", children: _jsx(Button, { className: "view-proposal-vote-btn-no", type: "primary", onClick: () => setMode(VOTE_STEPS.DISABLED), children: "Cancel" }) }), _jsxs("p", { className: "paragraph-2r", children: ["This is only on Testnet! Need help?", _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: "nl-white-box-modal view-proposal-vote-ctn", footer: [], children: [_jsxs("h1", { className: "view-proposal-vote-h1", children: ["You Selected ", voteValState, "! "] }), _jsxs("p", { className: "view-proposal-vote-p", children: ["You are locking ", _jsx("br", {}), _jsx("br", {}), _jsxs("span", { className: "header-1r", children: [quantityState, " $", `${ticker}`] })] }), _jsx("p", { className: "paragraph-2r", children: "This is only on Testnet! Need help? Join our telegram group!" }), _jsxs(ProgressButton, { actionName: "newcoin.daoVoteProposal", progressText: "Voting...", type: "primary", onClick: vote, children: [" ", "Vote"] })] })] })] }))] }));
};
//# sourceMappingURL=VoteModal.js.map