"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 ModeSelectServer_exports = {};
__export(ModeSelectServer_exports, {
  ModeSelectBaseServer: () => ModeSelectBaseServer,
  ModeSelectServer: () => ModeSelectServer
});
module.exports = __toCommonJS(ModeSelectServer_exports);
var import_general_diagnostics = require("#behaviors/general-diagnostics");
var import_on_off = require("#behaviors/on-off");
var import_ServerNode = require("#node/ServerNode.js");
var import_general = require("@matter/general");
var import_types = require("@matter/types");
var import_general_diagnostics2 = require("@matter/types/clusters/general-diagnostics");
var import_mode_select = require("@matter/types/clusters/mode-select");
var import_on_off2 = require("@matter/types/clusters/on-off");
var import_ModeSelectBehavior = require("./ModeSelectBehavior.js");
/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
const logger = import_general.Logger.get("ModeSelectServer");
const ModeSelectBase = import_ModeSelectBehavior.ModeSelectBehavior.with(import_mode_select.ModeSelect.Feature.OnOff);
class ModeSelectBaseServer extends ModeSelectBase {
  initialize() {
    this.#assertCurrentMode();
    this.#assertStartUpMode();
    this.#assertOnMode();
    let currentModeOverridden = false;
    if (this.features.onOff && this.state.onMode !== void 0 && this.state.onMode !== null) {
      const onOffServer = this.agent.get(import_on_off.OnOffServer);
      if (onOffServer !== void 0) {
        if (onOffServer.features.lighting && "startUpOnOff" in onOffServer.state && onOffServer.state.startUpOnOff === import_on_off2.OnOff.StartUpOnOff.On) {
          this.state.currentMode = this.state.onMode;
          currentModeOverridden = true;
        }
        this.reactTo(onOffServer.events.onOff$Changed, this.#handleOnOffDependency);
      } else {
        logger.warn("OnOffServer not found on endpoint, but OnMode is set.");
      }
    }
    if (!currentModeOverridden && this.state.startUpMode !== void 0 && this.state.startUpMode !== null && this.#getBootReason() !== import_general_diagnostics2.GeneralDiagnostics.BootReason.SoftwareUpdateCompleted) {
      this.state.currentMode = this.state.startUpMode;
    }
  }
  changeToMode({ newMode }) {
    this.#assertModeValue("NewMode", newMode);
    this.state.currentMode = newMode;
  }
  #assertCurrentMode() {
    this.#assertModeValue("currentMode", this.state.currentMode);
  }
  #assertStartUpMode() {
    if (this.state.startUpMode !== void 0 && this.state.startUpMode !== null) {
      this.#assertModeValue("startUpMode", this.state.startUpMode);
    }
  }
  #assertOnMode() {
    if (this.state.onMode !== void 0 && this.state.onMode !== null) {
      this.#assertModeValue("onMode", this.state.onMode);
    }
  }
  #assertModeValue(fieldName, mode) {
    if (!this.state.supportedModes.some(({ mode: supportedMode }) => supportedMode === mode)) {
      throw new import_types.StatusResponseError(
        `Mode ${mode} provided in ${fieldName} is not supported`,
        import_types.StatusCode.InvalidCommand
      );
    }
  }
  #handleOnOffDependency(newValue, oldValue) {
    if (newValue && !oldValue && this.state.onMode !== void 0 && this.state.onMode !== null) {
      this.state.currentMode = this.state.onMode;
    }
  }
  #getBootReason() {
    const rootEndpoint = this.env.get(import_ServerNode.ServerNode);
    if (rootEndpoint.behaviors.has(import_general_diagnostics.GeneralDiagnosticsBehavior)) {
      return rootEndpoint.stateOf(import_general_diagnostics.GeneralDiagnosticsBehavior).bootReason;
    }
  }
}
class ModeSelectServer extends ModeSelectBaseServer.for((0, import_types.ClusterType)(import_mode_select.ModeSelect.Base)) {
}
//# sourceMappingURL=ModeSelectServer.js.map
