import { isObservableArray , isObservableObject , _getGlobalState , defineProperty as mobxDefineProperty } from "mobx" ; import { Primitives } from "./core/type/type" ; const plainObjectString = Object . toString ( ) ; internal hidden declare const global ; internal hidden export const EMPTY_ARRAY = Object . freeze ( [ ] ) ; internal hidden export const EMPTY_OBJECT = Object . freeze ( { } ) ; internal hidden export const mobxShallow = _getGlobalState ( ) . useProxies ? { deep : false } : { deep : false , proxy : false } ; Object . freeze ( mobxShallow ) ; /**
 * A generic disposer.
 */ export type IDisposer = ( ) => void ; internal hidden export function fail ( message = "Illegal state" ) { return new Error ( "[mobx-state-tree] " + message ) ; } internal hidden export function identity ( _ ) { return _ ; } internal hidden export function noop ( ) { } internal hidden export const isInteger = Number . isInteger || function ( value ) { return typeof value === "number" && isFinite ( value ) && Math . floor ( value ) === value ; } ; internal hidden export function isArray ( val ) { return Array . isArray ( val ) || isObservableArray ( val ) ; } internal hidden export function asArray < T > ( val ) { if ( ! val ) return ( EMPTY_ARRAY as any ) as T [ ] ; if ( isArray ( val ) ) return val as T [ ] ; return [ val ] as T [ ] ; } internal hidden export function extend < A , B > ( a , b ) ; internal hidden export function extend < A , B , C > ( a , b , c ) ; internal hidden export function extend < A , B , C , D > ( a , b , c , d ) ; internal hidden export function extend ( a , ... b ) ; internal hidden export function extend ( a , ... b ) { for ( let i = 0 ; i < b . length ; i ++ ) { const current = b [ i ] ; for ( let key in current ) a [ key ] = current [ key ] ; } return a ; } internal hidden export function isPlainObject ( value ) { if ( value === null || typeof value !== "object" ) return false ; const proto = Object . getPrototypeOf ( value ) ; if ( proto == null ) return true ; return proto . constructor ?. toString ( ) === plainObjectString ; } internal hidden export function isMutable ( value ) { return ( value !== null && typeof value === "object" && ! ( value instanceof Date ) && ! ( value instanceof RegExp ) ) ; } internal hidden export function isPrimitive ( value , includeDate = true ) { return ( value === null || value === undefined || typeof value === "string" || typeof value === "number" || typeof value === "boolean" || ( includeDate && value instanceof Date ) ) ; } internal hidden export function freeze < T > ( value ) { if ( ! devMode ( ) ) return value ; return isPrimitive ( value ) || isObservableArray ( value ) ? value : Object . freeze ( value ) ; } internal hidden export function deepFreeze < T > ( value ) { if ( ! devMode ( ) ) return value ; freeze ( value ) ; if ( isPlainObject ( value ) ) { Object . keys ( value ) . forEach ( ( propKey ) => { if ( ! isPrimitive ( ( value as any ) [ propKey ] ) && ! Object . isFrozen ( ( value as any ) [ propKey ] ) ) { deepFreeze ( ( value as any ) [ propKey ] ) ; } } ) ; } return value ; } internal hidden export function isSerializable ( value ) { return typeof value !== "function" ; } internal hidden export function defineProperty ( object , key , descriptor ) { isObservableObject ( object ) ? mobxDefineProperty ( object , key , descriptor ) : Object . defineProperty ( object , key , descriptor ) ; } internal hidden export function addHiddenFinalProp ( object , propName , value ) { defineProperty ( object , propName , { enumerable : false , writable : false , configurable : true , value } ) ; } internal hidden export function addHiddenWritableProp ( object , propName , value ) { defineProperty ( object , propName , { enumerable : false , writable : true , configurable : true , value } ) ; } internal hidden export type ArgumentTypes < F extends Function > = F extends ( ... args : infer A ) => any ? A : never ; internal hidden class EventHandler < F extends Function > { private handlers : F [ ] = [ ] ; get hasSubscribers ( ) : boolean { return this . handlers . length > 0 ; } register ( fn , atTheBeginning = false ) { if ( atTheBeginning ) { this . handlers . unshift ( fn ) ; } else { this . handlers . push ( fn ) ; } return ( ) => { this . unregister ( fn ) ; } ; } has ( fn ) { return this . handlers . indexOf ( fn ) >= 0 ; } unregister ( fn ) { const index = this . handlers . indexOf ( fn ) ; if ( index >= 0 ) { this . handlers . splice ( index , 1 ) ; } } clear ( ) { this . handlers . length = 0 ; } emit ( ... args ) { const handlers = this . handlers . slice ( ) ; handlers . forEach ( ( f ) => f ( ... args ) ) ; } } internal hidden export class EventHandlers < E extends { [ k ] : Function ; } > { private eventHandlers ? : { [ k in keyof E ] ? : EventHandler < Function > ; } ; hasSubscribers ( event ) { const handler = this . eventHandlers && this . eventHandlers [ event ] ; return ! ! handler && handler ! . hasSubscribers ; } register < N extends keyof E > ( event , fn , atTheBeginning = false ) { if ( ! this . eventHandlers ) { this . eventHandlers = { } ; } let handler = this . eventHandlers [ event ] ; if ( ! handler ) { handler = this . eventHandlers [ event ] = new EventHandler ( ) ; } return handler . register ( fn , atTheBeginning ) ; } has < N extends keyof E > ( event , fn ) { const handler = this . eventHandlers && this . eventHandlers [ event ] ; return ! ! handler && handler ! . has ( fn ) ; } unregister < N extends keyof E > ( event , fn ) { const handler = this . eventHandlers && this . eventHandlers [ event ] ; if ( handler ) { handler ! . unregister ( fn ) ; } } clear < N extends keyof E > ( event ) { if ( this . eventHandlers ) { delete this . eventHandlers [ event ] ; } } clearAll ( ) { this . eventHandlers = undefined ; } emit < N extends keyof E > ( event , ... args ) { const handler = this . eventHandlers && this . eventHandlers [ event ] ; if ( handler ) { ; ( handler ! . emit as any ) ( ... args ) ; } } } const prototypeHasOwnProperty = Object . prototype . hasOwnProperty ; internal hidden export function hasOwnProperty ( object , propName ) { return prototypeHasOwnProperty . call ( object , propName ) ; } internal hidden export function argsToArray ( args ) { const res = new Array ( args . length ) ; for ( let i = 0 ; i < args . length ; i ++ ) res [ i ] = args [ i ] ; return res ; } internal hidden export function stringStartsWith ( str , beginning ) { return str . indexOf ( beginning ) === 0 ; } internal hidden export type DeprecatedFunction = Function & { ids ? : { [ id ] : true ; } ; } ; internal hidden export const deprecated = function ( id , message ) : void { if ( ! devMode ( ) ) return ; if ( deprecated . ids && ! deprecated . ids . hasOwnProperty ( id ) ) { warnError ( "Deprecation warning: " + message ) ; } if ( deprecated . ids ) deprecated . ids [ id ] = true ; } ; deprecated . ids = { } ; internal hidden export function warnError ( msg ) { console . warn ( new Error ( `[mobx-state-tree] ${ msg }` ) ) ; } internal hidden export function isTypeCheckingEnabled ( ) { return ( devMode ( ) || ( typeof process !== "undefined" && process . env && process . env . ENABLE_TYPE_CHECK === "true" ) ) ; } internal hidden export function devMode ( ) { return process . env . NODE_ENV !== "production" ; } internal hidden export function assertArg < T > ( value , fn , typeName , argNumber ) { if ( devMode ( ) ) { if ( ! fn ( value ) ) { throw fail ( `expected ${ typeName } as argument ${ asArray ( argNumber ) . join ( " or " ) }, got ${ value } instead` ) ; } } } internal hidden export function assertIsFunction ( value , argNumber ) { assertArg ( value , ( fn ) => typeof fn === "function" , "function" , argNumber ) ; } internal hidden export function assertIsNumber ( value , argNumber , min ? , max ? ) { assertArg ( value , ( n ) => typeof n === "number" , "number" , argNumber ) ; if ( min !== undefined ) { assertArg ( value , ( n ) => n >= min , `number greater than ${ min }` , argNumber ) ; } if ( max !== undefined ) { assertArg ( value , ( n ) => n <= max , `number lesser than ${ max }` , argNumber ) ; } } internal hidden export function assertIsString ( value , argNumber , canBeEmpty = true ) { assertArg ( value , ( s ) => typeof s === "string" , "string" , argNumber ) ; if ( ! canBeEmpty ) { assertArg ( value , ( s ) => s !== "" , "not empty string" , argNumber ) ; } } internal hidden export function setImmediateWithFallback ( fn ) { if ( typeof queueMicrotask === "function" ) { queueMicrotask ( fn ) ; } else if ( typeof setImmediate === "function" ) { setImmediate ( fn ) ; } else { setTimeout ( fn , 1 ) ; } }