/**
 * @license
 * Copyright 2022-2024 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
import { Identify } from "#clusters/identify";
import { Observable, Time } from "#general";
import { IdentifyBehavior } from "./IdentifyBehavior.js";
class IdentifyServer extends IdentifyBehavior {
  initialize() {
    if (this.state.identifyType === void 0) {
      this.state.identifyType = Identify.IdentifyType.None;
    }
    this.internal.identifyTimer = Time.getPeriodicTimer(
      "Identify time update",
      1e3,
      this.callback(this.#identifyTick)
    );
    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;
  }
}
((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();
  }
  IdentifyServer2.Events = Events;
})(IdentifyServer || (IdentifyServer = {}));
export {
  IdentifyServer
};
//# sourceMappingURL=IdentifyServer.js.map
