async function sendPostRequest(url, data = {}, options = {}) {
    const {signal: signal = null, timeout: timeout = 25e3, headers: headers = {}, validateJSON: validateJSON = true, useFormData: useFormData = false, auth: auth = null} = options;
    let timeoutId = null;
    try {
        const authToken = auth?.token || document.querySelector('meta[name="auth-token"]')?.content || "";
        const authExpiry = auth?.expiry || document.querySelector('meta[name="auth-expiry"]')?.content || "";
        const authPublicKey = auth?.public_key || document.querySelector('meta[name="auth-public-key"]')?.content || "";
        let body;
        if (data instanceof FormData || data instanceof URLSearchParams) {
            body = data;
            if (authToken) {
                body.append("_token", authToken);
                body.append("_expiry", authExpiry);
                body.append("_public_key", authPublicKey);
            }
        } else if (typeof data === "object" && data !== null) {
            if (useFormData) {
                body = new FormData;
                if (authToken) {
                    body.append("_token", authToken);
                    body.append("_expiry", authExpiry);
                    body.append("_public_key", authPublicKey);
                }
                Object.keys(data).forEach(key => {
                    if (Array.isArray(data[key])) {
                        data[key].forEach(item => body.append(`${key}[]`, item));
                    } else if (typeof data[key] === "object" && data[key] !== null) {
                        body.append(key, JSON.stringify(data[key]));
                    } else {
                        body.append(key, data[key]);
                    }
                });
            } else {
                body = new URLSearchParams;
                if (authToken) {
                    body.append("_token", authToken);
                    body.append("_expiry", authExpiry);
                    body.append("_public_key", authPublicKey);
                }
                Object.keys(data).forEach(key => {
                    if (Array.isArray(data[key])) {
                        data[key].forEach(item => body.append(`${key}[]`, item));
                    } else if (typeof data[key] === "object" && data[key] !== null) {
                        body.append(key, JSON.stringify(data[key]));
                    } else {
                        body.append(key, data[key]);
                    }
                });
            }
        } else {
            body = data;
        }
        const config = {
            method: "POST",
            body: body
        };
        if (signal) {
            config.signal = signal;
        }
        if (timeout && !signal) {
            const controller = new AbortController;
            config.signal = controller.signal;
            timeoutId = setTimeout(() => controller.abort(), timeout);
        }
        const response = await fetch(url, config);
        if (timeoutId) clearTimeout(timeoutId);
        if (!response.ok) {
            throw new Error(`HTTP ${response.status}: ${response.statusText}`);
        }
        if (validateJSON) {
            const contentType = response.headers.get("content-type");
            if (!contentType || !contentType.includes("application/json")) {
                throw new Error("Invalid response format - Expected JSON");
            }
        }
        let responseText = await response.text();
        const jsonStart = responseText.indexOf("{");
        if (jsonStart > 0) {
            responseText = responseText.substring(jsonStart);
        }
        const result = JSON.parse(responseText);
        if (result.status === false && result.code === 101) {
            const sessionExpiredMsg = getLang("symbol_detail.header.errors.session_expired", "Your session has expired. Please refresh the page.");
            import("../utils/helper.js").then(({showNotification: showNotification}) => {
                showNotification(sessionExpiredMsg, "error", false);
            }).catch(() => {
                alert(sessionExpiredMsg);
            });
            throw new Error(result.msg || "Session expired");
        }
        if (result.success === false || result.error) {
            const requestFailedMsg = getLang("symbol_detail.header.errors.request_failed", "Request failed");
            throw new Error(result.message || requestFailedMsg);
        }
        return result;
    } catch (error) {
        if (timeoutId) clearTimeout(timeoutId);
        const enhancedError = new Error(error.message);
        enhancedError.name = error.name;
        enhancedError.url = url;
        enhancedError.originalError = error;
        if (error.name !== "AbortError") {
            console.error("POST Request Error:", {
                url: url,
                error: error.message,
                name: error.name
            });
        }
        throw enhancedError;
    }
}

export { sendPostRequest };

if (typeof window !== "undefined") {
    window.sendPostRequest = sendPostRequest;
}