import { NamedProperty , PropertyFactory , SetProperty } from "@fluid-experimental/property-properties" ; import { PropertyProxy } from "./propertyProxy" ; import { PropertyProxyErrors } from "./errors" ; import { forceType , Utilities } from "./utilities" ; /**
 * The function returns an iterator for  external .
 *  param target The  ComponentSet that holds a reference
 * to the  external .
 *  return hidden const createSetIterator = ( target ) => function * ( ) { const property = target . getProperty ( ) ; const keys = property . getIds ( ) ; for ( const key of keys ) { const property_to_proxy = property . get ( key ) ; if ( property_to_proxy ) { yield PropertyProxy . proxify ( property_to_proxy ) ; } else { throw new Error ( PropertyProxyErrors . INVALID_PROPERTY ) ; } } } ; /**
 * ComponentSet extends Set in such a way that a referenced  external * can be modified and accessed directly.
 *  extends Set hidden class ComponentSet extends Set { private readonly property ! : SetProperty ; /**
     * Sets the  external to operate on sets the Symbol.iterator attribute.
     *  param property The  external to operate on.
      constructor ( property ) { super ( ) ; Object . defineProperty ( this , "property" , { enumerable : false , value : property } ) ; this [ Symbol . iterator ] = createSetIterator ( this ) ; } /**
     * Retrieves the length of the array returned by  external to infer
     * the size (number of entries).
     *  return The size of the  external .
      get size ( ) { return this . property . getIds ( ) . length ; } /**
     * Returns the wrapped  external property.
     *  return The wrapped  external .
      getProperty ( ) { return this . property ; } inheritdoc add ( value ) { let valueIsProperty = false ; if ( PropertyFactory . instanceOf ( value , "BaseProperty" ) ) { valueIsProperty = true ; } else { value = PropertyFactory . create ( this . property . getTypeid ( ) , "single" , value ) ; } if ( valueIsProperty ) { this . delete ( value ) ; } this . property . insert ( value ) ; return this ; } inheritdoc clear ( ) { Utilities . wrapWithPushPopNotificationDelayScope ( this . property , ( ) => { this . property . clear ( ) ; } ) ; } inheritdoc delete ( value ) { if ( ! this . has ( value ) ) { return false ; } const guid = this . _getGuid ( value ) ; this . _deleteById ( guid ) ; return true ; } inheritdoc entries ( ) { const keys = this . property . getIds ( ) ; const entriesIterator = function * ( this ) : Generator < [ any , any ] > { for ( const key of keys ) { const property_to_proxy = this . property . get ( key ) ; if ( property_to_proxy ) { const proxy = PropertyProxy . proxify ( property_to_proxy ) ; yield [ proxy , proxy ] ; } else { throw new Error ( PropertyProxyErrors . INVALID_PROPERTY ) ; } } } ; return entriesIterator . call ( this ) ; } inheritdoc forEach ( func ) { const keys = this . property . getIds ( ) ; for ( const key of keys ) { const property_to_proxy = this . property . get ( key ) ; if ( property_to_proxy ) { const value = PropertyProxy . proxify ( property_to_proxy ) ; func ( value , value , this ) ; } else { throw new Error ( PropertyProxyErrors . INVALID_PROPERTY ) ; } } } inheritdoc has ( value ) { const guid = this . _getGuid ( value ) ; return this . property . has ( guid ) ; } inheritdoc values ( ) { return createSetIterator ( this ) ( ) ; } /**
     * Obtains the guid from a  external that is
     * part of a  external .
     *  param value return The guid of the passed  external .
      _getGuid ( value ) { let guid ; if ( "guid" in value ) { guid = value . guid ; } if ( guid === undefined && forceType < NamedProperty > ( value ) ) { guid = value . getId ( ) ; } if ( guid === undefined ) { throw new Error ( PropertyProxyErrors . INVALID_GUID ) ; } return guid ; } /**
     * Removes the entry with the passed guid from the wrapped  external .
     *  param guid _deleteById ( guid ) { this . property . remove ( guid ) ; } inheritdoc toJSON ( ) { return { } ; } } export { ComponentSet } ;