import { createCheckers } from "ts-interface-checker"
import { getCoreInstance } from "../coreInstanceGetter"
import { AnyKnownType, EventTeaser } from "./typings"

import CommonTypesTI from './commonTypes-ti'
import {ContentBlock, NavigationTypes} from "../modules/Navigation/typings"

const injectEvent = (
    properties: any,
    modulePropertiesTI: any[],
    eventType: any,
    moduleName: ContentBlock,
    userId = '',
    sendNow = false
) => {
    const capitalize = word => `${word[0].toUpperCase()}${word.slice(1)}`

    // Generates the type name that defines the properties (ex.: VirtualCreditProperties),
    // from the type of event (virtual_credit)
    //
    // ex.: virtual_credit -> VirtualCreditProperties
    const typeName = eventType
        .split('_')
        .map(capitalize)
        .join('')
        .concat('Properties')

    const { [typeName]: PropertiesChecker } = createCheckers(...modulePropertiesTI, CommonTypesTI)
    PropertiesChecker.check(properties)

    const cfLog = getCoreInstance()

    const eventTeaser: EventTeaser = {
        type: eventType,
        props: properties
    }

    cfLog.trackEvent(eventTeaser, moduleName, userId, sendNow)
}

export {
    injectEvent
}
