text: /*
  SPDX-License-Identifier: Apache-2.0
  */
  
  'use strict';
  const State = require('./state.js');
  
  /**
   * StateList provides a named virtual container for a set of ledger states.
   * Each state has a unique key which associates it with the container, rather
   * than the container containing a link to the state. This minimizes collisions
   * for parallel transactions on different states.
   */
  class StateList {
  
      /**
       * Store Fabric context for subsequent API access, and name of list
       */
      constructor(ctx, listName) {
          this.ctx = ctx;
          this.name = listName;
          this.id = 0;
          this.supportedClasses = {};
  
      }
  
      /**
       * Add a state to the list. Creates a new state in worldstate with
       * appropriate composite key.  Note that state defines its own key.
       * State object is serialized before writing.
       */
      async addState(state) {
          let key = this.name + state.getKey();
          console.log('This composite key: ' + key);
          let data = State.serialize(state);
          await this.ctx.stub.putState(key, data);
          return key;
      }
  
      /**
       * Get a state from the list using supplied keys. Form composite
       * keys to retrieve state from world state. State data is deserialized
       * into JSON object before being returned.
       */
      async getState(key) {
          let ledgerKey = this.name + state.getKey();
          let data = await this.ctx.stub.getState(ledgerKey);
          let state = State.deserialize(data, this.supportedClasses);
          return state;
      }
  
      /**
       * Update a state in the list. Puts the new state in world state with
       * appropriate composite key.  Note that state defines its own key.
       * A state is serialized before writing. Logic is very similar to
       * addState() but kept separate becuase it is semantically distinct.
       */
      async updateState(state) {
          let key = this.name + state.getKey();
          let data = State.serialize(state);
          await this.ctx.stub.putState(key, data);
      }
  
      /** Stores the class for future deserialization */
      use(stateClass) {
          this.supportedClasses[stateClass.getClass()] = stateClass;
      }
  
  }
  
  module.exports = StateList;