import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { Avatar, Dropdown, Menu, Space } from "antd-latest";
import { Burger } from "../../../../Components/Icons/Burger";
import { Button, Col, Row } from "antd";
import { ContentImage } from "../../../../Components/Image";
import { Link, useParams } from "react-router-dom";
import { LoadMore } from "../../../../Components/LoadMore";
import { ProposalCard } from "../../Components/Previews/Standard/Card/ProposalCard";
import { ProposalRow } from "../../Components/Previews/Standard/Row/ProposalRow";
import { UserStake } from "../../../../Components/UserWidget";
import { WhitelistProposalRow } from "../../Components/Previews/Whitelist/Row/WhitelistProposalRow";
import { convertDaoProposalsForUI } from "../../Utils/Helpers";
import { useActions, useAppState } from "../../../../overmind";
import { useCachedDaoProposals, useCachedDaoWhitelist, useCachedDaoWhitelistProposals, useCachedPool, useCachedUser, } from "../../../../hooks/useCached";
import { useState } from "react";
import ArrowDown from "../../../../Components/Icons/ArrowDown";
import ListToggle from "../../../../Components/Icons/ListToggle";
import MemberRow from "../../Components/Previews/Member/MemberRow";
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 pool = useCachedPool({ owner: daoOwner });
    const myPools = state.newcoin.pools || {};
    let stakedAmt = myPools[pool.code];
    const daoProposals = useCachedDaoProposals({ daoOwner });
    const daoWhitelistProposals = useCachedDaoWhitelistProposals({ daoOwner });
    const daoMembers = useCachedDaoWhitelist({ daoOwner });
    const [toggleView, setToggleView] = useState(false);
    const Toggle = () => setToggleView(!toggleView);
    const [showProposalType, setShowProposalType] = useState("standard");
    const showWhitelistProposals = () => {
        setShowProposalType("whitelist");
    };
    const showStandardProposals = () => {
        setShowProposalType("standard");
    };
    const showMembersProposals = () => {
        setShowProposalType("members");
    };
    const [sortSelection, setSortSelection] = useState("all");
    let proposals;
    if (sortSelection == "all") {
        showProposalType == "standard" && (proposals = convertDaoProposalsForUI(daoProposals.rows));
        showProposalType == "whitelist" && (proposals = convertDaoProposalsForUI(daoWhitelistProposals.rows));
        showProposalType == "members" && (proposals = daoMembers.rows);
    }
    if (sortSelection == "byMe") {
        !showProposalType && (proposals = convertDaoProposalsForUI(daoProposals.rows)?.filter((row) => row.proposer == currUser));
        showProposalType &&
            (proposals = convertDaoProposalsForUI(daoWhitelistProposals.rows)?.filter((row) => row.proposer == currUser));
    }
    if (sortSelection == "byOthers") {
        !showProposalType && (proposals = convertDaoProposalsForUI(daoProposals.rows)?.filter((row) => row.proposer != currUser));
        showProposalType &&
            (proposals = convertDaoProposalsForUI(daoWhitelistProposals.rows)?.filter((row) => row.proposer != currUser));
    }
    if (sortSelection == "expired") {
        !showProposalType &&
            (proposals = convertDaoProposalsForUI(daoProposals.rows)?.filter((row) => row.time_data.time_left_minutes < 0));
        showProposalType &&
            (proposals = convertDaoProposalsForUI(daoWhitelistProposals.rows)?.filter((row) => row.time_data.time_left_minutes < 0));
    }
    if (sortSelection == "needsApproval") {
        !showProposalType &&
            (proposals = convertDaoProposalsForUI(daoProposals.rows)?.filter((row) => row.status == "created" && row.time_data.time_left_minutes > 0));
        showProposalType &&
            (proposals = convertDaoProposalsForUI(daoWhitelistProposals.rows)?.filter((row) => (row.status = "created" && row.time_data.time_left_minutes > 0)));
    }
    if (sortSelection == "needsExecution") {
        !showProposalType &&
            (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")));
        showProposalType &&
            (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") {
        !showProposalType &&
            (proposals = convertDaoProposalsForUI(daoProposals.rows)?.filter((row) => row.status == "approved" && row.time_data.time_left_minutes > 0));
        showProposalType &&
            (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" }),
            },
        ] }));
    let proposalsState;
    showProposalType == "whitelist" && (proposalsState = (state.newcoin.daos[daoOwner] || {})["whitelistProposals"] || {});
    showProposalType == "standard" && (proposalsState = (state.newcoin.daos[daoOwner] || {})["standardProposals"] || {});
    showProposalType == "members" && (proposalsState = (state.newcoin.daos[daoOwner] || {})["memberProposals"] || {});
    let inProgress;
    showProposalType == "whitelist" && (inProgress = !!state.indicators.specific[`newcoin.daoGetWhitelistProposals`]);
    showProposalType == "standard" && (inProgress = !!state.indicators.specific[`newcoin.daoGetProposals`]);
    showProposalType == "members" && (inProgress = !!state.indicators.specific[`newcoin.daoGetWhitelist`]);
    const shouldLoadMore = !inProgress && (!proposalsState || proposalsState?.more);
    const loadMore = () => {
        showProposalType == "whitelist" && actions.newcoin.daoGetWhitelistProposals({ daoOwner });
        showProposalType == "standard" && actions.newcoin.daoGetProposals({ daoOwner });
        showProposalType == "members" && actions.newcoin.daoGetWhitelist({ daoOwner });
    };
    const daoOwnerObj = useCachedUser({ username: daoOwner });
    if (!inProgress && !daoProposals.dao_id && !ownDao) {
        return window.location.pathname === "/dao/create" ? null : (_jsxs("p", { className: "paragraph-2b", children: ["User ", daoOwner, " had not created their dao yet."] }));
    }
    return (_jsxs(Col, { children: [_jsxs(Row, { align: "middle", className: "dao-details-ctn", children: [_jsxs(Row, { align: "middle", className: "dao-details-left", children: [_jsx(Link, { to: `/user/${daoOwner}`, children: _jsx(Avatar, { src: _jsx(ContentImage, { ...daoOwnerObj }), className: "dao-owner-avi" }) }), _jsxs(Col, { children: [_jsxs("p", { className: "header-1r", children: [daoOwner, " DAO"] }), _jsxs("p", { children: [" ", Math.round(stakedAmt), " $", pool.code, " Staked"] })] }), _jsx("div", { children: _jsx(UserStake, { buttonText: `Get $${pool.code}`, user: { username: daoOwner }, hideButton: false }) })] }), _jsx(Button, { type: "primary", className: "new-proposal-btn", children: _jsx(Link, { to: `/dao/${daoOwner}/new-proposal`, children: "New Proposal" }) })] }), _jsx(Row, { children: _jsxs(Row, { align: "middle", className: "dao-tabs-ctn", children: [_jsxs("h2", { className: `menu-h2-${showProposalType == "standard" ? "active" : "inactive"}`, onClick: showStandardProposals, children: [" ", "Proposal History"] }), _jsxs("h2", { className: `menu-h2-${showProposalType == "whitelist" ? "active" : "inactive"}`, onClick: showWhitelistProposals, children: [" ", "Members Proposals"] }), _jsxs("h2", { className: `menu-h2-${showProposalType == "members" ? "active" : "inactive"}`, onClick: showMembersProposals, children: [" ", "Members"] }), _jsx(Dropdown, { overlay: menu, 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, {}) })] }) }), _jsxs("div", { className: "toggle-wrapper", children: [showProposalType == "standard" &&
                        proposals.reverse().map((proposal) => !toggleView ? (_jsx(ProposalRow, { daoOwner: daoOwner, proposal: proposal, proposalId: proposal.id, wrapperClass: "row-wrapper", buttonType: "view" })) : (
                        // <Row justify={"space-between"}>
                        _jsx(ProposalCard, { daoOwner: daoOwner, proposal: proposal })
                        // </Row>
                        )), showProposalType == "whitelist" &&
                        proposals.reverse().map((proposal) => !toggleView ? (_jsx(WhitelistProposalRow, { daoOwner: daoOwner, proposal: proposal, proposalId: proposal.id, wrapperClass: "row-wrapper", buttonType: "view" })) : (_jsx(Row, { justify: "space-between", children: _jsx(ProposalCard, { daoOwner: daoOwner, proposal: proposal }) }))), showProposalType == "members" &&
                        proposals.map((member) => _jsx(MemberRow, { user: member.user, daoId: daoProposals.dao_id, ticker: pool.code })), shouldLoadMore && _jsx(LoadMore, { loadMore: loadMore })] })] }));
};
export default Proposals;
//# sourceMappingURL=ProposalList.js.map