import axios from "axios"


axios.interceptors.request.use(
    config => {
        config.headers = {
            Accept: "application/json",
            "Access-Control-Allow-Origin": "*",
            // "Access-Control-Expose-Headers": "Access-Control-Allow-Origin",
            // "Access-Control-Allow-Headers": "Origin, X-API-KEY, X-Requested-With, Content-Type, Accept, Access-Control-Request-Method, Access-Control-Allow-Headers, Authorization, observe, enctype, Content-Length, X-Csrf-Token",
            // "Content-Type": "application/json;charset=UTF-8",
            'Authorization': (localStorage && localStorage.getItem('access_token')) ? `Bearer ${localStorage.getItem('access_token')}` : '',
            // 'uuid': 'dummy'
          };
    // config.headers['Authorization'] = `Bearer ${localStorage.getItem('token')}`;
    // config.baseURL = process.env.REACT_APP_MAIN_API
        return config;
    },
    error => {
        return Promise.reject(error);
    }
);

const postAuxEnd = (path, postData, media=false) => {
    const basePath = media ? process.env.REACT_APP_MAIN_API : process.env.REACT_APP_MAIN_API
    return axios.post(`${basePath}${path}`, postData).then(res => {
        return res
    }).catch(err => {
        throw new Error(err.response.data.msg);
    })
}

const getAuxEnd = (path, reqData= null) => {
    const basePath = process.env.REACT_APP_MAIN_API
    return axios.get(`${basePath}${path}`,{ params: reqData }).then(res => {
        return res
        // localStorage.clear();
        // window.location.href = `/login?sessionExpired=true`;
    }).catch(err => {
        throw new Error(err);
        // throw new Error(err.response.data.message);
        // return err
    })
}

// FUNCTION TO AUTHORIZE USER IN
export const userToken = (reqData) => {
    let postData = {
        ...reqData
    }
    return postAuxEnd('/Authorize', postData, false)
}

// FUNCTION TO LOGIN USER IN
export const loginUser = (reqData) => {
    let postData = {
        ...reqData
    }
    return postAuxEnd('/panel/Login', postData, false)
}

// FUNCTION TO GET USER INFO DATA
export const userInfo = (reqData) => {
    let postData = {
        ...reqData
    }
    return postAuxEnd('/panel/account', postData, false)
}

// FUNCTION TO GET DASHBOARD TOP BAR SECTION
export const topBar = (reqData) => {
    let postData = {
        ...reqData,
    }
    return postAuxEnd(`/panel/account/bar`, postData, false)
}

// FUNCTION TO GET PRODUCT BY ID
export const MyProductData = (reqData) => {
    let postData = {
        ...reqData,
    }
    return postAuxEnd(`/panel/myproduct/dash`, postData, false)
}

export const StripeSubscriptionCreate = (reqData) => {
    let postData = {
        ...reqData,
    }
    return postAuxEnd(`/panel/subscription/start`, postData, false)
}

// FUNCTION TO GET CALENDAR EVENTS
export const getCalendarEvents = (reqData) => {
    let postData = {
        ...reqData,
    }
    return postAuxEnd(`/panel/account/calendar`, postData, false)
}

// FUNCTION TO GET DASHBOARD PRODUCT DATA SECTION
export const contactData = (reqData) => {
    let postData = {
        ...reqData,
    }
    return postAuxEnd(`/panel/contacts`, postData, false)
}

// FUNCTION TO GET DASHBOARD PRODUCT URL DATA SECTION
export const productsURL = (reqData) => {
    let postData = {
        ...reqData,
    }
    return postAuxEnd(`/panel/account/products/url`, postData, false)
}

// FUNCTION TO GET DASHBOARD PRODUCT DATA SECTION
export const productsData = (reqData) => {
    let postData = {
        ...reqData,
    }
    return postAuxEnd(`/panel/account/products`, postData, false)
}

