var __defProp = Object.defineProperty;
var __returnValue = (v) => v;
function __exportSetter(name, newValue) {
  this[name] = __returnValue.bind(null, newValue);
}
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, {
      get: all[name],
      enumerable: true,
      configurable: true,
      set: __exportSetter.bind(all, name)
    });
};
var __esm = (fn, res) => () => (fn && (res = fn(fn = 0)), res);
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
  get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
}) : x)(function(x) {
  if (typeof require !== "undefined")
    return require.apply(this, arguments);
  throw Error('Dynamic require of "' + x + '" is not supported');
});

// src/logger.ts
function isProduction() {
  if (typeof process !== "undefined" && process.env) {
    return false;
  }
  return false;
}
function setLogLevel(level) {
  config.level = level;
}
function getLogLevel() {
  return config.level;
}
function configureLogger(options) {
  config = { ...config, ...options };
}
function enableLogging() {
  config.level = "debug";
}
function disableLogging() {
  config.level = "none";
}
function setDebugMode(enabled) {
  config.level = enabled ? "debug" : "error";
}
function isDebugMode() {
  return config.level === "debug";
}
function shouldLog(level) {
  return LOG_LEVEL_ORDER[level] >= LOG_LEVEL_ORDER[config.level];
}
function formatTag(tag, level) {
  const timestamp = config.timestamps ? `${new Date().toISOString()} ` : "";
  if (config.colors && level !== "none") {
    const color = LOG_LEVEL_COLORS[level];
    return `${timestamp}${color}[${tag}]${RESET_COLOR}`;
  }
  return `${timestamp}[${tag}]`;
}

class Logger {
  tag;
  constructor(tag) {
    this.tag = tag;
  }
  debug(...args) {
    if (!shouldLog("debug"))
      return;
    if (config.handler) {
      config.handler.debug(this.tag, ...args);
    } else {
      console.log(formatTag(this.tag, "debug"), ...args);
    }
  }
  info(...args) {
    if (!shouldLog("info"))
      return;
    if (config.handler) {
      config.handler.info(this.tag, ...args);
    } else {
      console.info(formatTag(this.tag, "info"), ...args);
    }
  }
  warn(...args) {
    if (!shouldLog("warn"))
      return;
    if (config.handler) {
      config.handler.warn(this.tag, ...args);
    } else {
      console.warn(formatTag(this.tag, "warn"), ...args);
    }
  }
  error(...args) {
    if (!shouldLog("error"))
      return;
    if (config.handler) {
      config.handler.error(this.tag, ...args);
    } else {
      console.error(formatTag(this.tag, "error"), ...args);
    }
  }
  time(label, fn) {
    if (!shouldLog("debug")) {
      return fn();
    }
    const start = performance.now();
    try {
      return fn();
    } finally {
      const duration = performance.now() - start;
      this.debug(`${label}: ${duration.toFixed(2)}ms`);
    }
  }
  async timeAsync(label, fn) {
    if (!shouldLog("debug")) {
      return fn();
    }
    const start = performance.now();
    try {
      return await fn();
    } finally {
      const duration = performance.now() - start;
      this.debug(`${label}: ${duration.toFixed(2)}ms`);
    }
  }
  child(subTag) {
    return new Logger(`${this.tag}:${subTag}`);
  }
  debugIf(condition, ...args) {
    if (condition)
      this.debug(...args);
  }
  warnIf(condition, ...args) {
    if (condition)
      this.warn(...args);
  }
  errorIf(condition, ...args) {
    if (condition)
      this.error(...args);
  }
  loggedOnce = new Set;
  warnOnce(key, ...args) {
    if (this.loggedOnce.has(key))
      return;
    this.loggedOnce.add(key);
    this.warn(...args);
  }
  debugOnce(key, ...args) {
    if (this.loggedOnce.has(key))
      return;
    this.loggedOnce.add(key);
    this.debug(...args);
  }
}
function createLogger(tag) {
  return new Logger(tag);
}
var LOG_LEVEL_ORDER, LOG_LEVEL_COLORS, RESET_COLOR = "\x1B[0m", config, logger, log, frameworkLoggers;
var init_logger = __esm(() => {
  LOG_LEVEL_ORDER = {
    debug: 0,
    info: 1,
    warn: 2,
    error: 3,
    none: 4
  };
  LOG_LEVEL_COLORS = {
    debug: "\x1B[36m",
    info: "\x1B[32m",
    warn: "\x1B[33m",
    error: "\x1B[31m"
  };
  config = {
    level: isProduction() ? "error" : "info",
    colors: true,
    timestamps: false
  };
  logger = createLogger("Hypen");
  log = {
    debug: (tag, ...args) => {
      if (!shouldLog("debug"))
        return;
      console.log(formatTag(tag, "debug"), ...args);
    },
    info: (tag, ...args) => {
      if (!shouldLog("info"))
        return;
      console.info(formatTag(tag, "info"), ...args);
    },
    warn: (tag, ...args) => {
      if (!shouldLog("warn"))
        return;
      console.warn(formatTag(tag, "warn"), ...args);
    },
    error: (tag, ...args) => {
      if (!shouldLog("error"))
        return;
      console.error(formatTag(tag, "error"), ...args);
    }
  };
  frameworkLoggers = {
    hypen: createLogger("Hypen"),
    engine: createLogger("Engine"),
    router: createLogger("Router"),
    state: createLogger("State"),
    events: createLogger("Events"),
    remote: createLogger("Remote"),
    renderer: createLogger("Renderer"),
    module: createLogger("Module"),
    lifecycle: createLogger("Lifecycle"),
    loader: createLogger("Loader"),
    context: createLogger("Context"),
    discovery: createLogger("Discovery"),
    plugin: createLogger("Plugin"),
    canvas: createLogger("Canvas"),
    debug: createLogger("Debug")
  };
});

