/**
 * @license
 * Copyright 2022-2024 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
function DatasourceStore(endpointStore, behaviorId) {
  return {
    initialValues: endpointStore.initialValues[behaviorId],
    async set(transaction, values) {
      const participant = participantFor(transaction, endpointStore);
      if (!participant.mutations) {
        participant.mutations = {};
      }
      const behaviorMutations = participant.mutations[behaviorId];
      if (behaviorMutations) {
        Object.assign(behaviorMutations, values);
      } else {
        participant.mutations[behaviorId] = { ...values };
      }
    }
  };
}
function participantFor(transaction, endpointStore) {
  let participant = transaction.getParticipant(endpointStore);
  if (participant) {
    return participant;
  }
  participant = {
    role: endpointStore,
    mutations: void 0,
    toString() {
      return endpointStore.toString();
    },
    commit1() {
    },
    async commit2() {
      if (this.mutations) {
        await endpointStore.set(this.mutations);
      }
    },
    rollback() {
      delete this.mutations;
    }
  };
  transaction.addParticipants(participant);
  return participant;
}
export {
  DatasourceStore
};
//# sourceMappingURL=DatasourceStore.js.map
