System.register(["__unresolved_0", "cc", "__unresolved_1", "__unresolved_2"], function (_export, _context) {
  "use strict";

  var _reporterNs, _cclegacy, LccList, LccSet, _crd, Timer, Clock;

  function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

  function _reportPossibleCrUseOfLccList(extras) {
    _reporterNs.report("LccList", "../MoreStruct/LccList", _context.meta, extras);
  }

  function _reportPossibleCrUseOfLccSet(extras) {
    _reporterNs.report("LccSet", "../MoreStruct/LccSet", _context.meta, extras);
  }

  function _reportPossibleCrUseOfActionFunVoid(extras) {
    _reporterNs.report("ActionFunVoid", "./BtConst", _context.meta, extras);
  }

  return {
    setters: [function (_unresolved_) {
      _reporterNs = _unresolved_;
    }, function (_cc) {
      _cclegacy = _cc.cclegacy;
    }, function (_unresolved_2) {
      LccList = _unresolved_2.LccList;
    }, function (_unresolved_3) {
      LccSet = _unresolved_3.LccSet;
    }],
    execute: function () {
      _crd = true;

      _cclegacy._RF.push({}, "e602c9TxSFPE4Lcl70E9zEH", "Clock", undefined);

      Timer = function Timer() {
        _defineProperty(this, "absoluteTime", 0);

        _defineProperty(this, "repeat", 0);

        _defineProperty(this, "used", false);
      };

      _export("Clock", Clock = /*#__PURE__*/function () {
        function Clock() {
          _defineProperty(this, "isInUpdate", false);

          _defineProperty(this, "elapsedTime", 0);

          _defineProperty(this, "timers", new Map());

          _defineProperty(this, "addTimers", new Map());

          _defineProperty(this, "removeTimers", new Map());

          _defineProperty(this, "timerPool", new (_crd && LccList === void 0 ? (_reportPossibleCrUseOfLccList({
            error: Error()
          }), LccList) : LccList)());

          _defineProperty(this, "currentTimerPoolIndex", 0);

          _defineProperty(this, "updateObservers", new (_crd && LccList === void 0 ? (_reportPossibleCrUseOfLccList({
            error: Error()
          }), LccList) : LccList)());

          _defineProperty(this, "removeObservers", new (_crd && LccSet === void 0 ? (_reportPossibleCrUseOfLccSet({
            error: Error()
          }), LccSet) : LccSet)());

          _defineProperty(this, "addObservers", new (_crd && LccSet === void 0 ? (_reportPossibleCrUseOfLccSet({
            error: Error()
          }), LccSet) : LccSet)());
        }

        var _proto = Clock.prototype;

        _proto.AddTimer = function AddTimer(time, randomVariance, repeat, action) {
          if (randomVariance === void 0) {
            randomVariance = 0;
          }

          time = time - randomVariance * 0.5 + randomVariance * Math.random();

          if (!this.isInUpdate) {
            if (this.timers.has(action)) {
              this.timers.get(action).absoluteTime = this.elapsedTime + time;
              this.timers.get(action).repeat = repeat;
            } else {
              this.timers.set(action, this.getTimerFromPool(this.elapsedTime + time, repeat));
            }
          } else {
            if (!this.addTimers.has(action)) {
              this.addTimers.set(action, this.getTimerFromPool(this.elapsedTime + time, repeat));
            } else {
              this.addTimers.get(action).repeat = repeat;
              this.addTimers.get(action).absoluteTime = this.elapsedTime + time;
            }

            if (this.removeTimers.has(action)) {
              this.removeTimers["delete"](action);
            }
          }
        };

        _proto.RemoveTimer = function RemoveTimer(action) {
          if (!this.isInUpdate) {
            if (this.timers.has(action)) {
              this.timers.get(action).used = false;
              this.timers["delete"](action);
            }
          } else {
            if (this.timers.has(action)) {
              this.removeTimers.set(action, null);
            }

            if (this.addTimers.has(action)) {
              this.addTimers.get(action).used = false;
              this.addTimers["delete"](action);
            }
          }
        };

        _proto.HasTimer = function HasTimer(action) {
          if (!this.isInUpdate) {
            return this.timers.has(action);
          } else {
            if (this.removeTimers.has(action)) {
              return false;
            } else if (this.addTimers.has(action)) {
              return true;
            } else {
              return this.timers.has(action);
            }
          }
        };

        _proto.getTimerFromPool = function getTimerFromPool(absoluteTime, repeat) {
          var i = 0;
          var l = this.timerPool.Count;
          var timer = null;

          while (i < l) {
            var timerIndex = (i + this.currentTimerPoolIndex) % l;

            if (!this.timerPool.Get(timerIndex).used) {
              this.currentTimerPoolIndex = timerIndex;
              timer = this.timerPool.Get(timerIndex);
              break;
            }

            i++;
          }

          if (timer == null) {
            timer = new Timer();
            this.currentTimerPoolIndex = 0;
            this.timerPool.Add(timer);
          }

          timer.used = true;
          timer.absoluteTime = absoluteTime;
          timer.repeat = repeat;
          return timer;
        };

        _proto.AddUpdateObserver = function AddUpdateObserver(action) {
          if (!this.isInUpdate) {
            this.updateObservers.Add(action);
          } else {
            if (!this.updateObservers.Contains(action)) {
              this.addObservers.add(action);
            }

            if (this.removeObservers.has(action)) {
              this.removeObservers.remove(action);
            }
          }
        };

        _proto.RemoveUpdateObserver = function RemoveUpdateObserver(action) {
          if (!this.isInUpdate) {
            // cc.log('this.updateObservers.Remove bef', this.updateObservers.Count);
            this.updateObservers.Remove(action); // cc.log('this.updateObservers.Remove aft', this.updateObservers.Count);
          } else {
            if (this.updateObservers.Contains(action)) {
              // cc.log('this.removeObservers.add bef', this.removeObservers.size())
              this.removeObservers.add(action); // cc.log('this.removeObservers.add aft', this.removeObservers.size())
            }

            if (this.addObservers.has(action)) {
              // cc.log(' this.addObservers.remove bef', this.addObservers.size())
              this.addObservers.remove(action); // cc.log(' this.addObservers.remove aft', this.addObservers.size())
            }
          }
        };

        _proto.HasUpdateObserver = function HasUpdateObserver(action) {
          if (!this.isInUpdate) {
            return this.updateObservers.Contains(action);
          } else {
            if (this.removeObservers.has(action)) {
              return false;
            } else if (this.addObservers.has(action)) {
              return true;
            } else {
              return this.updateObservers.Contains(action);
            }
          }
        };

        _proto.Update = function Update(deltaTime) {
          var _this = this;

          this.elapsedTime += deltaTime;
          this.isInUpdate = true;
          this.updateObservers.ToArray().forEach(function (action) {
            if (!_this.removeObservers.has(action)) {
              action();
            }
          });
          this.timers.forEach(function (val, key) {
            if (_this.removeTimers.has(key)) {
              return;
            }

            var time = _this.timers.get(key);

            if (time.absoluteTime <= _this.elapsedTime) {
              if (time.repeat == 0) {
                _this.RemoveTimer(key);
              } else if (time.repeat >= 0) {
                time.repeat--;
              }

              key();
            }
          });
          this.addObservers.toArray().forEach(function (action) {
            _this.updateObservers.Add(action);
          });
          this.removeObservers.toArray().forEach(function (action) {
            _this.updateObservers.Remove(action);
          });
          this.addTimers.forEach(function (val, action) {
            if (_this.timers.has(action)) {
              _this.timers.get(action).used = false;
            }

            _this.timers.set(action, _this.addTimers.get(action));
          });
          this.removeTimers.forEach(function (val, action) {
            _this.timers.get(action).used = false;

            _this.timers["delete"](action);
          });
          this.addObservers.clear();
          this.removeObservers.clear();
          this.addTimers.clear();
          this.removeTimers.clear();
          this.isInUpdate = false;
        };

        return Clock;
      }());

      _cclegacy._RF.pop();

      _crd = false;
    }
  };
});
//# sourceMappingURL=Clock.js.map