// src/disposable.ts
init_logger();
var log2 = frameworkLoggers.lifecycle;
function isDisposable(obj) {
  return obj !== null && typeof obj === "object" && "dispose" in obj && typeof obj.dispose === "function";
}

class DisposableStack {
  stack = [];
  disposed = false;
  add(disposable) {
    if (this.disposed) {
      disposable.dispose();
      return disposable;
    }
    this.stack.push(disposable);
    return disposable;
  }
  addCallback(callback) {
    this.add({ dispose: callback });
  }
  addValue(value, dispose) {
    this.add({ dispose: () => dispose(value) });
    return value;
  }
  dispose() {
    if (this.disposed)
      return;
    this.disposed = true;
    while (this.stack.length > 0) {
      const item = this.stack.pop();
      try {
        item.dispose();
      } catch (error) {
        log2.error("Error during dispose:", error);
      }
    }
  }
  get isDisposed() {
    return this.disposed;
  }
  get size() {
    return this.stack.length;
  }
}
function disposableListener(target, event, handler, options) {
  target.addEventListener(event, handler, options);
  return {
    dispose: () => target.removeEventListener(event, handler, options)
  };
}
function disposableTimeout(callback, ms) {
  const id = setTimeout(callback, ms);
  return {
    id,
    dispose: () => clearTimeout(id)
  };
}
function disposableInterval(callback, ms) {
  const id = setInterval(callback, ms);
  return {
    id,
    dispose: () => clearInterval(id)
  };
}
function disposableWebSocket(ws) {
  return {
    dispose: () => {
      if (ws.readyState === WebSocket.OPEN || ws.readyState === WebSocket.CONNECTING) {
        ws.close();
      }
    }
  };
}
function disposableAbortController() {
  const controller = new AbortController;
  return {
    controller,
    signal: controller.signal,
    dispose: () => controller.abort()
  };
}
function disposableSubscription(unsubscribe) {
  return { dispose: unsubscribe };
}
var ELEMENT_DISPOSABLES = Symbol("hypen.disposables");
function getElementDisposables(element) {
  const existing = element[ELEMENT_DISPOSABLES];
  if (existing instanceof DisposableStack) {
    return existing;
  }
  const stack = new DisposableStack;
  element[ELEMENT_DISPOSABLES] = stack;
  return stack;
}
function disposeElement(element) {
  const stack = element[ELEMENT_DISPOSABLES];
  if (stack instanceof DisposableStack) {
    stack.dispose();
    delete element[ELEMENT_DISPOSABLES];
  }
}
function hasElementDisposables(element) {
  return element[ELEMENT_DISPOSABLES] instanceof DisposableStack;
}

class DisposableMixin {
  disposables = new DisposableStack;
  track(disposable) {
    return this.disposables.add(disposable);
  }
  onDispose(callback) {
    this.disposables.addCallback(callback);
  }
  dispose() {
    this.disposables.dispose();
  }
}
function compositeDisposable(...disposables) {
  return {
    dispose: () => {
      for (const d of disposables) {
        try {
          d.dispose();
        } catch (error) {
          log2.error("Error during dispose:", error);
        }
      }
    }
  };
}
async function using(resource, fn) {
  const r = typeof resource === "function" ? resource() : resource;
  try {
    return await fn(r);
  } finally {
    r.dispose();
  }
}
function usingSync(resource, fn) {
  const r = typeof resource === "function" ? resource() : resource;
  try {
    return fn(r);
  } finally {
    r.dispose();
  }
}
export {
  usingSync,
  using,
  isDisposable,
  hasElementDisposables,
  getElementDisposables,
  disposeElement,
  disposableWebSocket,
  disposableTimeout,
  disposableSubscription,
  disposableListener,
  disposableInterval,
  disposableAbortController,
  compositeDisposable,
  DisposableStack,
  DisposableMixin
};

//# debugId=3EB92CA6084D365D64756E2164756E21
