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/events.ts
init_logger();
var log2 = frameworkLoggers.events;

class TypedEventEmitter {
  eventBus = new Map;
  emit(event, payload) {
    const handlers = this.eventBus.get(event);
    if (!handlers || handlers.size === 0) {
      return;
    }
    handlers.forEach((handler) => {
      try {
        handler(payload);
      } catch (error) {
        log2.error(`Error in event handler for "${String(event)}":`, error);
      }
    });
  }
  on(event, handler) {
    if (!this.eventBus.has(event)) {
      this.eventBus.set(event, new Set);
    }
    const handlers = this.eventBus.get(event);
    handlers.add(handler);
    return () => {
      handlers.delete(handler);
      if (handlers.size === 0) {
        this.eventBus.delete(event);
      }
    };
  }
  once(event, handler) {
    const wrappedHandler = (payload) => {
      handler(payload);
      unsubscribe();
    };
    const unsubscribe = this.on(event, wrappedHandler);
    return unsubscribe;
  }
  off(event, handler) {
    const handlers = this.eventBus.get(event);
    if (handlers) {
      handlers.delete(handler);
      if (handlers.size === 0) {
        this.eventBus.delete(event);
      }
    }
  }
  removeAllListeners(event) {
    this.eventBus.delete(event);
  }
  clearAll() {
    this.eventBus.clear();
  }
  listenerCount(event) {
    return this.eventBus.get(event)?.size ?? 0;
  }
  eventNames() {
    return Array.from(this.eventBus.keys());
  }
}
function createEventEmitter() {
  return new TypedEventEmitter;
}

// src/context.ts
init_logger();
var log3 = frameworkLoggers.context;

class HypenGlobalContext {
  modules = new Map;
  typedEvents;
  legacyEventBus = new Map;
  constructor() {
    this.typedEvents = new TypedEventEmitter;
  }
  get events() {
    return this.typedEvents;
  }
  registerModule(id, instance) {
    if (this.modules.has(id)) {
      log3.warn(`Module "${id}" is already registered. Overwriting.`);
    }
    this.modules.set(id, instance);
    log3.debug(`Registered module: ${id}`);
  }
  unregisterModule(id) {
    this.modules.delete(id);
    log3.debug(`Unregistered module: ${id}`);
  }
  getModule(id) {
    const module = this.modules.get(id);
    if (!module) {
      throw new Error(`Module "${id}" not found. Available modules: ${Array.from(this.modules.keys()).join(", ")}`);
    }
    return {
      state: module.getLiveState(),
      setState: (patch) => module.updateState(patch),
      getState: () => module.getState()
    };
  }
  hasModule(id) {
    return this.modules.has(id);
  }
  getModuleIds() {
    return Array.from(this.modules.keys());
  }
  getGlobalState() {
    const state = {};
    this.modules.forEach((module, id) => {
      state[id] = module.getState();
    });
    return state;
  }
  emit(event, payload) {
    const handlers = this.legacyEventBus.get(event);
    if (!handlers || handlers.size === 0) {
      log3.debug(`Event "${event}" emitted but no listeners`);
    } else {
      log3.debug(`Emitting event: ${event}`, payload);
      handlers.forEach((handler) => {
        try {
          handler(payload);
        } catch (error) {
          log3.error(`Error in event handler for "${event}":`, error);
        }
      });
    }
    if (this.typedEvents.listenerCount(event) > 0) {
      this.typedEvents.emit(event, payload);
    }
  }
  on(event, handler) {
    if (!this.legacyEventBus.has(event)) {
      this.legacyEventBus.set(event, new Set);
    }
    const handlers = this.legacyEventBus.get(event);
    handlers.add(handler);
    log3.debug(`Listening to event: ${event}`);
    return () => {
      handlers.delete(handler);
      if (handlers.size === 0) {
        this.legacyEventBus.delete(event);
      }
    };
  }
  off(event, handler) {
    const handlers = this.legacyEventBus.get(event);
    if (handlers) {
      handlers.delete(handler);
      if (handlers.size === 0) {
        this.legacyEventBus.delete(event);
      }
    }
  }
  clearEvent(event) {
    this.legacyEventBus.delete(event);
  }
  clearAllEvents() {
    this.legacyEventBus.clear();
  }
  debug() {
    return {
      modules: this.getModuleIds(),
      events: Array.from(this.legacyEventBus.keys()),
      typedEvents: this.typedEvents.eventNames(),
      state: this.getGlobalState()
    };
  }
}
export {
  HypenGlobalContext
};

//# debugId=8FE23F2EDADEE4C164756E2164756E21
