import { ElMessage } from "element-plus";
import { useNav } from "/@/layout/hooks/nav";

type Params = {
  code: number;
  message: string;
  handler: () => void;
};

export function useResponseHandle() {
  const { logout } = useNav();

  function responseHandle({ code, message, handler }: Params) {
    const c = Number(code);
    if (c === 0) {
      handler();
    } else if (c >= 100 && c <= 140) {
      logout();
    } else {
      ElMessage.error(message);
    }
  }

  return responseHandle;
}
