/**
 * @license
 * Copyright 2022-2025 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
import { GeneralDiagnosticsBehavior } from "#behaviors/general-diagnostics";
import { ScenesManagementServer } from "#behaviors/scenes-management";
import { GeneralDiagnostics } from "#clusters/general-diagnostics";
import { OnOff } from "#clusters/on-off";
import { Millis, Time } from "#general";
import { ServerNode } from "#node/ServerNode.js";
import { hasRemoteActor } from "#protocol";
import { OnOffBehavior } from "./OnOffBehavior.js";
const OnOffLogicBase = OnOffBehavior.with(OnOff.Feature.Lighting);
class OnOffBaseServer extends OnOffLogicBase {
  initialize() {
    if (this.features.lighting && this.#getBootReason() !== GeneralDiagnostics.BootReason.SoftwareUpdateCompleted) {
      const startUpOnOffValue = this.state.startUpOnOff ?? null;
      const currentOnOffStatus = this.state.onOff;
      if (startUpOnOffValue !== null) {
        const targetOnOffValue = startUpOnOffValue === OnOff.StartUpOnOff.Toggle ? !currentOnOffStatus : startUpOnOffValue === OnOff.StartUpOnOff.On;
        if (targetOnOffValue !== currentOnOffStatus) {
          this.state.onOff = targetOnOffValue;
        }
      }
    }
    if (this.agent.has(ScenesManagementServer)) {
      this.agent.get(ScenesManagementServer).implementScenes(this, this.#applySceneValues);
      this.reactTo(this.events.onOff$Changed, this.#clearDelayedSceneApplyData);
    }
  }
  async [Symbol.asyncDispose]() {
    this.internal.timedOnTimer?.stop();
    this.internal.delayedOffTimer?.stop();
    await super[Symbol.asyncDispose]?.();
  }
  on() {
    if (!this.state.onOff) {
      this.#invalidateScenes();
    }
    this.state.onOff = true;
    if (this.features.lighting) {
      this.state.globalSceneControl = true;
      if (!this.timedOnTimer.isRunning) {
        if (this.delayedOffTimer.isRunning) {
          this.delayedOffTimer.stop();
        }
        this.state.offWaitTime = 0;
      }
    }
  }
  /** Invalidate all stored scenes manually for this endpoint in the Scenesmanagement cluster because SDK behavior. */
  #invalidateScenes() {
    this.agent.maybeGet(ScenesManagementServer)?.makeAllFabricSceneInfoEntriesInvalid();
  }
  off() {
    if (this.state.onOff) {
      this.#invalidateScenes();
    }
    this.state.onOff = false;
    if (this.features.lighting) {
      if (this.timedOnTimer.isRunning) {
        this.timedOnTimer.stop();
        if ((this.state.offWaitTime ?? 0) > 0) {
          this.delayedOffTimer.start();
        }
      }
      this.state.onTime = 0;
    }
  }
  /**
   * Default implementation notes:
   * This method uses the on/off methods when timed actions should occur. This means that it is enough to override
   * on() and off() with custom control logic.
   */
  toggle() {
    if (this.state.onOff) {
      return this.off();
    } else {
      return this.on();
    }
  }
  /**
   * Default implementation notes:
   * * This implementation ignores the effect and just calls off().
   */
  offWithEffect() {
    if (this.state.globalSceneControl) {
      if (hasRemoteActor(this.context) && this.context.session.fabric !== void 0) {
        this.endpoint.agentFor(this.context).maybeGet(ScenesManagementServer)?.storeGlobalScene(this.context.session.fabric.fabricIndex);
      }
      this.state.globalSceneControl = false;
    }
    return this.off();
  }
  async onWithRecallGlobalScene() {
    if (this.state.globalSceneControl) {
      return;
    }
    if (hasRemoteActor(this.context) && this.context.session.fabric !== void 0 && this.agent.has(ScenesManagementServer)) {
      await this.endpoint.agentFor(this.context).maybeGet(ScenesManagementServer)?.recallGlobalScene(this.context.session.fabric.fabricIndex);
    }
    this.state.globalSceneControl = true;
    if (this.state.onTime === 0) {
      this.state.offWaitTime = 0;
    }
    await this.on();
  }
  /**
   * Default implementation notes:
   * * This method uses the on/off methods when timed actions should occur. This means that it is enough to override
   * on() and off() with custom control logic.
   */
  onWithTimedOff({ onOffControl, offWaitTime, onTime }) {
    if (onOffControl.acceptOnlyWhenOn && !this.state.onOff) {
      return;
    }
    if (this.delayedOffTimer.isRunning && !this.state.onOff) {
      this.state.offWaitTime = Math.min(offWaitTime ?? 0, this.state.offWaitTime ?? 0);
      return;
    }
    this.state.onTime = Math.max(onTime ?? 0, this.state.onTime ?? 0);
    this.state.offWaitTime = offWaitTime;
    if (this.state.onTime !== 0 && this.state.offWaitTime !== 0) {
      this.timedOnTimer.start();
    }
    return this.on();
  }
  get timedOnTimer() {
    let timer = this.internal.timedOnTimer;
    if (timer === void 0) {
      timer = this.internal.timedOnTimer = Time.getPeriodicTimer(
        "Timed on",
        Millis(100),
        this.callback(this.#timedOnTick, { lock: true })
      );
    }
    return timer;
  }
  async #timedOnTick() {
    let time = (this.state.onTime ?? 0) - 1;
    if (time <= 0) {
      time = 0;
      this.internal.timedOnTimer?.stop();
      this.state.offWaitTime = 0;
      await this.off();
    }
    this.state.onTime = time;
  }
  get delayedOffTimer() {
    let timer = this.internal.delayedOffTimer;
    if (timer === void 0) {
      timer = this.internal.delayedOffTimer = Time.getPeriodicTimer(
        "Delayed off",
        Millis(100),
        this.callback(this.#delayedOffTick, { lock: true })
      );
    }
    return timer;
  }
  /** Apply Scene values when requested from ScenesManagement cluster */
  #applySceneValues(values, transitionTime) {
    this.#clearDelayedSceneApplyData();
    const onOff = values.onOff;
    if (typeof onOff !== "boolean" || this.state.onOff === onOff) {
      return;
    }
    if (transitionTime === 0) {
      this.state.onOff = onOff;
    } else {
      this.internal.applyScenePendingOnOff = onOff;
      this.internal.applySceneDelayTimer = Time.getPeriodicTimer(
        "delayed scene apply",
        Millis(transitionTime),
        this.callback(this.#applyDelayedSceneOnOffValue)
      ).start();
    }
  }
  #clearDelayedSceneApplyData() {
    if (this.internal.applySceneDelayTimer?.isRunning) {
      this.internal.applySceneDelayTimer.stop();
    }
    this.internal.applySceneDelayTimer = void 0;
    this.internal.applyScenePendingOnOff = void 0;
  }
  #applyDelayedSceneOnOffValue() {
    const onOff = this.internal.applyScenePendingOnOff;
    this.#clearDelayedSceneApplyData();
    if (onOff === void 0) {
      return;
    }
    this.state.onOff = onOff;
  }
  #delayedOffTick() {
    let time = (this.state.offWaitTime ?? 0) - 1;
    if (time <= 0) {
      time = 0;
      this.internal.delayedOffTimer?.stop();
    }
    this.state.offWaitTime = time;
  }
  #getBootReason() {
    const rootEndpoint = this.env.get(ServerNode);
    if (rootEndpoint.behaviors.has(GeneralDiagnosticsBehavior)) {
      return rootEndpoint.stateOf(GeneralDiagnosticsBehavior).bootReason;
    }
  }
}
((OnOffBaseServer2) => {
  class Internal {
    timedOnTimer;
    delayedOffTimer;
    applySceneDelayTimer;
    applyScenePendingOnOff;
  }
  OnOffBaseServer2.Internal = Internal;
})(OnOffBaseServer || (OnOffBaseServer = {}));
class OnOffServer extends OnOffBaseServer.with() {
}
export {
  OnOffBaseServer,
  OnOffServer
};
//# sourceMappingURL=OnOffServer.js.map