// FUNCTION TO REFRESH SITE
export const productRefreshSite = (reqData) => {
    let postData = {
        ...reqData,
    }
    return postAuxEnd(`/panel/account/products/refresh`, postData, false)
}

// FUNCTION TO GET DASHBOARD RECENT ACTIONS SECTION
export const recentActions = (reqData) => {
    let postData = {
        ...reqData,
        token: localStorage.getItem('token'), // USER TOKEN
        uid: localStorage.getItem('uid') // USER UID
    }
    //return getAuxEnd(`/panel/account/actions`)
    return postAuxEnd(`/panel/account/actions`, postData, false)
}

// FUNCTION TO VERIFY RESET TOKEN
export const verifyResetToken = (reqData) => {
    let postData = {
        ...reqData
    }
    return postAuxEnd('/panel/auth/resetverify', postData, false)
}

// FUNCTION TO COMPLETE PASSWORD RESET
export const completePWDReset = (reqData) => {
    let postData = {
        ...reqData
    }
    return postAuxEnd('/panel/auth/resetcomplete', postData, false)
}

// FUNCTION TO SUBMIT PAGE TAB SETTINGS
export const pageSettings = (reqData) => {
    let postData = {
        ...reqData,
    }
    return postAuxEnd(`/panel/myproduct/settings`, postData, false)
}

// FUNCTION TO GET SETTINGS DATA
export const getSettingsData = (reqData) => {
    let postData = {
        ...reqData,
    }
    return postAuxEnd(`/panel/myproduct/settings/values`, postData, false)
}

// FUNCTION TO GET SETTINGS DATA
export const getProductTemplateData = (reqData) => {
    let postData = {
        ...reqData,
    }
    return postAuxEnd(`/panel/account/products/templates`, postData, false)
}

// FUNCTION TO GET PRODUCT SUBSCRIPTIONS
export const completeProfile = (reqData) => {
    let postData = {
        ...reqData,
    }
    return postAuxEnd(`/panel/account/startprofile`, postData, false)
}

// FUNCTION TO COMPLETE PROFILE
export const getSubscriptions = (reqData) => {
    let postData = {
        ...reqData,
    }
    return postAuxEnd(`/panel/subscription/products`, postData, false)
}

// FUNCTION TO GET COMMON PRACTICE
export const getCommonPractice = (reqData) => {
    let postData = {
        ...reqData,
    }
    return postAuxEnd(`/panel/common/practice`, postData, false)
}






// FUNCTION TO REGISTER USER
export const signUpUser = (reqData) => {
    let postData = {
        ...reqData
    }
    return postAuxEnd('/panel/Register', postData, false)
}

// FUNCTION TO VERIFY EMAIL
export const verifyEmail = (reqData) => {
    let postData = {
        ...reqData
    }
    return postAuxEnd('/panel/Register/verify', postData, false)
}

// FUNCTION TO COMPLETE REGISTRATION
export const completeRegistration = (reqData) => {
    let postData = {
        ...reqData
    }
    return postAuxEnd('/panel/Register/complete', postData, false)
}

// FUNCTION TO SUBSCRIBE
export const subscribe = (reqData) => {
    let postData = {
        ...reqData,
        token: localStorage.getItem('token'), // USER TOKEN
        uid: localStorage.getItem('uid') // USER UID
    }
    return postAuxEnd(`/panel/myproduct/subscription`, postData, false)
}


// FUNCTION TO RESET USER PASSWORD
export const recoverPWD = (reqData) => {
    let postData = {
        ...reqData
    }
    return postAuxEnd('/panel/auth/reset', postData, false)
}

// FUNCTION TO GET MY PRODUCT PROVISION DATA
export const productProvision = (reqData) => {
    let postData = {
        ...reqData,
        token: localStorage.getItem('token'), // USER TOKEN
        uid: localStorage.getItem('uid') // USER UID
    }
    return postAuxEnd(`/panel/myproduct/provision`, postData, false)
    // return getAuxEnd(`/panel/myproduct/provision`, postData)
}

