"use client";
import { jsx, jsxs } from "../../packages/jsx/src/jsx-runtime.js";
import { toGRT, GRTBufferedMaxUint256 } from "@edgeandnode/common";
import { constants } from "ethers";
import { ApprovalFlowMenu } from "./ApprovalFlowMenu.js";
import { MultisigPendingMessage } from "./MultisigPendingMessage.js";
import { FlowStepContainer } from "../../molecules/Flow/FlowStepContainer.js";
import { Button } from "../../molecules/Button/Button.js";
const ApprovalFlowStep = ({ ...props }) => /* @__PURE__ */ jsx(FlowStepContainer, { ...props });
const ApprovalFlowStepAllowance = ({
  stepLabel,
  actionText,
  actionIcon,
  stepTooltip,
  requestedAmount,
  userAllowanceAmount,
  handleAllowAmount,
  active = false,
  disabled = false,
  status,
  isMultisig = false,
  showStepNumber = true
}) => {
  switch (true) {
    /**
     * It was found that the user allowance gets reduced each time a user spends against it.
     * So if a user approves an infinite amount and then performs an action on that,
     * that amount is removed from their allowance. So checking for equality against constants.MaxUint256 fails after a user has spent any
     * amount on the contract.
     * To handle this, an artificial, buffered, amount has been created to check against instead
     */
    case userAllowanceAmount.gte(GRTBufferedMaxUint256): {
      return null;
    }
    case requestedAmount.eq(constants.Zero): {
      return /* @__PURE__ */ jsxs(
        ApprovalFlowStep,
        {
          stepNumber: 1,
          stepLabel,
          active,
          tooltip: stepTooltip,
          showStepNumber,
          children: [
            /* @__PURE__ */ jsxs(Button.Primary, { disabled: true, sx: { flexGrow: 1 }, children: [
              actionText,
              actionIcon
            ] }),
            /* @__PURE__ */ jsx(ApprovalFlowMenu, { amount: requestedAmount, handleAllowAmount, disabled: true })
          ]
        }
      );
    }
    case requestedAmount.lte(userAllowanceAmount): {
      return /* @__PURE__ */ jsx(
        ApprovalFlowStep,
        {
          stepNumber: 1,
          stepLabel,
          active,
          tooltip: stepTooltip,
          showStepNumber,
          children: /* @__PURE__ */ jsx(
            Button.Primary,
            {
              status: "success",
              statusMessage: `Allowed ${toGRT(requestedAmount)} GRT`,
              sx: { flexGrow: 1 }
            }
          )
        }
      );
    }
    case (isMultisig && status === "loading"): {
      return /* @__PURE__ */ jsx(MultisigPendingMessage, {});
    }
    default: {
      return /* @__PURE__ */ jsxs(
        ApprovalFlowStep,
        {
          stepNumber: 1,
          stepLabel,
          active,
          tooltip: stepTooltip,
          showStepNumber,
          children: [
            /* @__PURE__ */ jsxs(
              Button.Primary,
              {
                onClick: () => {
                  if (!active || disabled) {
                    return;
                  }
                  void handleAllowAmount(constants.MaxUint256);
                },
                status,
                statusMessage: status != null && status !== "loading" ? actionText : void 0,
                disabled: !active || disabled,
                sx: { flexGrow: 1 },
                children: [
                  actionText,
                  actionIcon
                ]
              }
            ),
            /* @__PURE__ */ jsx(
              ApprovalFlowMenu,
              {
                amount: requestedAmount,
                handleAllowAmount,
                disabled: !active || disabled
              }
            )
          ]
        }
      );
    }
  }
};
ApprovalFlowStepAllowance.displayName = "ApprovalFlowStep.Allowance";
ApprovalFlowStep.Allowance = ApprovalFlowStepAllowance;
const ApprovalFlowStepAction = ({
  stepLabel,
  actionText,
  actionIcon,
  handleAction,
  active = false,
  disabled = false,
  status,
  actionAmount,
  userAllowanceAmount = constants.Zero,
  approvalPermit,
  loadingProgress,
  isMultisig = false,
  showStepNumber = true
}) => {
  if (isMultisig && status === "loading") {
    return /* @__PURE__ */ jsx(MultisigPendingMessage, {});
  }
  if (userAllowanceAmount.gte(GRTBufferedMaxUint256)) {
    return /* @__PURE__ */ jsxs(
      Button.Primary,
      {
        onClick: () => void handleAction(actionAmount, approvalPermit),
        status,
        statusMessage: status != null && status !== "loading" ? actionText : void 0,
        disabled: !active || disabled || status != null,
        loadingProgress,
        sx: { flexGrow: 1 },
        children: [
          actionText,
          actionIcon
        ]
      }
    );
  }
  return /* @__PURE__ */ jsx(ApprovalFlowStep, { stepNumber: 2, stepLabel, active, showStepNumber, children: /* @__PURE__ */ jsxs(
    Button.Primary,
    {
      onClick: () => void handleAction(actionAmount, approvalPermit),
      status,
      statusMessage: status != null && status !== "loading" ? actionText : void 0,
      disabled: !active || disabled || status != null,
      loadingProgress,
      sx: { flexGrow: 1 },
      children: [
        actionText,
        actionIcon
      ]
    }
  ) });
};
ApprovalFlowStepAction.displayName = "ApprovalFlowStep.Action";
ApprovalFlowStep.Action = ApprovalFlowStepAction;
const ApprovalFlowStepPermit = ({
  stepLabel,
  actionText,
  actionIcon,
  handleAllowAmount,
  active = false,
  disabled = false,
  status,
  requestedAmount,
  userAllowanceAmount,
  stepTooltip,
  showStepNumber = true
}) => {
  if (requestedAmount.gt(0) && requestedAmount.lte(userAllowanceAmount)) {
    return /* @__PURE__ */ jsx(
      ApprovalFlowStep,
      {
        stepNumber: 1,
        stepLabel,
        active,
        tooltip: stepTooltip,
        showStepNumber,
        children: /* @__PURE__ */ jsx(Button.Primary, { status: "success", statusMessage: `Allowed ${toGRT(requestedAmount)} GRT`, sx: { flexGrow: 1 } })
      }
    );
  }
  return /* @__PURE__ */ jsx(
    ApprovalFlowStep,
    {
      stepNumber: 1,
      stepLabel,
      active,
      tooltip: stepTooltip,
      showStepNumber,
      children: /* @__PURE__ */ jsxs(
        Button.Primary,
        {
          onClick: () => void handleAllowAmount(requestedAmount),
          status,
          statusMessage: status != null && status !== "loading" ? actionText : void 0,
          disabled: !active || disabled || status != null,
          sx: { flexGrow: 1 },
          children: [
            actionText,
            actionIcon
          ]
        }
      )
    }
  );
};
ApprovalFlowStepPermit.displayName = "ApprovalFlowStep.Permit";
ApprovalFlowStep.Permit = ApprovalFlowStepPermit;
export {
  ApprovalFlowStep
};
