"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 ServerBehaviorBacking_exports = {};
__export(ServerBehaviorBacking_exports, {
  ServerBehaviorBacking: () => ServerBehaviorBacking
});
module.exports = __toCommonJS(ServerBehaviorBacking_exports);
var import_general = require("#general");
var import_model = require("#model");
var import_NodeStore = require("#node/storage/NodeStore.js");
var import_BehaviorBacking = require("./BehaviorBacking.js");
/**
 * @license
 * Copyright 2022-2024 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
class ServerBehaviorBacking extends import_BehaviorBacking.BehaviorBacking {
  #store;
  #eventHandler;
  get store() {
    if (!this.#store) {
      this.#store = this.#serverStore.endpointStores.storeForPart(this.endpoint).storeForBehavior(this.type.id);
    }
    return this.#store;
  }
  get eventHandler() {
    if (!this.#eventHandler) {
      this.#eventHandler = this.#serverStore.eventHandler;
    }
    return this.#eventHandler;
  }
  invokeInitializer(behavior, options) {
    const finalizeState = () => {
      this.#applyTransitiveDefaults(behavior.state);
      const context = behavior.context;
      this.datasource.validate(context, behavior.state);
    };
    const promise = super.invokeInitializer(behavior, options);
    if (promise) {
      return promise.then(finalizeState);
    }
    finalizeState();
  }
  get #serverStore() {
    return this.endpoint.env.get(import_NodeStore.NodeStore);
  }
  /**
   * Schema may specify that state fields default to the value of another field.  We apply these defaults after
   * initialization when the other field should be defined.
   */
  #applyTransitiveDefaults(state) {
    const schema = this.type.schema;
    if (!schema) {
      return;
    }
    for (const member of schema.activeMembers) {
      const name = (0, import_general.camelize)(member.name);
      if (state[name] === void 0) {
        const referenced = import_model.FieldValue.referenced(member.default);
        if (referenced) {
          const val = state[(0, import_general.camelize)(referenced)];
          if (val !== void 0) {
            state[name] = val;
          }
        }
      }
    }
  }
}
//# sourceMappingURL=ServerBehaviorBacking.js.map
