"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 RvcOperationalStateServer_exports = {};
__export(RvcOperationalStateServer_exports, {
  RvcOperationalStateServer: () => RvcOperationalStateServer
});
module.exports = __toCommonJS(RvcOperationalStateServer_exports);
var import_general = require("@matter/general");
var import_operational_state = require("@matter/types/clusters/operational-state");
var import_rvc_operational_state = require("@matter/types/clusters/rvc-operational-state");
var import_RvcOperationalStateBehavior = require("./RvcOperationalStateBehavior.js");
/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
class RvcOperationalStateServer extends import_RvcOperationalStateBehavior.RvcOperationalStateBehavior {
  initialize() {
    this.#assertOperationalStateList();
    this.#syncCurrentPhaseWithPhaseList();
    this.reactTo(this.events.operationalState$Changing, this.#assertOperationalState);
    this.reactTo(this.events.phaseList$Changed, this.#syncCurrentPhaseWithPhaseList);
    this.reactTo(this.events.currentPhase$Changing, this.#assertCurrentPhase);
    this.reactTo(this.events.operationalError$Changed, this.#handleOperationalError);
  }
  #assertOperationalStateList() {
    if (!this.state.operationalStateList.some(
      ({ operationalStateId }) => operationalStateId === import_operational_state.OperationalState.OperationalStateEnum.Error
    )) {
      throw new import_general.ImplementationError(`Operational state list must at least contain an error entry.`);
    }
  }
  #syncCurrentPhaseWithPhaseList() {
    if (this.state.phaseList === null || this.state.phaseList.length === 0) {
      this.state.currentPhase = null;
    }
  }
  #assertCurrentPhase(currentPhase) {
    if (this.state.phaseList === null || this.state.phaseList.length === 0) {
      if (currentPhase === null) {
        return;
      }
      throw new import_general.ImplementationError(
        "Cannot set current phase to an other value than null when phase list is empty"
      );
    }
    if (currentPhase === null || currentPhase < 0 || currentPhase >= this.state.phaseList.length) {
      throw new import_general.ImplementationError(
        `Current phase ${currentPhase} is out of bounds for phase list of length ${this.state.phaseList.length}`
      );
    }
  }
  #assertOperationalState(newState, oldState) {
    if (!this.state.operationalStateList.some(({ operationalStateId }) => operationalStateId === newState)) {
      throw new import_general.ImplementationError(
        `Cannot set operational state id ${newState} as it is not in the operational state list`
      );
    }
    if (oldState === import_operational_state.OperationalState.OperationalStateEnum.Error && this.state.operationalError.errorStateId !== import_operational_state.OperationalState.ErrorState.NoError && newState !== import_operational_state.OperationalState.OperationalStateEnum.Error) {
      this.state.operationalError = { errorStateId: import_operational_state.OperationalState.ErrorState.NoError };
    }
  }
  #handleOperationalError(operationalError) {
    if (operationalError.errorStateId === import_operational_state.OperationalState.ErrorState.NoError) {
      return;
    }
    if (this.state.operationalState !== import_rvc_operational_state.RvcOperationalState.OperationalState.Error) {
      this.state.operationalState = import_rvc_operational_state.RvcOperationalState.OperationalState.Error;
    }
    this.events.operationalError.emit({ errorState: operationalError }, this.context);
  }
}
//# sourceMappingURL=RvcOperationalStateServer.js.map
