// useTrackEvent — emit a merchant-defined custom analytics event.
//
// Thin React wrapper over search-sdk's `trackCustomEvent`. Resolves the
// TrackContext from the surrounding <SearchProvider> and returns a stable
// callback the consumer invokes with their own event name + properties:
//
//   const track = useTrackEvent();
//   track("promo_viewed", { promo_id: "summer", discount: 10 });
//
// The event name becomes the `analytics_v4_<orgcode>.<EventName>` ClickHouse
// table; the merchant defines its schema server-side (admin-ui custom-events
// UI) and the Rotor `seekora-event-validate` function validates the payload
// downstream. SCRUM-259 Phase 3.
import { useCallback } from "react";
import { trackCustomEvent } from "@seekora-ai/ui-sdk-core";
import { useTrackContext } from "../components/SearchProvider";
/**
 * Returns a stable callback that emits a custom analytics event through the
 * V4 batch pipeline.
 *
 * Null-safe: when there is no surrounding <SearchProvider> (so no
 * TrackContext), the callback is a no-op — nothing to emit against. When a
 * context is present the call delegates to `trackCustomEvent`, which stamps
 * tenant context and throws on an empty name or a name that collides with a
 * reserved built-in event (a developer error worth surfacing).
 */
export function useTrackEvent() {
    const trackCtx = useTrackContext();
    return useCallback((eventName, properties = {}) => {
        if (!trackCtx)
            return;
        trackCustomEvent(trackCtx, eventName, properties);
    }, [trackCtx]);
}
