var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
import { RequestError } from "./utils/classes/RequestError";
const RequestsWrapper = (apiKey) => {
    const baseURL = "https://analytics.locallogic.co/track-v2";
    function post(url, data) {
        return __awaiter(this, void 0, void 0, function* () {
            const response = yield fetch(`${baseURL}${url}`, {
                body: JSON.stringify(data),
                headers: {
                    Accept: "application/json",
                    Authorization: apiKey,
                    "Content-type": "application/json",
                },
                method: "POST",
            });
            if (!response.ok) {
                // Try to parse message
                let error;
                try {
                    error = yield response.json();
                }
                catch (_error) {
                    // Do nothing
                }
                // Discard unauthorized messages since they are not useful
                const message = (error === null || error === void 0 ? void 0 : error.message) ? error.message : "";
                if (message.toLowerCase() !== "unauthorized") {
                    throw new RequestError(`Could not post tracking event: ${message}`);
                }
            }
        });
    }
    return {
        post,
    };
};
export default RequestsWrapper;
//# sourceMappingURL=requests.js.map