import { deTagIds , Entity , EntityManager , EntityOrmField , fail , getMetadata , IdOf , isEntity , keyToNumber , OptsOf , PartialOrNull , } from "./index" ; /**
 * The base class for all entities.
 *
 * Currently this just adds the `.load(lensFn)` method for declarative reference traversal.
 */ export abstract class BaseEntity implements Entity { abstract id : string | undefined ; readonly __orm : EntityOrmField ; protected constructor ( em , metadata , defaultValues , opts ) { this . __orm = { em , metadata , data : { ... defaultValues } , originalData : { } , isNew : true , isTouched : false } ; if ( typeof opts === "string" ) { this . __orm . data [ "id" ] = opts ; this . __orm . isNew = false ; } em . register ( metadata , this ) ; } get idUntagged ( ) : string | undefined { if ( this . id ) { return deTagIds ( getMetadata ( this ) , [ this . id ] ) [ 0 ] ; } return undefined ; } get idUntaggedOrFail ( ) : string { return this . idUntagged || fail ( "Entity has no id yet" ) ; } abstract set ( values ) ; /**
     * Similar to `set` but applies "Partial API" semantics, i.e. `null` means unset and `undefined` means don't change.
     */ abstract setPartial ( values ) ; returns get idOrFail ( ) : IdOf < this > { return this . __orm . data [ "id" ] || fail ( "Entity has no id yet" ) ; } get isNewEntity ( ) : boolean { return this . __orm . isNew ; } get isDeletedEntity ( ) : boolean { return this . __orm . deleted !== undefined ; } get isDirtyEntity ( ) : boolean { return Object . keys ( this . __orm . originalData ) . length > 0 ; } get isPendingFlush ( ) : boolean { return this . isNewEntity || this . isDirtyEntity || this . isPendingDelete || this . __orm . isTouched ; } get isPendingDelete ( ) : boolean { return this . __orm . deleted === "pending" ; } toString ( ) { const meta = getMetadata ( this ) ; const id = keyToNumber ( meta , this . id ) || "new" ; return `${ meta . type }:${ id }` ; } /**
     * A very simple toJSON.
     *
     * This is not really meant as something you would actually put on the
     * wire as an API response, but instead is to keep accidental/debugging
     * JSON-ification of an Entity (i.e. by a logger like pino) to not
     * recurse into all of our References/Collections/EntityManager/etc.
     *  */ public toJSON ( ) { return Object . fromEntries ( Object . entries ( this . __orm . data ) . map ( ( [ key , value ] ) => { return [ key , isEntity ( value ) ? value . toString ( ) : value ] ; } ) ) ; } [ Symbol . toStringTag ] ( ) { return this . toString ( ) ; } }