/**
 * @license
 * Copyright 2022-2025 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
import { ModeUtils } from "#behaviors/mode-base";
import { ModeBase } from "#clusters/mode-base";
import { RvcRunMode } from "#clusters/rvc-run-mode";
import { ImplementationError } from "#general";
import { RvcRunModeBehavior } from "./RvcRunModeBehavior.js";
class RvcRunModeServer extends RvcRunModeBehavior {
  initialize() {
    this.#assertSupportedModes();
    ModeUtils.assertMode(this.state.supportedModes, this.state.currentMode);
    this.reactTo(this.events.currentMode$Changing, this.#assertMode);
  }
  #assertSupportedModes() {
    ModeUtils.assertSupportedModes(this.state.supportedModes);
    if (!this.state.supportedModes.some(
      ({ modeTags }) => modeTags.some(({ value }) => value === RvcRunMode.ModeTag.Idle)
    )) {
      throw new ImplementationError("Provided supportedModes need to include at least one Idle mode tag");
    }
    if (!this.state.supportedModes.some(
      ({ modeTags }) => modeTags.some(({ value }) => value === RvcRunMode.ModeTag.Cleaning)
    )) {
      throw new ImplementationError("Provided supportedModes need to include at least one Cleaning mode tag");
    }
    if (this.state.supportedModes.some(({ modeTags }) => {
      let exclusiveModeCounter = 0;
      exclusiveModeCounter += modeTags.some(({ value }) => value === RvcRunMode.ModeTag.Idle) ? 1 : 0;
      exclusiveModeCounter += modeTags.some(({ value }) => value === RvcRunMode.ModeTag.Cleaning) ? 1 : 0;
      exclusiveModeCounter += modeTags.some(({ value }) => value === RvcRunMode.ModeTag.Mapping) ? 1 : 0;
      return exclusiveModeCounter > 1;
    })) {
      throw new ImplementationError(
        "Provided supportedModes must not have Idle, Cleaning and Mapping mode tags together in one mode"
      );
    }
  }
  #assertMode(newMode) {
    ModeUtils.assertMode(this.state.supportedModes, newMode);
  }
  /**
   * This command is used to change device modes.
   * On receipt of this command the device shall respond with a ChangeToModeResponse command.
   *
   * The default implementation automatically validates the new mode against the supported modes and returns an error
   * if the new mode is not supported. If the new mode is supported, the current mode is updated to the new mode.
   *
   * If you need to override this with extra validation logic you can use
   * `ModeUtils.assertModeChange(this.state.supportedModes, this.state.currentMode, newMode)`
   * to just execute the validation and add your own validation requirements before or after these standard checks.
   * The above method returns a `ModeBase.ChangeToModeResponse` object that you can use to return the result of the
   * validation.
   */
  changeToMode({ newMode }) {
    const result = ModeUtils.assertModeChange(this.state.supportedModes, this.state.currentMode, newMode);
    if (result.status === ModeBase.ModeChangeStatus.Success) {
      this.state.currentMode = newMode;
    }
    return result;
  }
}
export {
  RvcRunModeServer
};
//# sourceMappingURL=RvcRunModeServer.js.map
