import { jsxs as m, jsx as p } from "react/jsx-runtime";
import T from "lodash/pick";
import { Confirm as k } from "@arcblock/ux/lib/Dialog";
import { Box as f } from "@mui/material";
import h from "@arcblock/ux/lib/Toast";
import F from "@arcblock/ux/lib/CardSelector";
import { translate as C } from "@arcblock/ux/lib/Locale/util";
import { useMemoizedFn as R, useCreation as I } from "ahooks";
import { createPassportSvg as x } from "@arcblock/ux/lib/Util/passport";
import { ReactGA as P } from "@arcblock/ux/lib/withTracker";
import B from "../OAuth/guest.svg.js";
const G = (e) => e ? typeof e == "string" ? { sessionToken: e } : T(e, ["sessionToken", "refreshToken", "visitorId", "destroySessionId", "csrfToken"]) : {}, M = {
  zh: {
    switchPassport: "切换通行证",
    switchPassportFailed: "切换通行证失败",
    switchPassportSucceed: "切换通行证成功",
    selectedPassport: "请选择一个通行证",
    switch: "切换",
    cancel: "取消",
    currentRole: "当前角色：",
    passport: "通行证",
    getPassportFailed: "获取通行证失败"
  },
  en: {
    switchPassport: "Switch passport",
    switchPassportFailed: "Switch passport failed",
    switchPassportSucceed: "Switch passport succeed",
    selectedPassport: "Select a passport to switch",
    switch: "Switch",
    cancel: "Cancel",
    currentRole: "Current role: ",
    passport: "Passport",
    getPassportFailed: "Get passports failed"
  }
};
function D({ api: e, locale: w, switchState: r, onSwitchPassport: g, session: a }) {
  const t = R((s, i = {}) => C(M, s, w, "en", i)), l = I(() => r.passports.filter((s) => s.role !== r.currentUser?.role), [r.passports, r.currentUser?.role]), S = async () => {
    const s = r.selectedPassport, i = a?.user?.role, v = a?.user?.passports?.find((o) => o.id === s)?.role ?? "guest", d = `${i} -> ${v}`;
    try {
      const { data: o } = await e.post("/switch", { passportId: s }), { sessionToken: c, refreshToken: n, csrfToken: y } = G(o);
      g({ sessionToken: c, refreshToken: n, csrfToken: y }), h.success(t("switchPassportSucceed"));
      const u = {
        action: "switchPassportSuccess",
        change: d,
        success: !0
      };
      P.event(u.action, u);
    } catch (o) {
      const c = o.message || t("switchPassportFailed"), n = {
        action: "switchPassportFailed",
        success: !1,
        change: d,
        errorMessage: c
      };
      P.event(n.action, n), h.error(c);
    }
  };
  return /* @__PURE__ */ m(
    k,
    {
      open: r.open,
      title: t("switchPassport"),
      onConfirm: S,
      onCancel: r.reset,
      confirmButton: {
        text: t("switch"),
        props: {
          variant: "contained",
          color: "primary"
        }
      },
      cancelButton: {
        text: t("cancel"),
        props: {
          color: "inherit"
        }
      },
      PaperProps: { style: { width: 600 } },
      children: [
        /* @__PURE__ */ m(
          f,
          {
            sx: {
              mb: 2
            },
            children: [
              t("currentRole"),
              r.currentUser?.role
            ]
          }
        ),
        /* @__PURE__ */ p(
          F,
          {
            width: 160,
            height: 240,
            cardSpace: 24,
            list: [
              // eslint-disable-next-line react/no-unstable-nested-components
              () => /* @__PURE__ */ p(B, {}),
              ...l.map((s) => s.display ? {
                src: s.display,
                name: s.title
              } : () => /* @__PURE__ */ p(
                f,
                {
                  sx: { width: "100%" },
                  dangerouslySetInnerHTML: {
                    __html: x(s)
                  }
                },
                s.id
              ))
            ],
            onSelect: (s) => {
              r.selectedPassport = s > 0 ? l[s - 1].id : void 0;
            },
            defaultIndex: 0
          }
        )
      ]
    }
  );
}
export {
  D as PassportSwitcher,
  G as parseResponse
};
