"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 BehaviorSupervisor_exports = {};
__export(BehaviorSupervisor_exports, {
  BehaviorSupervisor: () => BehaviorSupervisor
});
module.exports = __toCommonJS(BehaviorSupervisor_exports);
var import_general = require("#general");
var import_model = require("#model");
var import_RootSupervisor = require("./RootSupervisor.js");
var import_Schema = require("./Schema.js");
/**
 * @license
 * Copyright 2022-2024 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
function BehaviorSupervisor(options) {
  const logical = options.schema ?? import_Schema.Schema.empty;
  const schema = logical.extend({ name: `${(0, import_general.camelize)(options.id, true)}$State` });
  addExtensionFields(schema, new options.State());
  return new import_RootSupervisor.RootSupervisor(schema);
}
function addExtensionFields(schema, defaultState) {
  const props = /* @__PURE__ */ new Set();
  for (const field of schema.activeMembers) {
    props.add((0, import_general.camelize)(field.name));
  }
  function addProperties(object) {
    if (!object || object === Object.prototype) {
      return;
    }
    const descriptors = Object.getOwnPropertyDescriptors(object);
    for (const name in descriptors) {
      if (props.has(name) || name === "constructor") {
        continue;
      }
      if (!props.has(name)) {
        props.add(name);
        const field = new import_model.FieldModel({
          name,
          type: "any",
          access: new import_model.Access({
            readPriv: import_model.Access.Privilege.View,
            writePriv: import_model.Access.Privilege.Operate
          })
        });
        const descriptor = descriptors[name];
        if (!descriptor.writable && !descriptor.set) {
          field.quality = "F";
        }
        schema.children.push(field);
      }
    }
    addProperties(Object.getPrototypeOf(object));
  }
  addProperties(defaultState);
}
//# sourceMappingURL=BehaviorSupervisor.js.map
