// @ts-nocheck
import React, { AnchorHTMLAttributes } from 'react';
import { loginService } from '@/service';

interface ToLogoutProps extends AnchorHTMLAttributes<HTMLAnchorElement> {
  token: string;
  onSuccess?: () => {};
  onFailed?: () => {};
}

const ToLogout: React.FC<ToLogoutProps> = (props: ToLogoutProps) => {
  const { children, token, onSuccess, onFailed, ...restProps } = props;
  return (
    <a
      {...restProps}
      onClick={async () => {
        try {
          let data = await loginService.sendLogout({
            token,
          });
          if (data?.error) {
            throw new Error(data?.msg);
          }
          if (onSuccess) {
            onSuccess();
          }
        } catch (e) {
          if (onFailed) {
            onFailed();
          }
          throw e;
        }
      }}
    >
      {children}
    </a>
  );
};

export default ToLogout;
