import { v4 as uuid } from 'uuid';
import { listRobotAccounts, getRobotAccount, createRobotAccount, updateRobotAccount, deleteRobotAccount } from '../storage/robot-account-store.js';
export { listRobotAccounts, getRobotAccount, deleteRobotAccount };
export function createAccount(input) {
    const now = new Date().toISOString();
    const account = { id: uuid(), name: input.name, type: input.type, lark: input.lark, wechat: input.wechat, createdAt: now, updatedAt: now };
    createRobotAccount(account);
    return account;
}
export function updateAccount(id, patch) {
    return updateRobotAccount(id, patch);
}
export function resolveCredentials(settings) {
    if (settings.robotAccountId) {
        const account = getRobotAccount(settings.robotAccountId);
        if (account) {
            if (account.type === 'lark' && account.lark) {
                return { type: 'lark', appId: account.lark.appId, appSecret: account.lark.appSecret };
            }
            if (account.type === 'wechat' && account.wechat) {
                return { type: 'wechat', token: account.wechat.token, baseUrl: account.wechat.baseUrl || 'https://qyapi.weixin.qq.com', accountId: account.wechat.accountId, userId: account.wechat.userId };
            }
        }
    }
    // fallback to inline credentials
    if (settings.provider === 'lark' && settings.lark?.appId && settings.lark?.appSecret) {
        return { type: 'lark', appId: settings.lark.appId, appSecret: settings.lark.appSecret };
    }
    if (settings.provider === 'wechat' && settings.wechat?.token && settings.wechat?.accountId) {
        return { type: 'wechat', token: settings.wechat.token, baseUrl: settings.wechat.baseUrl || 'https://qyapi.weixin.qq.com', accountId: settings.wechat.accountId, userId: settings.wechat.userId };
    }
    return null;
}
//# sourceMappingURL=robot-account.js.map