import { isDate } from 'lodash' ; import type { HttpStart } from 'src/core/public' ; import { LicenseType } from '../../common/types' ; public export interface FeatureUsageServiceSetup { /**
     * Register a feature to be able to notify of it's usages using the  FeatureUsageServiceStart .
      register ( featureName , licenseType ) : void ; } public export interface FeatureUsageServiceStart { param featureName param usedAt notifyUsage ( featureName , usedAt ? ) : Promise < void > ; } interface StartDeps { http : HttpStart ; } internal export class FeatureUsageService { private readonly registrations : Array < { featureName : string ; licenseType : LicenseType ; } > = [ ] ; public setup ( ) { return { register : async ( featureName , licenseType ) => { this . registrations . push ( { featureName , licenseType } ) ; } , } ; } public start ( { http } ) { const registrationPromise = http . anonymousPaths . isAnonymous ( window . location . pathname ) || this . registrations . length === 0 ? Promise . resolve ( ) : http . post ( '/internal/licensing/feature_usage/register' , { body : JSON . stringify ( this . registrations ) , } ) ; return { notifyUsage : async ( featureName , usedAt = Date . now ( ) ) => { if ( http . anonymousPaths . isAnonymous ( window . location . pathname ) ) return ; await registrationPromise ; const lastUsed = isDate ( usedAt ) ? usedAt . getTime ( ) : usedAt ; await http . post ( '/internal/licensing/feature_usage/notify' , { body : JSON . stringify ( { featureName , lastUsed , } ) , } ) ; } , } ; } }