import { createRequire as createRequireShim } from 'module'; const require = createRequireShim(import.meta.url);

// src/state.ts
import { isMainThread, parentPort, threadId } from "node:worker_threads";
var State = class _State {
  stateMap = /* @__PURE__ */ new Map();
  static INSTANCE = new _State();
  static reset() {
    _State.INSTANCE = new _State();
  }
};
var StateStorage = class {
  // TODO learn how to define single instance for all subclasses
  constructor() {
  }
  key() {
    return this.constructor.name;
  }
  getOrRegister() {
    let metricState = State.INSTANCE.stateMap.get(this.key());
    if (!metricState) {
      metricState = this.initValue();
      State.INSTANCE.stateMap.set(this.key(), metricState);
    }
    return metricState;
  }
  unregister() {
    const value = State.INSTANCE.stateMap.get(this.key());
    State.INSTANCE.stateMap.delete(this.key());
    return value;
  }
};
var MapStateStorage = class extends StateStorage {
  initValue() {
    return /* @__PURE__ */ new Map();
  }
  getValue(key) {
    const m = this.getOrRegister();
    return m.get(key);
  }
  getValues() {
    const m = this.getOrRegister();
    return Array.from(m.values());
  }
  getOrSetValue(key, value) {
    const m = this.getOrRegister();
    const oldValue = m.get(key);
    if (oldValue) {
      if (oldValue !== value) {
        console.warn(key, "has been registered twice, use the previous one");
      }
      return oldValue;
    }
    m.set(key, value);
    return value;
  }
};
var ListStateStorage = class extends StateStorage {
  initValue() {
    return [];
  }
  getValues() {
    return this.getOrRegister();
  }
  addValue(value) {
    const m = this.getOrRegister();
    m.push(value);
    return value;
  }
};
var TemplateInstanceState = class _TemplateInstanceState extends ListStateStorage {
  static INSTANCE = new _TemplateInstanceState();
  constructor() {
    super();
  }
  addValue(value) {
    if (!isMainThread) {
      parentPort?.postMessage({ event: "add_template_instance", value, from: threadId });
    }
    return super.addValue(value);
  }
};
import("node:process").then((p) => p.stdout.write(""));

export {
  State,
  StateStorage,
  MapStateStorage,
  ListStateStorage,
  TemplateInstanceState
};
//# sourceMappingURL=chunk-6XHWJ2VS.js.map