import { useState as m } from "react";
import { notReachable as u } from "../utils/notReachable.js";
import { calculatePairAmount as c } from "../utils/tokens/calculatePairAmount.js";
import { formatHumanToBig as a } from "../utils/formatters/formatHumanToBig.js";
import { b as l } from "../config-D-Ik_SSx.js";
import "ethers";
import "../machine/machine.js";
import { fireEvent as o } from "../machine/events/utils/fireEvent.js";
const b = () => {
  const { ctx: t } = l(), [i, n] = m(void 0);
  return { lastChangedInput: i, onChangeAmount: (r, e) => {
    switch (r) {
      case "source":
        if (!t.sourceToken)
          return;
        o("tokenSetAmount", {
          variant: "source",
          amount: a(e, t.sourceToken.decimals)
        }), o("tokenSetAmount", {
          variant: "target",
          amount: c(e, t.sourceToken, t.targetToken)
        }), n("source");
        break;
      case "target":
        if (!t.targetToken)
          return;
        o("tokenSetAmount", {
          variant: "source",
          amount: c(e, t.targetToken, t.sourceToken)
        }), o("tokenSetAmount", {
          variant: "target",
          amount: a(e, t.targetToken.decimals)
        }), n("target");
        break;
      default:
        u(r, { throwError: !1 });
    }
  }, onChangeToken: (r, e) => {
    switch (n(void 0), r) {
      case "source":
        if (o("tokenSelect", {
          variant: "source",
          token: e
        }), t.isDepositFromExternalWallet)
          o("tokenSetAmount", {
            variant: "source",
            amount: ""
          });
        else if (!e || !t.targetToken)
          o("tokenSetAmount", {
            variant: "source",
            amount: ""
          });
        else {
          const s = parseFloat(t.targetTokenAmount) / 10 ** t.targetToken.decimals * t.targetToken.price / e.price;
          o("tokenSetAmount", {
            variant: "source",
            amount: a(
              s.toFixed(5).toString(),
              e.decimals
            )
          });
        }
        break;
      case "target":
        if (o("tokenSelect", {
          variant: "target",
          token: e
        }), !e || !t.sourceToken)
          o("tokenSetAmount", {
            variant: "target",
            amount: ""
          });
        else {
          const s = parseFloat(t.sourceTokenAmount) / 10 ** t.sourceToken.decimals * t.sourceToken.price / e.price;
          o("tokenSetAmount", {
            variant: "target",
            amount: a(
              s.toFixed(5).toString(),
              e.decimals
            )
          });
        }
        break;
      default:
        u(r, { throwError: !1 });
    }
  } };
};
export {
  b as useTokenInputPair
};
//# sourceMappingURL=useTokenInputPair.js.map
