import { useBaseConnector } from '@/UI-kit/composables';
import type {
    IAuthSendEmailResponse,
    IAuthVerifyCodeResponse,
    IAuthLogoutResponse,
} from '@/modules/auth/type/auth.type';
import { useUiKitConfigStore } from '@/configs/store';
import { storeToRefs } from 'pinia';

export default function (authToken = '') {
    const configStore = useUiKitConfigStore();
    const { userAuthToken } = storeToRefs(configStore);

    const baseConnector = useBaseConnector(userAuthToken.value || authToken);

    const authEmailCodeUser = async (email: string): Promise<IAuthSendEmailResponse> => {
        return baseConnector.post<IAuthSendEmailResponse>('/api/rosmigrant/auth/email', { email });
    };

    const authEmailCodeVerificationUser = async (formData: {
        code: string;
        email: string;
    }): Promise<IAuthVerifyCodeResponse> => {
        return baseConnector.post<IAuthVerifyCodeResponse>(
            '/api/rosmigrant/auth/email/verification',
            formData,
        );
    };

    const logoutUser = async (): Promise<IAuthLogoutResponse> => {
        return baseConnector.remove<IAuthLogoutResponse>('/api/rosmigrant/auth/logout');
    };

    return {
        authEmailCodeUser,
        authEmailCodeVerificationUser,
        logoutUser,
    };
}
