/**
 * @license
 * Copyright 2022-2024 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
import { camelize } from "#general";
import { Access, FieldModel } from "#model";
import { RootSupervisor } from "./RootSupervisor.js";
import { Schema } from "./Schema.js";
function BehaviorSupervisor(options) {
  const logical = options.schema ?? Schema.empty;
  const schema = logical.extend({ name: `${camelize(options.id, true)}$State` });
  addExtensionFields(schema, new options.State());
  return new RootSupervisor(schema);
}
function addExtensionFields(schema, defaultState) {
  const props = /* @__PURE__ */ new Set();
  for (const field of schema.activeMembers) {
    props.add(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 FieldModel({
          name,
          type: "any",
          access: new Access({
            readPriv: Access.Privilege.View,
            writePriv: 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);
}
export {
  BehaviorSupervisor
};
//# sourceMappingURL=BehaviorSupervisor.js.map
