import { useState as y, useMemo as u, useEffect as F } from "react";
import { useMemoizedFn as f } from "ahooks";
import U from "lodash/isNil";
import { BlockletSDK as D } from "@blocklet/js-sdk";
import a from "../Toast/index.js";
import { formatAxiosError as d } from "../Util/index.js";
function K({
  user: e,
  t: w,
  isMySelf: l,
  visible: m
}) {
  const [p, c] = y(!1), t = u(() => e?.did, [e]), n = u(() => Object.prototype.hasOwnProperty.call(e, "isFollowing") && !U(e.isFollowing), [e]), r = u(() => {
    let o = null;
    try {
      o = new D();
    } catch (s) {
      console.error("Failed to initialize BlockletSDK:", s), o = null;
    }
    return o;
  }, []), i = f(async () => {
    if (!r) {
      c(!1);
      return;
    }
    try {
      if (l) {
        c(!0);
        return;
      }
      const o = await r.user.isFollowingUser({ userDid: t });
      c(o);
    } catch (o) {
      console.error(o);
    }
  }), h = f(async (o = t) => {
    if (!(!r || l && o === t))
      try {
        await r.user.followUser({ userDid: o }), a.success(w("follow_success")), i();
      } catch (s) {
        console.error(s), a.error(d(s));
      }
  }), g = f(async (o = t) => {
    if (!(!r || l && o === t))
      try {
        await r.user.unfollowUser({ userDid: o }), a.success(w("unfollow_success")), i();
      } catch (s) {
        console.error(s), a.error(d(s));
      }
  });
  return F(() => {
    m && t && !l && r && !n && i();
  }, [i, t, l, r, n, m]), F(() => {
    n && c(e?.isFollowing || !1);
  }, [n, e]), {
    followed: p,
    followUser: h,
    unfollowUser: g
  };
}
export {
  K as default
};
