import Immutable from "immutable" ; import encodeNavigation , { EncodedNavigation } from "./encodeNavigation" ; import Output from "../models/output" ; import Page from "../models/page" ; export type EncodedChapters = Array < EncodedNavigation [ string ] & { path : string ; done : boolean ; percent : number ; } > ; export type PartialEncodedChapterValue = EncodedNavigation [ string ] & Partial < { path : string ; done : boolean ; percent : number ; } > ; export type EncodeProgress = { prevPercent : number ; percent : number ; current : number ; chapters : EncodedChapters ; } ; /**
 page.progress is a deprecated property from GitBook v2
 */ function encodeProgress ( output , page ) { const current = page . getPath ( ) ; const navigation = Immutable . Map < string , PartialEncodedChapterValue > ( encodeNavigation ( output ) ) ; const n = navigation . size ; let percent = 0 , prevPercent = 0 , currentChapter = null ; let done = true ; const chapters = navigation . map ( ( nav , chapterPath ) => { nav . path = chapterPath ; return nav ; } ) . valueSeq ( ) . sortBy ( ( nav ) => { return nav . index ; } ) . map ( ( nav , i ) => { nav . percent = ( i * 100 ) / Math . max ( n - 1 , 1 ) ; nav . done = done ; if ( nav . path == current ) { currentChapter = nav ; percent = nav . percent ; done = false ; } else if ( done ) { prevPercent = nav . percent ; } return nav ; } ) . toJS ( ) as EncodedChapters ; return { prevPercent : prevPercent , percent : percent , chapters : chapters , current : currentChapter , } ; } export default encodeProgress ;