//#region extensions/telegram/src/account-manager.ts
var TelegramAccountManager = class {
	accounts = /* @__PURE__ */ new Map();
	bots = /* @__PURE__ */ new Map();
	runners = /* @__PURE__ */ new Map();
	statuses = /* @__PURE__ */ new Map();
	botUsernames = /* @__PURE__ */ new Map();
	startingAccounts = /* @__PURE__ */ new Set();
	registerAccount(account) {
		this.accounts.set(account.accountId, account);
		this.statuses.set(account.accountId, {
			accountId: account.accountId,
			running: false,
			mode: "stopped"
		});
	}
	getAccount(accountId) {
		return this.accounts.get(accountId);
	}
	getAllAccounts() {
		return Array.from(this.accounts.values());
	}
	/** Clear all accounts and runtime state (used when reloading channel config). */
	reset() {
		this.accounts.clear();
		this.bots.clear();
		this.runners.clear();
		this.statuses.clear();
		this.botUsernames.clear();
		this.startingAccounts.clear();
	}
	registerBot(accountId, bot) {
		this.bots.set(accountId, bot);
	}
	getBot(accountId) {
		return this.bots.get(accountId);
	}
	registerRunner(accountId, runner) {
		this.runners.set(accountId, runner);
	}
	async stopRunner(accountId) {
		const runner = this.runners.get(accountId);
		const bot = this.bots.get(accountId);
		if (runner) {
			await runner.stop();
			this.runners.delete(accountId);
		}
		if (bot) {
			bot.stop();
			this.bots.delete(accountId);
		}
		this.startingAccounts.delete(accountId);
		this.updateStatus({
			accountId,
			running: false,
			mode: "stopped"
		});
	}
	isStarting(accountId) {
		return this.startingAccounts.has(accountId);
	}
	markStarting(accountId) {
		this.startingAccounts.add(accountId);
	}
	markStartComplete(accountId) {
		this.startingAccounts.delete(accountId);
	}
	isRunning(accountId) {
		return this.statuses.get(accountId)?.running ?? false;
	}
	updateStatus(status) {
		this.statuses.set(status.accountId, status);
	}
	getStatus(accountId) {
		return this.statuses.get(accountId);
	}
	setBotUsername(accountId, username) {
		this.botUsernames.set(accountId, username);
	}
	getBotUsername(accountId) {
		return this.botUsernames.get(accountId);
	}
	/** Map the bot handling this update to a configured account id (session keys must match inbound). */
	resolveAccountIdFromContext(ctx) {
		const botId = ctx.me?.id;
		if (botId == null) return this.getAllAccounts()[0]?.accountId ?? "default";
		for (const [accountId, bot] of this.bots) if (bot.botInfo?.id === botId) return accountId;
		const accounts = this.getAllAccounts();
		if (accounts.length === 1) return accounts[0].accountId;
		return "default";
	}
};
//#endregion
export { TelegramAccountManager };

//# sourceMappingURL=account-manager.js.map