import { SceneObjectBase } from '../core/SceneObjectBase.js';
import { writeSceneLog } from '../utils/writeSceneLog.js';

var __typeError$4 = (msg) => {
  throw TypeError(msg);
};
var __accessCheck$4 = (obj, member, msg) => member.has(obj) || __typeError$4("Cannot " + msg);
var __privateGet$4 = (obj, member, getter) => (__accessCheck$4(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
var __privateAdd$4 = (obj, member, value) => member.has(obj) ? __typeError$4("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
var __privateSet$4 = (obj, member, value, setter) => (__accessCheck$4(obj, member, "write to private field"), member.set(obj, value), value);
var _running, _tryCompleteProfileFrameId;
function isQueryController(s) {
  return "isQueryController" in s;
}
class SceneQueryController extends SceneObjectBase {
  constructor(state = {}, profiler) {
    super({ ...state, isRunning: false });
    this.profiler = profiler;
    this.isQueryController = true;
    __privateAdd$4(this, _running, /* @__PURE__ */ new Set());
    __privateAdd$4(this, _tryCompleteProfileFrameId, null);
    this.runningQueriesCount = () => {
      return __privateGet$4(this, _running).size;
    };
    if (profiler) {
      this.profiler = profiler;
      profiler.setQueryController(this);
    }
    this.addActivationHandler(() => {
      var _a;
      (_a = this.profiler) == null ? void 0 : _a.setQueryController(this);
      return () => {
        for (const entry of __privateGet$4(this, _running)) {
          this.changeRunningQueryCount(-1, entry);
        }
        __privateGet$4(this, _running).clear();
      };
    });
  }
  startProfile(name) {
    var _a;
    if (!this.state.enableProfiling) {
      return;
    }
    (_a = this.profiler) == null ? void 0 : _a.startProfile(name);
  }
  cancelProfile() {
    var _a;
    (_a = this.profiler) == null ? void 0 : _a.cancelProfile();
  }
  queryStarted(entry) {
    __privateGet$4(this, _running).add(entry);
    this.changeRunningQueryCount(1, entry);
    if (!this.state.isRunning) {
      this.setState({ isRunning: true });
    }
  }
  queryCompleted(entry) {
    if (!__privateGet$4(this, _running).has(entry)) {
      return;
    }
    __privateGet$4(this, _running).delete(entry);
    this.changeRunningQueryCount(-1);
    if (__privateGet$4(this, _running).size === 0) {
      this.setState({ isRunning: false });
    }
  }
  changeRunningQueryCount(dir, entry) {
    var _a, _b, _c, _d;
    window.__grafanaRunningQueryCount = ((_a = window.__grafanaRunningQueryCount) != null ? _a : 0) + dir;
    if (dir === 1 && this.state.enableProfiling) {
      if (entry) {
        (_b = this.profiler) == null ? void 0 : _b.addCrumb(`${entry.type}`);
      }
      if ((_c = this.profiler) == null ? void 0 : _c.isTailRecording()) {
        writeSceneLog("SceneQueryController", "New query started, cancelling tail recording");
        (_d = this.profiler) == null ? void 0 : _d.cancelTailRecording();
      }
    }
    if (this.state.enableProfiling) {
      if (__privateGet$4(this, _tryCompleteProfileFrameId)) {
        cancelAnimationFrame(__privateGet$4(this, _tryCompleteProfileFrameId));
      }
      __privateSet$4(this, _tryCompleteProfileFrameId, requestAnimationFrame(() => {
        var _a2;
        (_a2 = this.profiler) == null ? void 0 : _a2.tryCompletingProfile();
      }));
    }
  }
  cancelAll() {
    var _a;
    for (const entry of __privateGet$4(this, _running).values()) {
      (_a = entry.cancel) == null ? void 0 : _a.call(entry);
    }
  }
}
_running = new WeakMap();
_tryCompleteProfileFrameId = new WeakMap();

export { SceneQueryController, isQueryController };
//# sourceMappingURL=SceneQueryController.js.map
