import { jsx as t, jsxs as m, Fragment as F } from "react/jsx-runtime";
import { useRef as k, useState as d, useCallback as A, useEffect as B } from "react";
import { SearchW700 as D } from "@material-symbols-svg/react-rounded/icons/search";
import { Hr as H } from "../components/Hr.js";
import { Card as O } from "../components/Card.js";
import { Input as J } from "../components/Input.js";
import { Banner as K } from "../components/Banner.js";
import { CloseButton as U } from "../components/CloseButton.js";
import { cn as V } from "../utils/cn.js";
import { b as W, J as q, d as z, w as G } from "../config-BDnHptBC.js";
import "@tanstack/react-query";
import "@defuse-protocol/one-click-sdk-typescript";
import "../constants/aurora-assets.js";
import "../constants/chains.js";
import "../utils/feeServiceTokens.js";
import "../logger.js";
import "@scure/base";
import { notReachable as S } from "../utils/notReachable.js";
import "zod";
import "../utils/near/rpc.js";
import "ethers";
import "../machine/machine.js";
import "../index-DEq3oXQF.js";
import "@defuse-protocol/intents-sdk";
import "change-case";
import "../localstorage-DVS0q1yC.js";
import "@defuse-protocol/internal-utils";
import "bs58";
import "axios";
import "../network.js";
import "../hooks/useMakeNearTransfer/nearClient.js";
import "../hooks/useOneClickExecutionStatus.js";
import { useHandleKeyDown as g } from "../hooks/useHandleKeyDown.js";
import { useTypedTranslation as P } from "../localisation.js";
import { ChainsSelector as Q } from "./ChainsSelector/index.js";
const Le = ({
  variant: a,
  showBalances: y,
  showChainsSelector: v,
  chainsFilter: l,
  groupTokens: E,
  className: T,
  onMsg: f
}) => {
  const { t: _ } = P(), { ctx: N } = W(), { supportedChains: I } = q(), i = k(null), h = k(null), [n, s] = d(""), [c, u] = d(!1), p = z(a), M = A((e) => {
    if (!e)
      return;
    e.closest(
      'input, textarea, select, button, a[href], [tabindex]:not([tabindex="-1"]), [contenteditable="true"]'
    ) || i.current?.focus({ preventScroll: !0 });
  }, []), b = () => f({ type: "on_dismiss_tokens_modal" });
  B(() => {
    const e = i.current;
    if (!e)
      return;
    u(e.contains(document.activeElement));
    const r = () => u(!0), x = (j) => {
      const C = j.relatedTarget;
      C && e.contains(C) || u(!1);
    };
    return e.addEventListener("focusin", r), e.addEventListener("focusout", x), () => {
      e.removeEventListener("focusin", r), e.removeEventListener("focusout", x);
    };
  }, []), g(
    "Escape",
    () => {
      n ? s("") : b();
    },
    [n],
    { enabled: c }
  ), g(
    "Alphanumeric",
    (e) => {
      s((r) => r + e), h.current?.focus();
    },
    [],
    { enabled: c }
  );
  const L = p.length === 1 && p[0]?.id ? p[0]?.id : "all", [o, R] = d(L), w = o !== "all" && o !== "intents" && l.external !== "all" && !I.includes(o);
  return /* @__PURE__ */ t(
    "div",
    {
      tabIndex: -1,
      ref: i,
      onMouseDownCapture: (e) => {
        M(e.target);
      },
      className: "w-full outline-none",
      children: /* @__PURE__ */ m(
        O,
        {
          padding: "none",
          className: V(
            "w-full gap-sw-xl flex flex-col px-sw-2xl pt-sw-2xl",
            T
          ),
          children: [
            /* @__PURE__ */ m("header", { className: "flex items-center justify-between", children: [
              /* @__PURE__ */ t("h2", { className: "text-sw-label-lg text-sw-gray-50", children: "Select token" }),
              /* @__PURE__ */ t(U, { onClick: b })
            ] }),
            /* @__PURE__ */ t(
              J,
              {
                focusOnMount: !0,
                icon: D,
                ref: h,
                defaultValue: n,
                className: "w-full",
                placeholder: "Search or paste address",
                onChange: (e) => s(e.target.value.trim())
              }
            ),
            v && /* @__PURE__ */ t(
              Q,
              {
                variant: a,
                chainsFilter: l,
                selectedChain: o,
                onMsg: (e) => {
                  switch (e.type) {
                    case "on_select_chain":
                      R(e.chain);
                      break;
                    default:
                      S(e.type);
                  }
                }
              }
            ),
            w && !!N.walletAddress && /* @__PURE__ */ m(F, { children: [
              /* @__PURE__ */ t(
                K,
                {
                  variant: "error",
                  message: _(
                    "wallet.connected.error.notSupportedChain",
                    "This network isn’t supported by your wallet."
                  )
                }
              ),
              /* @__PURE__ */ t(H, {})
            ] }),
            /* @__PURE__ */ t(
              G,
              {
                variant: a,
                search: n,
                groupTokens: E,
                showBalances: y,
                keyboardNavigationEnabled: c,
                chainsFilter: l,
                selectedChain: o,
                chainIsNotSupported: w,
                onMsg: (e) => {
                  switch (e.type) {
                    case "on_reset_search":
                      s("");
                      break;
                    case "on_select_token":
                      f(e);
                      break;
                    default:
                      S(e, { throwError: !1 });
                  }
                }
              }
            )
          ]
        }
      )
    }
  );
};
export {
  Le as TokensModal
};
//# sourceMappingURL=TokensModal.js.map
