"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var DatasourceStore_exports = {};
__export(DatasourceStore_exports, {
  DatasourceStore: () => DatasourceStore
});
module.exports = __toCommonJS(DatasourceStore_exports);
/**
 * @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;
}
//# sourceMappingURL=DatasourceStore.js.map
