"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useAnalytics = void 0;
const tslib_1 = require("tslib");
const React = tslib_1.__importStar(require("react"));
const blockIdContext_1 = require("../../context/blockIdContext/index.js");
const models_1 = require("../../models/index.js");
const analyticsContext_1 = require("../context/analyticsContext/index.js");
const useAnalytics = (name = '', target) => {
    const { sendEvents, autoEvents } = React.useContext(analyticsContext_1.AnalyticsContext);
    const context = React.useContext(blockIdContext_1.BlockIdContext);
    const defaultEvent = React.useMemo(() => name
        ? {
            name,
            context: String(context),
            type: models_1.PredefinedEventTypes.Default,
            target: target,
        }
        : undefined, [context, name, target]);
    if (!sendEvents) {
        return () => { };
    }
    const defaultEvents = defaultEvent && autoEvents ? [defaultEvent] : [];
    return (e, additionalContext) => {
        let events = defaultEvents;
        if (e) {
            events = Array.isArray(e) ? [...events, ...e] : [...events, e];
        }
        if (!events) {
            return;
        }
        const preparedEvents = additionalContext
            ? events.map((event) => ({
                ...event,
                ...additionalContext,
            }))
            : events;
        sendEvents(preparedEvents);
    };
};
exports.useAnalytics = useAnalytics;
//# sourceMappingURL=useAnalytics.js.map
