import { lookup } from "./i18n" ; const defaultConfig = { abortEarly : true } ; type ErrorLike = { path : string ; message : string ; } ; type ContextOptions = { schemaMode ? : boolean ; abortEarly ? : boolean ; captureErrors ? : boolean ; throwErrors ? : boolean ; } ; class Context { errs : ErrorLike [ ] = [ ] ; errStack : string [ ] = [ ] ; objStack : string [ ] = [ ] ; objExactStack : boolean [ ] = [ ] ; config = { } ; batch : ErrorLike [ ] = [ ] ; isBatching : boolean = false ; blockErrors = "" ; schemaMode : boolean = false ; abortEarly : boolean = false ; captureErrors : boolean = false ; throwErrors : boolean = true ; constructor ( options = { } ) { this . reset ( ) ; Object . assign ( this , defaultConfig , options ) ; } reset ( options = { } ) { this . errs = [ ] ; this . errStack = [ ] ; this . objStack = [ ] ; this . objExactStack = [ ] ; this . config = { } ; this . batch = [ ] ; this . isBatching = false ; this . blockErrors = "" ; Object . assign ( this , options ) ; } batchStart ( ) { this . isBatching = true ; } batchCommit ( ) { this . errs = this . errs . concat ( this . batch ) ; } batchPurge ( ) { this . batch = [ ] ; this . isBatching = false ; } reportError ( msg , ... argstore ) { if ( ! msg ) return ; const message = msg . replace ( /\$(\d+)/g , ( ... matchArgs ) => { const [ , argIndex ] = matchArgs . slice ( 0 , - 2 ) ; return JSON . stringify ( argstore [ Number ( argIndex ) - 1 ] ) ; } ) . replace ( /\\\"/g , '"' ) ; const collection = this . isBatching ? this . batch : this . errs ; collection . push ( { path : this . objStack . join ( "." ) , message } ) ; } lookup ( key ) { return lookup ( key ) ; } pushObjStack ( key , isExactMatching ) { this . objExactStack . push ( isExactMatching ) ; const out = this . objStack . push ( key ) ; return out ; } popObjStack ( ) { this . objExactStack . pop ( ) ; const key = this . objStack . pop ( ) ; return key ; } getObjExactStack ( ) { return this . objExactStack ; } pushErrStack ( key ) { const out = this . errStack . push ( key ) ; return out ; } popErrStack ( ) { const key = this . errStack . pop ( ) ; return key ; } getErrors ( ) { return this . errs ; } } export default Context ;