import { logger } from "../util/logger.js";
import { toRawIlinkUserIdForApi } from "../auth/weixin-account-id.js";
import { getContextToken, getWeixinOutboundSendUserId, setContextToken } from "./inbound.js";
import { getConfig } from "../api/api.js";
//#region extensions/weixin/src/messaging/context-token-init.ts
/**
* When there is no cached context_token (e.g. cron before any inbound in this process),
* try ilink getconfig — some deployments return context_token for the given ilink_user_id.
*/
async function ensureWeixinContextTokenForOutbound(accountId, userId, account) {
	const cached = getContextToken(accountId, userId);
	if (cached) return cached;
	if (!account.token?.trim()) return void 0;
	try {
		const ilinkUserId = getWeixinOutboundSendUserId(accountId, userId)?.trim() || toRawIlinkUserIdForApi(userId);
		const resp = await getConfig({
			baseUrl: account.baseUrl,
			token: account.token,
			routeTag: account.routeTag,
			ilinkUserId,
			contextToken: void 0
		});
		if (resp.ret != null && resp.ret !== 0) {
			logger.debug({
				userId,
				accountId,
				ret: resp.ret,
				errmsg: resp.errmsg
			}, "ensureWeixinContextTokenForOutbound: getConfig returned error");
			return;
		}
		const t = resp.context_token?.trim();
		if (t) {
			setContextToken(accountId, userId, t);
			logger.info({
				userId,
				accountId
			}, "ensureWeixinContextTokenForOutbound: stored context_token from getConfig");
			return t;
		}
	} catch (e) {
		logger.debug({
			err: e,
			userId,
			accountId
		}, "ensureWeixinContextTokenForOutbound: getConfig failed");
	}
}
//#endregion
export { ensureWeixinContextTokenForOutbound };

//# sourceMappingURL=context-token-init.js.map