import { AxiosRequestConfig, AxiosResponse } from 'axios' import { CONFIGS } from '../utils' import { publicRequest } from './publicRequest' export const authorize = async ( data: IAuthorizeRequestData ): Promise => { const response: AxiosResponse = await publicRequest.post( `/auth?clientId=${CONFIGS.CLIENT_ID || 'e9d8f8922797b4621e562255afe90dbf'}`, data ) return response.data } export const signUp = async (data: ISignupRequestData): Promise => { const response: AxiosResponse = await publicRequest.post('auth/signup', { ...data }) return response.data } export const register = async (data: FormData): Promise => { const response: AxiosResponse = await publicRequest.post('v1/oauth/register-rn', data) return response.data } export const getProfileData = async (): Promise => { const response: AxiosResponse = await publicRequest.get('/customer/profile/') return response.data } export const logout = async (): Promise => { const response: AxiosResponse = await publicRequest.delete('/auth') return response.data } /** * Checks whether a given email address already exists via the `/ajax/contact-email` endpoint. * * The underlying API is expected to return a JSON object containing: * - `exists`: `1` if the email exists, `0` otherwise * - `error`: `1` if an error occurred, `0` otherwise * - `message`: an optional error message when `error === 1` * * This function normalizes that response to an object with: * - `exists`: a boolean indicating whether the email exists * - `error`: an optional string containing an error message, if any * * On network or unexpected errors, it returns `{ exists: false, error: 'Failed to check email' }`. * * @param {string} email - The email address to check for existence. * @returns {Promise<{ exists: boolean; error?: string }>} A promise that resolves to the normalized * result of the email existence check. */ export const checkEmailExists = async ( email: string ): Promise<{ exists: boolean, error?: string }> => { try { const formData = new FormData() formData.append('email', email) formData.append('is_checkout_flow', 'true') const baseUrl = publicRequest.defaults.baseURL?.replace('/api', '') || '' const url = `${baseUrl}/ajax/contact-email` const response: AxiosResponse = await publicRequest.post(url, formData, { headers: { 'Content-Type': 'multipart/form-data', }, }) return { exists: response.data.exists === 1, error: response.data.error === 1 ? response.data.message : undefined, } } catch (error) { console.error('Error checking email:', error) return { exists: false, error: 'Failed to check email', } } }