/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
import { deepCopy, StorageManager } from "@matter/general";
import { DatatypeModel, FieldElement } from "@matter/model";
import { NonvolatileEventStore, OccurrenceManager, VolatileEventStore } from "@matter/protocol";
import { Behavior } from "../../Behavior.js";
class EventsBehavior extends Behavior {
  static id = "events";
  static early = true;
  async initialize() {
    const storage = this.env.get(StorageManager).createContext("events");
    let store;
    if (this.state.nonvolatile) {
      store = new NonvolatileEventStore(storage);
    } else {
      store = new VolatileEventStore(storage, this.state.numberBlockSize);
    }
    const events = new OccurrenceManager({ store, bufferConfig: this.state.buffers });
    this.env.set(OccurrenceManager, events);
    await events.construction;
  }
  static schema = new DatatypeModel(
    {
      name: "EventsState",
      type: "struct"
    },
    FieldElement({ name: "nonvolatile", type: "bool" }),
    FieldElement({ name: "numberBlockSize", type: "uint16" }),
    FieldElement(
      { name: "buffer", type: "struct" },
      FieldElement({ name: "minEventAllowance", type: "uint32" }),
      FieldElement({ name: "maxEventAllowance", type: "uint32" }),
      FieldElement(
        { name: "minPriorityEventAllowance", type: "struct" },
        FieldElement({ name: "critical", type: "uint32" }),
        FieldElement({ name: "info", type: "uint32" }),
        FieldElement({ name: "debug", type: "uint32" })
      )
    )
  );
}
((EventsBehavior2) => {
  class State {
    nonvolatile = false;
    numberBlockSize = 1e3;
    buffers = deepCopy(OccurrenceManager.DefaultBufferConfig);
  }
  EventsBehavior2.State = State;
})(EventsBehavior || (EventsBehavior = {}));
export {
  EventsBehavior
};
//# sourceMappingURL=EventsBehavior.js.map
