import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
import { Row, Button, Col } from "antd";
import ListToggle from '../../Components/Icons/ListToggle';
import { Burger } from '../../Components/Icons/Burger';
import { Link, useParams } from "react-router-dom";
import { useActions, useAppState } from "../../overmind";
import { useState } from "react";
import { useCachedDaoProposals, useCachedDaoWhitelistProposals } from "../../hooks/useCached";
import { convertDaoProposalsForUI } from "./Utils/Helpers";
import { ProposalCard } from "./Components/ProposalCard";
import { ProposalRow } from "./Components/ProposalRow";
import { WhitelistProposalRow } from "./Components/WhitelistProposalRow";
import { LoadMore } from "../../Components/LoadMore";
import ArrowDown from "../../Components/Icons/ArrowDown";
import { Dropdown, Menu, Space } from 'antd-latest';
export const ProposalsPage = () => {
    const { daoOwner } = useParams();
    return _jsx(Proposals, { daoOwner: daoOwner });
};
const Proposals = (props) => {
    const state = useAppState();
    const actions = useActions();
    const currUser = state.api.auth.user?.username || "";
    const daoOwner = props.daoOwner || state.config.settings.newcoin.daoDomain;
    const ownDao = state.api.auth.user?.username === daoOwner;
    const daoProposals = useCachedDaoProposals({ daoOwner });
    const daoWhitelistProposals = useCachedDaoWhitelistProposals({ daoOwner });
    const [toggleView, setToggleView] = useState(false);
    const Toggle = () => setToggleView(!toggleView);
    const [showWhitelist, setShowWhitelist] = useState(false);
    const showWhitelistProposals = () => {
        setShowWhitelist(true);
    };
    const showStandardProposals = () => {
        setShowWhitelist(false);
    };
    const [sortSelection, setSortSelection] = useState("all");
    let proposals;
    if (sortSelection == "all") {
        !showWhitelist && (proposals = convertDaoProposalsForUI(daoProposals.rows));
        showWhitelist && (proposals = convertDaoProposalsForUI(daoWhitelistProposals.rows));
    }
    if (sortSelection == "byMe") {
        !showWhitelist && (proposals = convertDaoProposalsForUI(daoProposals.rows)?.filter(row => row.proposer == currUser));
        showWhitelist && (proposals = convertDaoProposalsForUI(daoWhitelistProposals.rows)?.filter(row => row.proposer == currUser));
    }
    if (sortSelection == "byOthers") {
        !showWhitelist && (proposals = convertDaoProposalsForUI(daoProposals.rows)?.filter(row => row.proposer != currUser));
        showWhitelist && (proposals = convertDaoProposalsForUI(daoWhitelistProposals.rows)?.filter(row => row.proposer != currUser));
    }
    if (sortSelection == "expired") {
        !showWhitelist && (proposals = convertDaoProposalsForUI(daoProposals.rows)?.filter(row => row.time_data.time_left_minutes < 0));
        showWhitelist && (proposals = convertDaoProposalsForUI(daoWhitelistProposals.rows)?.filter(row => row.time_data.time_left_minutes < 0));
    }
    if (sortSelection == "needsApproval") {
        !showWhitelist && (proposals = convertDaoProposalsForUI(daoProposals.rows)?.filter(row => row.status == "created" && row.time_data.time_left_minutes > 0));
        showWhitelist && (proposals = convertDaoProposalsForUI(daoWhitelistProposals.rows)?.filter(row => row.status = "created" && row.time_data.time_left_minutes > 0));
    }
    if (sortSelection == "needsExecution") {
        !showWhitelist && (proposals = convertDaoProposalsForUI(daoProposals.rows)?.filter(row => row.status == "approved" && row.time_data.time_left_minutes < 0 && (row.vote_no.quantity[0] !== "0" || row.vote_yes.quantity[0] != "0")));
        showWhitelist && (proposals = convertDaoProposalsForUI(daoWhitelistProposals.rows)?.filter(row => row.status == "approved" && row.time_data.time_left_minutes < 0 && (row.vote_no.quantity[0] !== "0" || row.vote_yes.quantity[0] !== "0")));
    }
    if (sortSelection == "needsVoting") {
        !showWhitelist && (proposals = convertDaoProposalsForUI(daoProposals.rows)?.filter(row => row.status == "approved" && row.time_data.time_left_minutes > 0));
        showWhitelist && (proposals = convertDaoProposalsForUI(daoWhitelistProposals.rows)?.filter(row => row.status == "approved" && row.time_data.time_left_minutes > 0));
    }
    const menu = (_jsx(Menu, { items: [
            {
                key: '1',
                label: (_jsx("p", { onClick: () => setSortSelection("needsApproval"), children: "Needs Approval" })),
            },
            {
                key: '2',
                label: (_jsx("p", { onClick: () => setSortSelection("needsVoting"), children: "Needs Votes" })),
            },
            {
                key: '3',
                label: (_jsx("p", { onClick: () => setSortSelection("needsExecution"), children: "Needs Execution" })),
            },
            {
                key: '4',
                label: (_jsx("p", { onClick: () => setSortSelection("expired"), children: "Expired" })),
            },
            {
                key: '5',
                label: (_jsx("p", { onClick: () => setSortSelection("all"), children: "See All" })),
            },
            {
                key: '6',
                label: (_jsx("p", { onClick: () => setSortSelection("byMe"), children: "By Me" }))
            },
            {
                key: '7',
                label: (_jsx("p", { onClick: () => setSortSelection("byOthers"), children: "By Others" }))
            },
        ] }));
    const proposalsState = (state.newcoin.daos[daoOwner] || {})[showWhitelist ? "whitelistProposals" : "proposals"] || {};
    const inProgress = !!(state.indicators.specific[`newcoin.${showWhitelist ? "daoGetProposals" : "daoGetWhitelistProposals"}`]);
    const shouldLoadMore = !inProgress && (!proposalsState || proposalsState?.more);
    const loadMore = () => {
        showWhitelist ?
            actions.newcoin.daoGetWhitelistProposals({ daoOwner }) :
            actions.newcoin.daoGetProposals({ daoOwner });
    };
    if (!inProgress && !daoProposals.dao_id) {
        return _jsx(_Fragment, { children: ownDao ?
                _jsxs(Col, { children: [_jsx("p", { children: "You have not created your DAO yet. Creating a DAO is easy, click below to start!" }), _jsx(Link, { to: "/dao/create", children: _jsx(Button, { className: "u-dao-view-btn", children: " Create Dao" }) })] }) :
                _jsxs(_Fragment, { children: ["User ", daoOwner, " had not created their dao yet."] }) });
    }
    return _jsxs(Col, { className: "u-dao-page-wrapper", children: [daoOwner, " DAO", _jsxs(Row, { className: "u-dao-menu-wrapper", justify: "space-between", children: [_jsxs(Row, { align: "middle", children: [_jsx("h2", { className: `proposal-list-menu-h2-${showWhitelist ? "inactive" : "active"}`, onClick: showStandardProposals, children: " Proposal History" }), _jsx("h2", { className: `proposal-list-menu-h2-${showWhitelist ? "active" : "inactive"}`, onClick: showWhitelistProposals, children: "  Members Proposals" }), _jsx(Link, { to: `/dao/${daoOwner}/whitelist/members`, className: `proposal-list-menu-h2-inactive`, children: " Members" }), _jsx(Dropdown, { overlay: menu, className: "proposal-list-menu-sort-ctn", children: _jsx("a", { onClick: e => e.preventDefault(), children: _jsxs(Space, { children: ["Sort By", _jsx(ArrowDown, {})] }) }) }), _jsx(Row, { onClick: Toggle, children: toggleView ? _jsx(Burger, {}) : _jsx(ListToggle, {}) })] }), _jsx(Button, { type: "primary", children: _jsx(Link, { to: `/dao/${daoOwner}/new-proposal`, children: "New Proposal" }) })] }), _jsxs(Col, { className: "u-dao-view-toggle-wrapper", children: [!showWhitelist &&
                        (!toggleView
                            ? (proposals).map(proposal => _jsx(ProposalRow, { daoOwner: daoOwner, proposal: proposal, proposalId: proposal.id, wrapperClass: "proposal-list-row-wrapper app-main-full-width" }))
                            : _jsx(Row, { justify: "space-between", children: (proposals).map(proposal => _jsx(ProposalCard, { daoOwner: daoOwner, proposal: proposal })) })), showWhitelist &&
                        (!toggleView
                            ? (proposals).map(proposal => {
                                return _jsx(WhitelistProposalRow, { daoOwner: daoOwner, proposal: proposal, proposalId: proposal.id, wrapperClass: "proposal-list-row-wrapper app-main-full-width" });
                            })
                            : _jsx(Row, { justify: "space-between", children: (proposals).map(proposal => _jsx(ProposalCard, { daoOwner: daoOwner, proposal: proposal })) })), shouldLoadMore && _jsx(LoadMore, { loadMore: loadMore })] })] });
};
export default Proposals;
//# sourceMappingURL=ProposalList.js.map