import { useCallback } from 'react';
import useTrackEvent from '../useTrackEvent';

/**
 * @param {object} [opts]
 * @param {string} [opts.component]
 * @param {string} [opts.event]
 * @param {string} [opts.context]
 * @param {string} [opts.detail]
 */
const useTrackStandardEvent = function useTrackStandardEvent(opts = {}) {
  const {
    component: defaultComponent,
    event: defaultEvent,
    context: defaultContext,
    detail: defaultDetail
  } = opts;
  const trackEvent = useTrackEvent();
  return useCallback((cbOpts = {}) => {
    const {
      component = defaultComponent,
      event = defaultEvent,
      context = defaultContext,
      detail = defaultDetail
    } = cbOpts;
    if (!component || !event) return;
    const eventLabel = [`${component}:${event}`, context, detail].filter(Boolean).join(':');
    trackEvent(eventLabel);
  }, [trackEvent, defaultComponent, defaultEvent, defaultContext, defaultDetail]);
};
export default useTrackStandardEvent;