/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
import { GeneralDiagnosticsBehavior } from "#behaviors/general-diagnostics";
import { OnOffServer } from "#behaviors/on-off";
import { ServerNode } from "#node/ServerNode.js";
import { Logger } from "@matter/general";
import { ClusterType, StatusCode, StatusResponseError } from "@matter/types";
import { GeneralDiagnostics } from "@matter/types/clusters/general-diagnostics";
import { ModeSelect } from "@matter/types/clusters/mode-select";
import { OnOff } from "@matter/types/clusters/on-off";
import { ModeSelectBehavior } from "./ModeSelectBehavior.js";
const logger = Logger.get("ModeSelectServer");
const ModeSelectBase = ModeSelectBehavior.with(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(OnOffServer);
      if (onOffServer !== void 0) {
        if (onOffServer.features.lighting && "startUpOnOff" in onOffServer.state && onOffServer.state.startUpOnOff === 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() !== 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 StatusResponseError(
        `Mode ${mode} provided in ${fieldName} is not supported`,
        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(ServerNode);
    if (rootEndpoint.behaviors.has(GeneralDiagnosticsBehavior)) {
      return rootEndpoint.stateOf(GeneralDiagnosticsBehavior).bootReason;
    }
  }
}
class ModeSelectServer extends ModeSelectBaseServer.for(ClusterType(ModeSelect.Base)) {
}
export {
  ModeSelectBaseServer,
  ModeSelectServer
};
//# sourceMappingURL=ModeSelectServer.js.map
