/**
 * @license
 * Copyright 2022-2025 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
import { RootSupervisor } from "#behavior/supervision/RootSupervisor.js";
import { InternalError } from "#general";
import { StateStream } from "#node/integration/StateStream.js";
import { ApiResource } from "../ApiResource.js";
import { Envelope } from "../Envelope.js";
class ChangesResource extends ApiResource {
  id = "changes";
  valueKind = "changes";
  supervisor = void 0;
  value = void 0;
  isSubscribable = true;
  constructor(parent) {
    super(parent);
  }
  get dataModelPath() {
    return this.parent.dataModelPath.at("changes");
  }
  async *subscribe(abort, request) {
    const requestEnv = new Envelope({ supervisor: RootSupervisor.for(StateStream.OptionsSchema), ...request });
    let options;
    if (requestEnv.js) {
      requestEnv.validate();
      options = requestEnv.js;
    }
    const stream = StateStream(this.parent.node, { ...options, abort });
    const { id } = request;
    for await (const change of stream) {
      const wire = StateStream.WireChange(change);
      switch (change.kind) {
        case "update":
          yield new Envelope({
            supervisor: RootSupervisor.for(StateStream.WireUpdateSchema),
            js: { id, ...wire }
          });
          break;
        case "delete":
          yield new Envelope({
            supervisor: RootSupervisor.for(StateStream.WireDeleteSchema),
            js: { id, ...wire }
          });
          break;
        default:
          throw new InternalError(`Unsupported change kind ${change.kind}`);
      }
    }
  }
}
export {
  ChangesResource
};
//# sourceMappingURL=ChangesResource.js.map
