"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 ChangesResource_exports = {};
__export(ChangesResource_exports, {
  ChangesResource: () => ChangesResource
});
module.exports = __toCommonJS(ChangesResource_exports);
var import_RootSupervisor = require("#behavior/supervision/RootSupervisor.js");
var import_StateStream = require("#node/integration/StateStream.js");
var import_general = require("@matter/general");
var import_ApiResource = require("../ApiResource.js");
var import_Envelope = require("../Envelope.js");
/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
class ChangesResource extends import_ApiResource.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 import_Envelope.Envelope({ supervisor: import_RootSupervisor.RootSupervisor.for(import_StateStream.StateStream.OptionsSchema), ...request });
    let options;
    if (requestEnv.js) {
      requestEnv.validate();
      options = requestEnv.js;
    }
    const stream = (0, import_StateStream.StateStream)(this.parent.node, { ...options, abort });
    const { id } = request;
    for await (const change of stream) {
      const wire = import_StateStream.StateStream.WireChange(change);
      switch (change.kind) {
        case "update":
          yield new import_Envelope.Envelope({
            supervisor: import_RootSupervisor.RootSupervisor.for(import_StateStream.StateStream.WireUpdateSchema),
            js: { id, ...wire }
          });
          break;
        case "delete":
          yield new import_Envelope.Envelope({
            supervisor: import_RootSupervisor.RootSupervisor.for(import_StateStream.StateStream.WireDeleteSchema),
            js: { id, ...wire }
          });
          break;
        default:
          throw new import_general.InternalError(`Unsupported change kind ${change.kind}`);
      }
    }
  }
}
//# sourceMappingURL=ChangesResource.js.map
