import axios from "axios";
import { prodEndpointCreation } from "../../../../../globalUtils/createUrl.js";

export const postOrders = async (data: any, user: any) => {
    let endpoint = await prodEndpointCreation(
        "/public/v1.0/DemandManagement/ORDERS"
    );

    try {
        const results = await axios({
            url: endpoint,
            method: "POST",
            headers: {
                Authorization: `Basic ${user.prod}`,
            },
            // if a body is sent over it would be like below
            data: data,
        });

        //console.log(results.status);
        if (results.data.errors) {
            return {
                success: true,
                message: "Error processing orders",
                data: results.data.errors[0].message,
            };
        }

        if (results.status === 200) {
            return {
                success: true,
                message: "Success on posting orders",
                data: data,
            };
        }
    } catch (error: any) {
        //console.log(`There is an error`, error);
        if (error) {
            //console.log(error.response.data);
            return {
                success: false,
                message: "There was an error processing data",
                data: error.response.data,
            };
        }
    }
};
