/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
import { Observable, Seconds, Time } from "@matter/general";
import { hasRemoteActor } from "@matter/protocol";
import { Identify } from "@matter/types/clusters/identify";
import { IdentifyBehavior } from "./IdentifyBehavior.js";
class IdentifyServer extends IdentifyBehavior {
  initialize() {
    if (this.state.identifyType === void 0) {
      this.state.identifyType = Identify.IdentifyType.None;
    }
    this.events.identifyTime$Changed.quiet.config = {
      shouldEmit: (newValue, oldValue, context) => hasRemoteActor(context) || (oldValue === 0 || newValue === 0) && newValue !== oldValue ? "now" : false,
      suppressionEnabled: false
    };
    this.internal.identifyTimer = Time.getPeriodicTimer(
      "Identify time update",
      Seconds.one,
      this.callback(this.#identifyTick, { lock: true })
    );
    this.reactTo(this.events.identifyTime$Changed, this.#identifyTimeChangedHandler);
  }
  #startIdentifying() {
    if (!this.internal.identifyTimer?.isRunning) {
      this.internal.identifyTimer?.start();
      this.state.isIdentifying = true;
      this.events.startIdentifying.emit();
    }
  }
  #stopIdentifying() {
    if (this.internal.identifyTimer?.isRunning) {
      this.internal.identifyTimer?.stop();
      this.state.isIdentifying = false;
      this.events.stopIdentifying.emit();
    }
  }
  #identifyTimeChangedHandler() {
    if (this.state.identifyTime === 0) {
      this.#stopIdentifying();
    } else {
      this.#startIdentifying();
    }
  }
  async [Symbol.asyncDispose]() {
    this.#stopIdentifying();
    await super[Symbol.asyncDispose]?.();
  }
  #identifyTick() {
    let time = (this.state.identifyTime ?? 0) - 1;
    if (time <= 0) {
      time = 0;
    }
    this.state.identifyTime = time;
  }
  identify({ identifyTime }) {
    this.state.identifyTime = identifyTime;
  }
  triggerEffect(effect) {
    this.events.effectTriggered.emit(effect);
  }
}
((IdentifyServer2) => {
  class Internal {
    identifyTimer;
  }
  IdentifyServer2.Internal = Internal;
  class State extends IdentifyBehavior.State {
    isIdentifying = false;
  }
  IdentifyServer2.State = State;
  class Events extends IdentifyBehavior.Events {
    startIdentifying = Observable();
    stopIdentifying = Observable();
    effectTriggered = Observable();
  }
  IdentifyServer2.Events = Events;
})(IdentifyServer || (IdentifyServer = {}));
export {
  IdentifyServer
};
//# sourceMappingURL=IdentifyServer.js.map
