import { useCurve, useMint, useSolanaUnixTime, useTokenBonding, useTokenSwapFromId, } from "@strata-foundation/react";
import { useVariables } from "../../theme/Root/variables";
import React, { useState } from "react";
import { EstimatedSalesVsTime, PriceVsSupplyDisplay, PriceVsTimeDisplay, RateVsTimeDisplay, } from "../CurveConfigurator";
import { toNumber } from "@strata-foundation/spl-utils";
import { useMemo } from "react";
import { Box, SimpleGrid, VStack, Input } from "@chakra-ui/react";
export function CurveConfiguratorFromBonding({ priceVsSupply = true, priceVsTime = true, rateVsTime = false, salesVsTime = false, }) {
    const { tokenBondingKey: inputTokenBondingKey, id } = useVariables();
    const { tokenBonding } = useTokenSwapFromId(id);
    const tokenBondingKey = useMemo(() => inputTokenBondingKey || tokenBonding?.publicKey, [tokenBonding, inputTokenBondingKey]);
    const { info: tokenBondingAcct } = useTokenBonding(tokenBondingKey);
    const { info: curve } = useCurve(tokenBondingAcct?.curve);
    const baseMint = useMint(tokenBondingAcct?.baseMint);
    const targetMint = useMint(tokenBondingAcct?.targetMint);
    const [timeOffset, setTimeOffset] = useState();
    const [reserves, setReserves] = useState();
    const [supply, setSupply] = useState();
    const [startSupply, setStartSupply] = useState();
    const [endSupply, setEndSupply] = useState();
    const [maxTime, setMaxTime] = useState();
    const unixTime = useSolanaUnixTime();
    const bondTimeOffset = useMemo(() => {
        return (tokenBondingAcct &&
            unixTime &&
            Math.max(unixTime - tokenBondingAcct.goLiveUnixTime.toNumber(), 0));
    }, [unixTime, tokenBondingAcct]);
    const bondReserves = useMemo(() => {
        return (tokenBondingAcct &&
            baseMint &&
            toNumber(tokenBondingAcct.reserveBalanceFromBonding, baseMint).toString());
    }, [tokenBondingAcct, baseMint]);
    const bondSupply = useMemo(() => {
        return (tokenBondingAcct &&
            targetMint &&
            toNumber(tokenBondingAcct.supplyFromBonding, targetMint).toString());
    }, [tokenBondingAcct, targetMint]);
    const bondStartSupply = 0;
    const bondEndSupply = tokenBondingAcct?.mintCap;
    const bondMaxTime = useMemo(() => {
        // @ts-ignore
        return curve?.definition.timeV0.curves[0].curve.timeDecayExponentialCurveV0
            .interval;
    }, [curve]);
    const numCharts = +priceVsSupply + +priceVsTime + +rateVsTime;
    const supplyOffset = useMemo(() => {
        if (tokenBondingAcct && targetMint) {
            return toNumber(tokenBondingAcct.supplyFromBonding.sub(targetMint.supply), targetMint);
        }
    }, [tokenBondingAcct, targetMint]);
    if (!bondTimeOffset ||
        !bondReserves ||
        !bondSupply ||
        !bondEndSupply ||
        !bondMaxTime ||
        !curve) {
        return React.createElement(Box, null, "Loading...");
    }
    const args = {
        timeOffset: timeOffset || bondTimeOffset?.toString(),
        setTimeOffset,
        reserves: reserves || bondReserves,
        setReserves,
        supply: supply || bondSupply,
        setSupply,
        startSupply: startSupply || bondStartSupply.toString(),
        setStartSupply,
        endSupply: endSupply || bondEndSupply?.toString(),
        setEndSupply,
        maxTime: maxTime || bondMaxTime.toString(),
        setMaxTime,
    };
    return (React.createElement(VStack, { w: "full" },
        React.createElement(SimpleGrid, { columns: 3, w: "full" },
            React.createElement(VStack, null,
                React.createElement("label", null, "Reserves"),
                React.createElement(Input, { type: "number", step: 0.001, onChange: (e) => setReserves(e.target.value), value: args.reserves })),
            React.createElement(VStack, null,
                React.createElement("label", null, "Supply"),
                React.createElement(Input, { type: "number", step: 0.001, onChange: (e) => setSupply(e.target.value), value: args.supply })),
            React.createElement(VStack, null,
                React.createElement("label", null, "Seconds since Launch"),
                React.createElement(Input, { type: "number", step: 1, onChange: (e) => setTimeOffset(e.target.value), value: args.timeOffset })),
            React.createElement(VStack, null,
                React.createElement("label", null, "Start Supply"),
                React.createElement(Input, { type: "number", step: 0.001, onChange: (e) => setStartSupply(e.target.value), value: args.startSupply })),
            React.createElement(VStack, null,
                React.createElement("label", null, "End Supply"),
                React.createElement(Input, { type: "number", step: 0.001, onChange: (e) => setEndSupply(e.target.value), value: args.endSupply })),
            React.createElement(VStack, null,
                React.createElement("label", null, "Max Time"),
                React.createElement(Input, { type: "number", step: 1, onChange: (e) => setMaxTime(e.target.value), value: args.maxTime }))),
        React.createElement(SimpleGrid, { columns: [1, 1, 1, 1, Math.min(numCharts, 2)], w: "full" },
            priceVsSupply && (React.createElement(PriceVsSupplyDisplay, { supplyOffset: supplyOffset || 0, curveConfig: curve, ...args })),
            priceVsTime && (React.createElement(PriceVsTimeDisplay, { curveConfig: curve, ...args })),
            rateVsTime && (React.createElement(RateVsTimeDisplay, { curveConfig: curve, ...args })),
            salesVsTime && (React.createElement(EstimatedSalesVsTime, { curveConfig: curve, ...args })))));
}
//# sourceMappingURL=index.js.map