import { RnTags , ObjectUID } from '../../types/CommonTypes' ; export type Tag = { tag : string ; value : any ; } ; /**
 * The root class of the objects in Rhodonite
 */ export default class RnObject { static readonly InvalidObjectUID = - 1 ; static currentMaxObjectCount = 0 ; private static __uniqueNames : string [ ] = [ ] ; private static __objectsByNameMap : Map < string , RnObject > = new Map ( ) ; private static __objects : RnObject [ ] = [ ] ; private readonly __objectUid : ObjectUID = RnObject . InvalidObjectUID ; private __uniqueName : string ; private __tags : RnTags = { } ; private __combinedTagString = '' ; constructor ( ) { this . __objectUid = RnObject . currentMaxObjectCount ++ ; RnObject . __objects [ this . __objectUid ] = this ; this . __uniqueName = `${ this . constructor . name }__uid_${ this . __objectUid }` ; RnObject . __uniqueNames [ this . __objectUid ] = this . __uniqueName ; RnObject . __objectsByNameMap . set ( this . __uniqueName , this ) ; } static searchByTag ( tag , value ) { for ( const obj of RnObject . __objects ) { if ( obj . getTagValue ( tag ) === value ) { return obj ; } } return void 0 ; } /**
     * Gets the objectUID of the object.
     */ get objectUID ( ) : ObjectUID { return this . __objectUid ; } param objectUid static getRnObject ( objectUid ) { return RnObject . __objects [ objectUid ] ; } param uniqueName static getRnObjectByName ( uniqueName ) { return RnObject . __objectsByNameMap . get ( uniqueName ) ; } param name param toAddNameIfConflict tryToSetUniqueName ( name , toAddNameIfConflict ) { if ( RnObject . __uniqueNames . indexOf ( name ) !== - 1 ) { if ( toAddNameIfConflict ) { const newName = name + '_(' + this . __objectUid + ')' ; if ( RnObject . __uniqueNames . indexOf ( newName ) === - 1 ) { this . __uniqueName = newName ; RnObject . __uniqueNames [ this . __objectUid ] = this . __uniqueName ; RnObject . __objectsByNameMap . set ( this . __uniqueName , this ) ; return true ; } } return false ; } else { this . __uniqueName = name ; RnObject . __uniqueNames [ this . __objectUid ] = this . __uniqueName ; RnObject . __objectsByNameMap . set ( this . __uniqueName , this ) ; return true ; } } param val validateTagString ( val ) { const reg = new RegExp ( /[!"#$%&'()\*\+\-\s\.,\/:;<=>?@\[\\\]^`{|}~]/g ) ; if ( reg . test ( val ) ) { return false ; } return true ; } param tagName param tagValue tryToSetTag ( tag ) { if ( this . validateTagString ( tag . tag ) ) { if ( this . hasTag ( tag . tag ) ) { this . removeTag ( tag . tag ) ; } this . __tags [ tag . tag ] = tag . value ; this . __combinedTagString += `${ tag . tag }:${ tag . value }` + ' ' ; return true ; } return false ; } param tagName getTagValue ( tagName ) { return this . __tags [ tagName ] ; } param tagName getTag ( tagName ) { const tag = { tag : tagName , value : this . __tags [ tagName ] , } ; return tag ; } param tagName hasTag ( tagName ) { if ( this . __tags [ tagName ] != null ) { return true ; } else { return false ; } } param tagName removeTag ( tagName ) { const strToDelete = `${ tagName }:${ this . __tags [ tagName ] }` + ' ' ; this . __combinedTagString . replace ( this . __combinedTagString , '' ) ; delete this . __tags [ tagName ] ; } param tagName param tagValue matchTag ( tagName , tagValue ) { if ( this . __tags [ tagName ] === tagValue ) { return true ; } else { return false ; } } param stringArray matchTagsAsFreeStrings ( stringArray ) { let regExpStr = '^' ; for ( let i = 0 ; i < stringArray . length ; i ++ ) { regExpStr += `(?=.*${ stringArray [ i ] })` ; } const reg = new RegExp ( regExpStr ) ; if ( reg . test ( this . __combinedTagString ) ) { return true ; } else { return false ; } } param tags matchTags ( tags ) { let regExpStr = '^' ; for ( const tagName in tags ) { regExpStr += `(?=.*${ [ tagName ] }:${ tags [ tagName ] })` ; } const reg = new RegExp ( regExpStr ) ; if ( reg . test ( this . __combinedTagString ) ) { return true ; } else { return false ; } } /**
     * Get the unique name of the entity.
     */ get uniqueName ( ) { return this . __uniqueName ; } private static _reset ( ) { this . currentMaxObjectCount = 0 ; this . __uniqueNames = [ ] ; this . __objectsByNameMap = new Map ( ) ; this . __objects = [ ] ; } }