import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { Button, DatePicker, Form, Input, Row, Select, Tooltip } from "antd-latest";
import { ContentLayout } from "../../../../Components/ContentLayout";
import { DownArrow } from "../../Components/Icons";
import { GetWhitelistedModal } from "../../Components/Modals/GetWhitelistedModal";
import { NewcoinRecept } from "../../../../Components/Recepts";
import { ProgressButton } from "../../../../Components/ProgressButton";
import { RowCheckbox } from "../../../../Components/RowCheckbox";
import { SearchWidget } from "../../../Search/SearchWidget";
import { useActions, useAppState } from "../../../../overmind";
import { useCachedDaoWhitelist } from "../../../../hooks/useCached";
import { useForm } from "antd-latest/lib/form/Form";
import { useParams } from "react-router-dom";
import { useState } from "react";
import moment from "moment";
const NewProposal = () => {
    const state = useAppState();
    const [form] = useForm();
    const actions = useActions();
    const proposalType = Form.useWatch("proposal_type", form);
    const whitelistSelection = Form.useWatch("whitelist_selection", form);
    const { daoOwner } = useParams();
    const ownDao = state.api.auth.user?.username === daoOwner;
    const [proposalCreated, setProposalCreated] = useState(false);
    // const [selection, setSelection] = useState<string>("");
    const [customTime, setCustomTime] = useState(false);
    const [voteEnd, setVoteEnd] = useState({});
    const [tx, setTx] = useState("");
    const [autoApprove, setAutoApprove] = useState({ consent: false, id: "" });
    const autoApproveConsentInfo = "Since you are the DAO owner, you can choose to approve this proposal on creation. If you'd rather approve this proposal later, leave the box unchecked.";
    const isWhitelisted = ownDao || useCachedDaoWhitelist(daoOwner).rows?.some((user) => user == state.api.auth.user?.username);
    const proposalCreate = async (val) => {
        const voteEndTime = val.vote_dates ? val.vote_dates : voteEnd ? voteEnd.time : moment().add("6", "minutes");
        const proposalAction = proposalType == "whitelist" ? "daoCreateWhitelistProposal" : "daoCreateProposal";
        const types = {
            whitelist: {
                user: val.whitelist_selection,
            },
            standard: {
                summary: val.summary + "#" + proposalType,
                url: val.url || "blank",
            },
        };
        const payLoad = proposalType == "whitelist" ? types.whitelist : types.standard;
        const res = await actions.newcoin[proposalAction]({
            ...val,
            ...payLoad,
            vote_start: moment().add("1", "minutes").utc().format("YYYY-MM-DDTHH:mm:ss"),
            vote_end: voteEndTime.utc().format("YYYY-MM-DDTHH:mm:ss"),
            dao_owner: daoOwner,
            user: val.whitelist_selection,
        });
        const success = !!res.TxID_createDaoProposal;
        if (success) {
            setTx(res["TxID_createDaoProposal"]);
            setProposalCreated(true);
            if (val.auto_approve)
                setAutoApprove({ consent: true, id: res.proposal_id });
        }
    };
    const createApprove = async () => {
        proposalType == "whitelist"
            ? await actions.newcoin.daoApproveWhitelistProposal({ dao_owner: daoOwner, proposal_id: autoApprove.id })
            : await actions.newcoin.daoApproveProposal({
                dao_owner: daoOwner,
                proposal_id: autoApprove.id,
            });
        actions.routing.historyPush({ location: `/dao/${daoOwner}/proposals` });
    };
    return (_jsx(ContentLayout, { children: _jsxs(Form, { className: "new-proposal-form", name: "new-proposal-form", form: form, onFinish: proposalCreate, autoComplete: "off", children: [!isWhitelisted && _jsx(GetWhitelistedModal, { daoOwner: daoOwner }), _jsx(Row, { justify: "center", children: _jsx("p", { className: "new-proposal-title", children: proposalType == "whitelist" ? "new member" : "new proposal" }) }), _jsx(Form.Item, { name: "proposal_type", children: _jsxs(Select, { showArrow: true, bordered: false, defaultValue: "feature", className: "type-dropdown", suffixIcon: DownArrow, children: [_jsx(Select.Option, { value: "feature", children: "feature request" }), _jsx(Select.Option, { value: "whitelist", children: "new members" }), _jsx(Select.Option, { value: "org-improvement", children: "org improvement" })] }) }), !(proposalType == "whitelist") && (_jsx(Form.Item, { name: "title", rules: [{ required: true }], children: _jsx(Input, { placeholder: "title" }) })), proposalType == "whitelist" && (_jsx(Form.Item, { name: "whitelist_selection", rules: [{ required: true }], children: _jsx(SearchWidget, { searchUsers: true, searchTags: false, showSearch: true, noNavigation: true }) })), _jsx("p", { className: "u-margin-top-large u-margin-bottom-30 paragraph-2b", children: "How long is your proposal live for?" }), _jsxs(Row, { align: "middle", className: "vote-time-btns", children: [_jsxs(Button, { className: `time-select-btn-${voteEnd.btn == "day" ? "active" : "inactive"}`, onClick: () => {
                                setVoteEnd({ btn: "day", time: moment().add("1", "day") });
                                customTime && setCustomTime(false);
                            }, children: [" ", "One Day", " "] }), _jsxs(Button, { className: `time-select-btn-${voteEnd.btn == "week" ? "active" : "inactive"}`, onClick: () => {
                                setVoteEnd({ btn: "week", time: moment().add("1", "week") });
                                customTime && setCustomTime(false);
                            }, children: [" ", "One Week", " "] }), _jsxs(Button, { className: `time-select-btn-${voteEnd.btn == "month" ? "active" : "inactive"}`, onClick: () => {
                                setVoteEnd({ btn: "month", time: moment().add("1", "month") });
                                customTime && setCustomTime(false);
                            }, children: [" ", "One Month", " "] }), _jsxs(Button, { className: `time-select-btn-${voteEnd.btn == "custom" ? "active" : "inactive"}`, onClick: () => {
                                setVoteEnd({ btn: "custom", time: null });
                                setCustomTime(true);
                            }, children: [" ", "Custom", " "] })] }), customTime && (_jsx(Form.Item, { name: "vote_dates", children: _jsx(DatePicker, { showTime: true, className: "new-proposal-custom-time", placeholder: "Vote End", format: "YYYY-MM-DD-HH:mm" }) })), !(proposalType == "whitelist") && (_jsx(Form.Item, { name: "summary", rules: [{ required: true }], children: _jsx(Input.TextArea, { className: "new-proposal-summary", placeholder: "Explain your proposal to the community!" }) })), !(proposalType == "whitelist") && (_jsx(Form.Item, { name: "url", children: _jsx(Input, { placeholder: "link" }) })), _jsxs("div", { className: "dao-checks", children: [_jsx(Form.Item, { className: "row-check", name: "dao-check", valuePropName: "checked", rules: [{ required: true }], children: _jsx(RowCheckbox, { children: _jsx("p", { className: "paragraph-2r", children: "I am fostering community health" }) }) }), ownDao && (_jsx(Form.Item, { name: "auto_approve", valuePropName: "checked", children: _jsx(RowCheckbox, { children: _jsx(Tooltip, { title: autoApproveConsentInfo, children: _jsx("p", { className: "paragraph-2r", children: "I consent to auto approval of my proposal" }) }) }) }))] }), _jsx(Row, { justify: "center", children: _jsxs(ProgressButton, { className: "share-proposal-btn", actionName: proposalType == "whitelist" ? "newcoin.daoCreateWhitelistProposal" : "newcoin.daoCreateProposal", progressText: "Sharing...", htmlType: "submit", children: [" ", "Share Proposal"] }) }), _jsxs(NewcoinRecept, { tx: tx, visible: proposalCreated, onDone: !autoApprove.consent ? () => actions.routing.historyPush({ location: `/dao/${daoOwner}/proposals` }) : createApprove, children: [_jsx("h1", { className: "view-proposal-vote-h1", children: "Congratulations! " }), _jsxs("p", { className: "view-proposal-vote-p", children: ["You Created a proposal and shared it to the ", daoOwner, " DAO!"] })] })] }) }));
};
export default NewProposal;
//# sourceMappingURL=NewProposal.js.map