import { Client } from '@src/core' ; import { ToJsonProperties } from '@src/api/entities/interfaces/ToJsonProperties' ; import { Json , JsonProperties } from '@src/api/entities/interfaces/Json' ; import { ToJsonOverrideSymbol } from '@src/constants' ; export abstract class AbstractEntity { public client : Client ; constructor ( client ) { this . client = client ; } abstract init ( data ) ; async _clone ( ) { return await new ( this . constructor as any ) ( this . client ) . init ( this . toJson ( ) ) ; } toJson ( properties = { } , obj ? ) { const json = { } ; const keys = Object . keys ( properties ) ; for ( const key of keys ) { const target = obj ?? this ; const prop = properties [ key ] , value = target [ key ] ; if ( typeof prop === 'object' ) { if ( prop . override === ToJsonOverrideSymbol ) json [ key ] = AbstractEntity . _handle ( prop . value ) ; else json [ key ] = AbstractEntity . _handle ( prop ) ; } else if ( prop ) { json [ key ] = AbstractEntity . _handle ( value ) ; } } return json ; } private static _handle ( data ) { switch ( typeof data ) { case 'string' : case 'boolean' : case 'number' : return data ; case 'bigint' : return data . toString ( ) + 'n' ; case 'object' : { if ( data === null ) return null ; if ( typeof data . toJson === 'function' ) return data . toJson ( ) ; return JSON . parse ( JSON . stringify ( data , ( k , v ) => typeof v === 'bigint' ? v . toString ( ) + 'n' : v ) ) ; } } return null ; } }