const vueAppMagenotApiUrl = process.env.VUE_APP_MAGENTO_API_URL;
import { authHeader } from './auth';
import { meWrapRequest } from './nav';
export const registrationService = {
    doResetPassword,
    doConfirmEmail,
    doSignUp,
    doForgotPassword
};
//function handleResponse(response, retryFunction){
function handleResponse(response) {
    return response.text().then(text => {
        const data = text && JSON.parse(text);
        if (!response.ok) {
            const error = (data && data.message) || response.statusText;
            return Promise.reject(error);
        } else {
            return data;
        }
    });
}

function doResetPassword(resetOne) { //userId, newPassword, token
    const requestOptions = {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ "email": "", "resetToken": resetOne.token, "newPassword": resetOne.newPassword })
    };
    return fetch(`${vueAppMagenotApiUrl}/rest/crn_retailer/V1/customers/resetPassword`, requestOptions).then(handleResponse);
}

function doForgotPassword(forgot) {
    const requestOptions = {
        method: 'PUT',
        headers: {
            'Content-type': 'application/json',
            'Access-Control-Allow-Origin': '*'
        },
        body: JSON.stringify({
            "email": forgot.email,
            "template": "email_reset",
            "websiteId": 2
        })
    };

    return fetch(`${vueAppMagenotApiUrl}/rest/crn_retailer/V1/customers/password`, requestOptions).then(handleResponse);
}

/*function doconfirmEmail(token) {
    const requestOptions = {
        method: 'PUT',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify(
            {
                token: token
            }
        )
    };
    return fetch(`${process.env.VUE_APP_MAGENTO_API_URL}/rpc/confirm_user_email`, requestOptions).then(handleResponse);
}*/

//async function doconfirmEmail(token) {
async function doConfirmEmail() {
    let details = {
        "massages": 'Your Email id confirm Please login In',
        "response": "400"
    }
    return details;
}

function doSignUp(registration) {
    const requestOptions = {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
            'Access-Control-Allow-Origin': '*'
        },
        body: JSON.stringify({
            "customer": {
                "email": registration.email,
                "firstname": registration.firstname,
                "lastname": registration.lastname
            },
            "password": registration.password
        })

    };

    // console.log(registration);

    return fetch(`${vueAppMagenotApiUrl}/rest/crn_retailer/V1/customers`, requestOptions).then(handleResponse);
}
/* eslint-disable no-alert, no-console ,  no-debugger */
export const doUpdateSubscription = meWrapRequest((one) => {
    const requestOptions = {
        method: 'PUT',
        headers: {
            'Content-type': 'application/json',
            ...authHeader()
        },
        body: JSON.stringify({
            "customer": {
                "firstname": one.firstname,
                "lastname": one.lastname,
                "email": one.email,
                "website_id": one.website_id,
                "extension_attributes": {
                    "is_subscribed": true
                }
            }
        })
    };
    return fetch(`${vueAppMagenotApiUrl}/rest/crn_retailer/V1/customers/me`, requestOptions)
});
////////////////// dummy api //////////////////
/*async function doSignUp(firstName, lastName, email, password) {
    let details = {
        "massages": 'ragistration successfully',
        "response": "ok"
    }


return details;
}*/