"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 IdentifyServer_exports = {};
__export(IdentifyServer_exports, {
  IdentifyServer: () => IdentifyServer
});
module.exports = __toCommonJS(IdentifyServer_exports);
var import_identify = require("#clusters/identify");
var import_general = require("#general");
var import_protocol = require("@matter/protocol");
var import_IdentifyBehavior = require("./IdentifyBehavior.js");
/**
 * @license
 * Copyright 2022-2025 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
class IdentifyServer extends import_IdentifyBehavior.IdentifyBehavior {
  initialize() {
    if (this.state.identifyType === void 0) {
      this.state.identifyType = import_identify.Identify.IdentifyType.None;
    }
    this.events.identifyTime$Changed.quiet.config = {
      shouldEmit: (newValue, oldValue, context) => (0, import_protocol.hasRemoteActor)(context) || (oldValue === 0 || newValue === 0) && newValue !== oldValue ? "now" : false,
      suppressionEnabled: false
    };
    this.internal.identifyTimer = import_general.Time.getPeriodicTimer(
      "Identify time update",
      import_general.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 import_IdentifyBehavior.IdentifyBehavior.State {
    isIdentifying = false;
  }
  IdentifyServer2.State = State;
  class Events extends import_IdentifyBehavior.IdentifyBehavior.Events {
    startIdentifying = (0, import_general.Observable)();
    stopIdentifying = (0, import_general.Observable)();
    effectTriggered = (0, import_general.Observable)();
  }
  IdentifyServer2.Events = Events;
})(IdentifyServer || (IdentifyServer = {}));
//# sourceMappingURL=IdentifyServer.js.map
