"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_model = require("@matter/model");
var import_RootSupervisor = require("./RootSupervisor.js");
/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
function BehaviorSupervisor(options) {
  const logical = options.schema ?? import_model.Schema.empty;
  const schema = addExtensionFields(logical, new options.State());
  return import_RootSupervisor.RootSupervisor.for(schema);
}
const extendedSchemaCache = /* @__PURE__ */ new Map();
function addExtensionFields(schema, defaultState) {
  const props = /* @__PURE__ */ new Set();
  const scope = (0, import_model.Scope)(schema, { forceOwner: true });
  for (const field of scope.membersOf(schema, { conformance: "deconflicted" })) {
    props.add(field.propertyName);
  }
  let newProperties;
  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 descriptor = descriptors[name];
        const fixed = !descriptor.writable && !descriptor.set;
        if (newProperties === void 0) {
          newProperties = {};
        }
        newProperties[name] = fixed;
      }
    }
    addProperties(Object.getPrototypeOf(object));
  }
  addProperties(defaultState);
  if (newProperties === void 0) {
    return schema;
  }
  const cacheKey = Object.entries(newProperties).map(([name, fixed]) => `${name}/${fixed}`).join(",");
  let cacheBucket = extendedSchemaCache.get(schema);
  if (cacheBucket === void 0) {
    extendedSchemaCache.set(schema, cacheBucket = {});
  } else if (cacheKey in cacheBucket) {
    return cacheBucket[cacheKey];
  }
  schema = schema.extend();
  for (const name in newProperties) {
    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
      })
    });
    if (newProperties[name]) {
      field.quality = "F";
    }
    schema.children.push(field);
  }
  cacheBucket[cacheKey] = schema;
  return schema;
}
//# sourceMappingURL=BehaviorSupervisor.js.map
