"use strict";
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());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.useAnalytics = void 0;
exports.getCookie = getCookie;
const axios_1 = __importDefault(require("axios"));
const package_json_1 = __importDefault(require("../../package.json"));
const hooks_1 = require("../hooks");
const utility_1 = require("../utility");
function getCookie(name) {
    var _a, _b;
    try {
        if (typeof window === "undefined") {
            return "";
        }
        const cookie = `; ${document.cookie}`.split(`; ${name}=`);
        if (cookie.length === 2) {
            return (_b = (_a = cookie.pop()) === null || _a === void 0 ? void 0 : _a.split(";").shift()) !== null && _b !== void 0 ? _b : "";
        }
    }
    catch (error) {
        console.error(error);
    }
    return "";
}
const useAnalytics = () => {
    const { options, search, curation } = (0, hooks_1.useReactifySearchContext)();
    const track = (event) => __awaiter(void 0, void 0, void 0, function* () {
        var _a, _b;
        if (!options.shopifyPermanentDomain) {
            console.warn(new Error('Unable to send tracking event, missing value for "shopifyPermanentDomain".'));
            return;
        }
        const attribution = {
            market: options.market,
            curation: (_a = curation === null || curation === void 0 ? void 0 : curation.id) !== null && _a !== void 0 ? _a : "",
            searchTerm: (_b = search.searchTerm) !== null && _b !== void 0 ? _b : "",
            collectionHandle: options.mode === "collection" ? options.collectionHandle : "",
            recommendation: "",
        };
        utility_1.debug.log("useAnalytics", "track", event, attribution);
        const events = getTrackEvents(event);
        return axios_1.default.post(`https://analytics.usereactify.com/record/`, {
            events,
            attribution,
        }, {
            headers: {
                "x-reactify-shop": options.shopifyPermanentDomain,
                "x-reactify-mode": options.mode,
                "x-reactify-client-id": options.clientId,
                "x-reactify-client-version": package_json_1.default.version,
                "x-reactify-ga": getCookie("_rs_ga"),
            },
        });
    });
    return { track };
};
exports.useAnalytics = useAnalytics;
function getTrackEvents(event) {
    const { eventName } = event;
    let events = [];
    switch (eventName) {
        case "search":
            events = [
                {
                    eventName,
                    payload: Object.assign(Object.assign({}, event.payload), { searchTerm: event.payload.searchTerm.trim() }),
                },
            ];
            break;
        case "zeroResults":
            events = [
                {
                    eventName,
                    payload: Object.assign(Object.assign({}, event.payload), { searchTerm: event.payload.searchTerm.trim() }),
                },
            ];
            break;
        case "viewProduct":
            events = [
                {
                    eventName,
                    payload: event.payload,
                },
            ];
            break;
        case "clickProduct":
            events = [
                {
                    eventName,
                    payload: event.payload,
                },
            ];
            break;
        case "viewPromotion":
            events = [
                {
                    eventName,
                    payload: event.payload,
                },
            ];
            break;
        case "clickPromotion":
            events = [
                {
                    eventName,
                    payload: event.payload,
                },
            ];
            break;
        case "paginationChange":
            events = [
                {
                    eventName,
                    payload: event.payload,
                },
            ];
            break;
        case "sortChange":
            events = [
                {
                    eventName,
                    payload: event.payload,
                },
            ];
            break;
        case "filterChange":
            events = [
                {
                    eventName,
                    payload: event.payload,
                },
            ];
            break;
    }
    return events;
}
//# sourceMappingURL=useAnalytics.js.map