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

export const useAnalyticsStartItemCreation = () => {
  const useAnalyticsSDK = useFeatureIsOn(FLAGS.USE_ANALYTICS_SDK_TALENT)
  const { sendSegmentEvent } = useAnalytics()
  const { createTracker } = useCreatorTracking()
  const startedItemCreation = createTracker(CreatorEvent.STARTED_ITEM_CREATION)

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

    if (!moduleId) {
      return
    }

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

  return { trackStartAddingItemToModule }
}
