// import serverConnectAPI from "@/server_api/config/server-connect-api";

import serverConnectAPI from "../config/server-connect-api";


// const postAPI = async (postAPIValues: any | undefined): Promise<any> => {
//     const { apiEndpoint, hostName, formData, ...restParams } = postAPIValues;
//     // console.log('postAPIValues  ', postAPIValues);
//     if (formData) {
//         const formData = new FormData();
//         for (const newValues in restParams) {
//             formData.append(newValues, restParams[newValues]);
//         }
//         return await serverConnectAPI.post(apiEndpoint, formData, {
//             headers: {
//                 'Origin': hostName,
//                 "Content-Type": "multipart/form-data", Accept: "*/*",
//             }
//         });
//     } else {
//         return await serverConnectAPI.post(apiEndpoint, JSON.stringify(restParams), {
//             headers: {
//                 "Content-Type": "application/json; charset=utf-8",
//                 Accept: "application/json",
//                 'Origin': hostName
//             }
//         });
//     }
// };


// const apiPost = {
//     postAPI,
// };

// export default apiPost;




const PostAPI = {

    postAPI: async (postAPIValues: any) => {

        const {
            apiEndpoint,
            hostName,
            formData,
            ...restParams
        } = postAPIValues;

        // ---------------- FORM DATA ----------------
        if (formData) {

            const fd = new FormData();

            const appendValue = (value: any, key: string) => {

                // null / undefined
                if (value === undefined || value === null) {

                    fd.append(key, "");

                }

                // file
                else if (value instanceof File || value instanceof Blob) {

                    fd.append(key, value);

                }

                // array
                else if (Array.isArray(value)) {

                    value.forEach((item, index) => {
                        appendValue(item, `${key}[${index}]`);
                    });

                }

                // object
                else if (typeof value === "object") {

                    for (const nestedKey in value) {

                        appendValue(
                            value[nestedKey],
                            `${key}[${nestedKey}]`
                        );

                    }

                }

                // primitive
                else {

                    fd.append(key, value);

                }
            };

            // append all payload values
            for (const key in restParams) {

                appendValue(restParams[key], key);

            }

            // api call
            return await serverConnectAPI.post(
                apiEndpoint,
                fd,
                {
                    headers: {
                        Origin: hostName,
                        "Content-Type": "multipart/form-data",
                        Accept: "*/*",
                    },
                }
            );
        }

        // ---------------- JSON ----------------
        else {

            return await serverConnectAPI.post(
                apiEndpoint,
                JSON.stringify(restParams),
                {
                    headers: {
                        "Content-Type": "application/json; charset=utf-8",
                        Accept: "application/json",
                        Origin: hostName,
                    },
                }
            );
        }
    },
};

export default PostAPI;

