import { Compiler , Injectable , Injector , OnDestroy } from '@angular/core' ; import { BehaviorSubject , of , Subject , Subscription } from 'rxjs' ; import { catchError , map , take } from 'rxjs/operators' ; import { ApiService } from '../../common/api/api.service' ; import { StackableModalEvent , StackableModalService , StackableModalState , } from '../../services/ux/stackable-modal.service' ; import { OnboardingV3ModalComponent } from './modal/onboarding-modal.component' ; /**
 * GET api/v3/onboarding response.
 */ export type OnboardingResponse = { status : string ; id ? : OnboardingGroup ; completed_pct ? : number ; is_completed ? : boolean ; steps ? : OnboardingStep [ ] ; } | null ; /**
 * Individual onboarding steps.
 */ export type OnboardingStep = { id : OnboardingStepName ; is_completed : boolean ; } ; /**
 * Names of different onboarding steps.
 */ export type OnboardingStepName = 'SuggestedHashtagsStep' | 'WelcomeStep' | 'VerifyEmailStep' | 'SetupChannelStep' | 'VerifyUniquenessStep' | 'VerifyPhoneStep' | 'VerifyBankStep' | 'VerifyWalletStep' | 'CreatePostStep' ; /**
 * Onboarding groups - possible ids returned from api/v3/onboarding
 */ export type OnboardingGroup = 'InitialOnboardingGroup' | 'OngoingOnboardingGroup' ; /**
 * Groups that the front-end has support for.
 */ export const RELEASED_GROUPS = [ 'InitialOnboardingGroup' ] ; /**
 * Core service for onboarding v3 for loading
 * progress and opening the modal.
 */ @ Injectable ( { providedIn : 'root' , } ) export class OnboardingV3Service implements OnDestroy { private subscriptions : Subscription [ ] = [ ] ; public readonly progress$ : BehaviorSubject < OnboardingResponse > = new BehaviorSubject < OnboardingResponse > ( null ) ; /**
     * True if object is completed
     */ public readonly completed$ : BehaviorSubject < boolean > = new BehaviorSubject < boolean > ( false ) ; /**
     * True if still loading
     */ public readonly loading$ : BehaviorSubject < boolean > = new BehaviorSubject < boolean > ( true ) ; constructor ( private compiler , private injector , private stackableModal , private api ) { } ngOnDestroy ( ) { for ( let subscription of this . subscriptions ) { subscription . unsubscribe ( ) ; } } returns { Promise < any > } public async open ( ) { const { OnboardingV3ProgressLazyModule } = await import ( './onboarding.lazy.module' ) ; const moduleFactory = await this . compiler . compileModuleAsync ( OnboardingV3ProgressLazyModule ) ; const moduleRef = moduleFactory . create ( this . injector ) ; const componentFactory = moduleRef . instance . resolveComponent ( ) ; const onSuccess$ = new Subject ( ) ; const evt = await this . stackableModal . present ( OnboardingV3ModalComponent , null , { wrapperClass : 'm-modalV2__wrapper' , dismissOnRouteChange : false , onSaveIntent : ( step ) => { } , onDismissIntent : ( ) => { this . dismiss ( ) ; } , } ) . toPromise ( ) ; if ( evt . state === StackableModalState . Dismissed && ! onSuccess$ . isStopped ) { throw 'DismissedModalException' ; } return onSuccess$ . toPromise ( ) ; } param { OnboardingStepName } step returns { void } public forceCompletion ( step ) { this . subscriptions . push ( this . progress$ . pipe ( take ( 1 ) , catchError ( ( e ) => { console . error ( e ) ; return of ( e ) ; } ) , map ( progress => progress . steps . map ( ( progressStep ) => { if ( step === progressStep . id ) { progressStep . is_completed = true ; } } ) ) ) . subscribe ( ) ) ; } returns { Promise < void > } public async presentHomepageModals ( ) { try { await this . open ( ) ; } catch ( e ) { if ( e === 'DismissedModalException' ) { return ; } console . error ( e ) ; } } returns { void } public dismiss ( ) { try { this . stackableModal . dismiss ( ) ; } catch ( e ) { } } returns { Promise < void > } public async load ( ) { this . subscriptions . push ( this . api . get ( '/api/v3/onboarding' ) . pipe ( take ( 1 ) , catchError ( e => { console . error ( e ) ; this . loading$ . next ( false ) ; return of ( null ) ; } ) ) . subscribe ( ( progress ) => { this . progress$ . next ( progress ) ; this . loading$ . next ( false ) ; } ) ) ; } }