import * as React from 'react';
import { BlockIdContext } from "../../context/blockIdContext/index.js";
import { PredefinedEventTypes } from "../../models/index.js";
import { AnalyticsContext } from "../context/analyticsContext/index.js";
export const useAnalytics = (name = '', target) => {
    const { sendEvents, autoEvents } = React.useContext(AnalyticsContext);
    const context = React.useContext(BlockIdContext);
    const defaultEvent = React.useMemo(() => name
        ? {
            name,
            context: String(context),
            type: 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);
    };
};
//# sourceMappingURL=useAnalytics.js.map
