import { useAnalytics } from 'hooks/useAnalytics'
import { FLAGS, useFeatureIsOn } from '@komi-app/flags-sdk'
import { CreatorEvent, useCreatorTracking } from '@komi-app/analytics-sdk'
import { SEGMENT_EVENTS } from 'constants/segment'
import { TalentProfileModule } from '../models/talent/talent-profile-module.model'

export const useAnalyticsModuleAdded = () => {
  const useAnalyticsSDK = useFeatureIsOn(FLAGS.USE_ANALYTICS_SDK_TALENT)
  const { sendSegmentEvent } = useAnalytics()
  const { createTracker } = useCreatorTracking()
  const moduleAdded = createTracker(CreatorEvent.MODULE_ADDED)

  const trackModuleAdded = <T>(module: TalentProfileModule<T>) => {
    const moduleId = module.id

    if (!moduleId) {
      return
    }

    if (useAnalyticsSDK) {
      moduleAdded({
        moduleId
      })
    } else {
      sendSegmentEvent(SEGMENT_EVENTS.MODULE_ADDED, {
        'Module ID': moduleId
      })
    }
  }

  return { trackModuleAdded }
}
