import { FlashcardType } from 'polar-shared/src/metadata/FlashcardType' ; import { ClozeFields , FrontAndBackFields } from './flashcard_input/FlashcardInputs' ; import { IDocAnnotation } from '../../DocAnnotation' ; import { Flashcard } from '../../../metadata/Flashcard' ; import { IRef , Refs } from 'polar-shared/src/metadata/Refs' ; import { Flashcards } from '../../../metadata/Flashcards' ; import { DocMetas } from '../../../metadata/DocMetas' ; import { IDocMeta } from "polar-shared/src/metadata/IDocMeta" ; import { IPageMeta } from "polar-shared/src/metadata/IPageMeta" ; import { IDStr } from "polar-shared/src/util/Strings" ; import { Analytics } from '../../../analytics/Analytics' ; export class FlashcardActions { public static create ( parent , pageMeta , type , fields ) { const flashcard = this . newInstanceFromParentRef ( parent , type , fields ) ; if ( flashcard ) { pageMeta . flashcards [ flashcard . id ] = Flashcards . createMutable ( flashcard ) ; Analytics . event2 ( 'annotation-manualFlashcardCreated' , { type } ) ; } } public static update ( docMeta , pageMeta , parent , type , fields , existingFlashcardID ? ) { const flashcard = this . newInstanceFromParentRef ( parent , type , fields ) ; if ( flashcard ) { DocMetas . withBatchedMutations ( docMeta , ( ) => { if ( existingFlashcardID ) { delete pageMeta . flashcards [ existingFlashcardID ] ; } pageMeta . flashcards [ flashcard . id ] = < Flashcard > { ... flashcard } ; } ) ; } } public static delete ( docMeta , pageMeta , parent , existingID ) { DocMetas . withBatchedMutations ( docMeta , ( ) => { delete pageMeta . flashcards [ existingID ] ; } ) ; } /**
     * Create a new instance from the given fields.
     */ private static newInstance ( parent , type , fields ) { const parentRef = { value : parent . id , type : Refs . toRefType ( parent . annotationType ) } ; return this . newInstanceFromParentRef ( parentRef , type , fields ) ; } private static newInstanceFromParentRef ( parent , type , fields ) { const ref = Refs . create ( parent . value , parent . type ) ; if ( type === FlashcardType . BASIC_FRONT_BACK ) { const frontAndBackFields = fields as FrontAndBackFields ; const { front , back } = frontAndBackFields ; return Flashcards . createFrontBack ( front , back , ref ) ; } if ( type === FlashcardType . CLOZE ) { const clozeFields = fields as ClozeFields ; const { text } = clozeFields ; return Flashcards . createCloze ( text , ref ) ; } return undefined ; } }