import { fail , ObjectNode , splitJsonPath , joinJsonPath , ScalarNode , IChildNodesMap , EMPTY_ARRAY , AnyObjectNode , AnyNode , IAnyType , IType , assertArg , STNValue , Instance , IAnyComplexType } from "../../internal" ; internal hidden export enum NodeLifeCycle { INITIALIZING , CREATED , FINALIZED , DETACHING , DEAD } hidden declare const $stateTreeNodeType ; hidden export interface IStateTreeNode < IT extends IAnyType = IAnyType > { internal readonly $treenode ? : any ; readonly [ $stateTreeNodeType ] ? : [ IT ] | [ any ] ; } hidden export type TypeOfValue < T extends IAnyStateTreeNode > = T extends IStateTreeNode < infer IT > ? IT : never ; hidden export interface IAnyStateTreeNode extends STNValue < any , IAnyType > { } param value returns export function isStateTreeNode < IT extends IAnyComplexType = IAnyComplexType > ( value ) { return ! ! ( value && value . $treenode ) ; } internal hidden export function assertIsStateTreeNode ( value , argNumber ) { assertArg ( value , isStateTreeNode , "mobx-state-tree node" , argNumber ) ; } internal hidden export function getStateTreeNode ( value ) { if ( ! isStateTreeNode ( value ) ) { throw fail ( `Value ${ value } is no MST Node` ) ; } return value . $treenode ! ; } internal hidden export function getStateTreeNodeSafe ( value ) { return ( value && value . $treenode ) || null ; } internal hidden export function toJSON < S > ( this ) { return getStateTreeNode ( this ) . snapshot ; } const doubleDot = ( _ ) => ".." ; internal hidden export function getRelativePathBetweenNodes ( base , target ) { if ( base . root !== target . root ) { throw fail ( `Cannot calculate relative path: objects '${ base }' and '${ target }' are not part of the same object tree` ) ; } const baseParts = splitJsonPath ( base . path ) ; const targetParts = splitJsonPath ( target . path ) ; let common = 0 ; for ( ; common < baseParts . length ; common ++ ) { if ( baseParts [ common ] !== targetParts [ common ] ) break ; } return ( baseParts . slice ( common ) . map ( doubleDot ) . join ( "/" ) + joinJsonPath ( targetParts . slice ( common ) ) ) ; } internal hidden export function resolveNodeByPath ( base , path , failIfResolveFails = true ) { return resolveNodeByPathParts ( base , splitJsonPath ( path ) , failIfResolveFails ) ; } internal hidden export function resolveNodeByPathParts ( base , pathParts , failIfResolveFails = true ) { let current = base ; for ( let i = 0 ; i < pathParts . length ; i ++ ) { const part = pathParts [ i ] ; if ( part === ".." ) { current = current ! . parent ; if ( current ) continue ; } else if ( part === "." ) { continue ; } else if ( current ) { if ( current instanceof ScalarNode ) { try { const value = current . value ; if ( isStateTreeNode ( value ) ) { current = getStateTreeNode ( value ) ; } } catch ( e ) { if ( ! failIfResolveFails ) { return undefined ; } throw e ; } } if ( current instanceof ObjectNode ) { const subType = current . getChildType ( part ) ; if ( subType ) { current = current . getChildNode ( part ) ; if ( current ) continue ; } } } if ( failIfResolveFails ) throw fail ( `Could not resolve '${ part }' in path '${ joinJsonPath ( pathParts . slice ( 0 , i ) ) || "/" }' while resolving '${ joinJsonPath ( pathParts ) }'` ) ; else return undefined ; } return current ! ; } internal hidden export function convertChildNodesToArray ( childNodes ) { if ( ! childNodes ) return EMPTY_ARRAY as AnyNode [ ] ; const keys = Object . keys ( childNodes ) ; if ( ! keys . length ) return EMPTY_ARRAY as AnyNode [ ] ; const result = new Array ( keys . length ) as AnyNode [ ] ; keys . forEach ( ( key , index ) => { result [ index ] = childNodes ! [ key ] ; } ) ; return result ; }