import { json , yaml , ini } from 'mrm-core' ; import { File } from './File' ; /**
 * Exposes the API to work with key/value pair files like `ini`, `yaml`
 * and `json`.
 */ export abstract class KeyValuePair extends File { protected actions = [ ] ; /**
     * Only these key-value pair files are supported
     */ public abstract filePointer : ReturnType < typeof json > | ReturnType < typeof yaml > | ReturnType < typeof ini > ; constructor ( basePath ) { super ( basePath ) ; } /**
     * Set key/value pair
     */ public set ( key , value ) { this . addAction ( 'set' , { key , value } ) ; return this ; } /**
     * Unset key/value pair
     */ public unset ( key ) { this . addAction ( 'unset' , { key } ) ; return this ; } /**
     * Remove file
     */ public delete ( ) { this . addAction ( 'delete' ) ; return this ; } /**
     * Returns value for a given key from the file
     */ public get ( ) ; public get ( address , defaultValue ? ) ; public get ( address ? , defaultValue ? ) { return address ? this . filePointer . get ( address , defaultValue ) : this . filePointer . get ( ) ; } /**
     * A boolean telling if the file already exists
     */ public exists ( ) { return this . filePointer . exists ( ) ; } /**
     * Commit mutations
     */ public commit ( ) { this . cdIn ( ) ; const actions = this . getCommitActions ( ) ; const deleteFile = actions . find ( ( { action } ) => action === 'delete' ) ; /**
         * In case of `delete` action. There is no point running
         * other actions and we can simply delete the file
         */ if ( deleteFile ) { this . filePointer . delete ( ) ; this . cdOut ( ) ; return ; } actions . forEach ( ( { action , body } ) => { if ( typeof this [ `on${ action }` ] === 'function' ) { const handled = this [ `on${ action }` ] ( 'commit' , body ) ; /**
                 * Return early when action is handled by the hook
                 */ if ( handled ) { return ; } } if ( action === 'set' ) { this . filePointer . set ( body . key , body . value ) ; return ; } if ( action === 'unset' ) { this . filePointer . unset ( body . key ) ; } } ) ; this . filePointer . save ( ) ; this . cdOut ( ) ; } /**
     * Rollback mutations
     */ public rollback ( ) { this . cdIn ( ) ; const actions = this . getRevertActions ( ) ; actions . forEach ( ( { action , body } ) => { if ( typeof this [ `on${ action }` ] === 'function' ) { const handled = this [ `on${ action }` ] ( 'rollback' , body ) ; /**
                 * Return early when action is handled by the hook
                 */ if ( handled ) { return ; } } if ( action === 'set' ) { this . filePointer . unset ( body . key ) ; return ; } } ) ; this . filePointer . save ( ) ; this . cdOut ( ) ; } }