const CHANNEL_ID = "dingtalk-connector";
export const dingtalkConnectorOnboardingAdapter = {
    channel: CHANNEL_ID,
    getStatus: async ({ cfg }) => {
        const channelCfg = cfg.channels?.[CHANNEL_ID];
        const configured = Boolean(channelCfg?.clientId && channelCfg?.clientSecret);
        return {
            channel: CHANNEL_ID,
            configured,
            statusLines: [`钉钉: ${configured ? "已配置" : "需要填写凭证"}`],
            selectionHint: configured ? "recommended · configured" : "需要钉钉开放平台应用凭证",
            quickstartScore: configured ? 1 : 10,
        };
    },
    configure: async ({ cfg, prompter }) => {
        const channelCfg = cfg.channels?.[CHANNEL_ID];
        const existingClientId = channelCfg?.clientId ?? "";
        const clientId = String(await prompter.text({
            message: "输入钉钉应用 Client ID",
            initialValue: existingClientId,
            validate: (val) => (val?.trim() ? undefined : "必填"),
        })).trim();
        const existingClientSecret = channelCfg?.clientSecret ?? "";
        const clientSecret = String(await prompter.text({
            message: "输入钉钉应用 Client Secret",
            initialValue: existingClientSecret,
            validate: (val) => (val?.trim() ? undefined : "必填"),
        })).trim();
        // Default to existing gateway.auth.token if available
        const resolvedExistingToken = (channelCfg?.gatewayToken ?? "") ||
            cfg.gateway?.auth?.token ||
            "";
        const gatewayToken = String(await prompter.text({
            message: "输入 Gateway 认证 Token（openclaw.json 中 gateway.auth.token 的值）",
            initialValue: resolvedExistingToken,
            validate: (val) => (val?.trim() ? undefined : "必填"),
        })).trim();
        const next = applyDingtalkConnectorConfig(cfg, { clientId, clientSecret, gatewayToken });
        return { cfg: next };
    },
    disable: (cfg) => {
        const next = { ...cfg };
        const channels = { ...next.channels };
        const existing = channels[CHANNEL_ID];
        if (existing) {
            channels[CHANNEL_ID] = { ...existing, enabled: false };
            next.channels = channels;
        }
        return next;
    },
};
function applyDingtalkConnectorConfig(cfg, updates) {
    const next = { ...cfg };
    const channels = { ...next.channels };
    const existing = channels[CHANNEL_ID] ?? {};
    channels[CHANNEL_ID] = {
        ...existing,
        clientId: updates.clientId,
        clientSecret: updates.clientSecret,
        gatewayToken: updates.gatewayToken,
        enabled: true,
    };
    next.channels = channels;
    // Ensure gateway.http.endpoints.chatCompletions.enabled = true (required by connector)
    const gateway = { ...next.gateway };
    const http = { ...gateway.http };
    const endpoints = { ...http.endpoints };
    const chatCompletions = {
        ...endpoints.chatCompletions,
        enabled: true,
    };
    endpoints.chatCompletions = chatCompletions;
    http.endpoints = endpoints;
    gateway.http = http;
    // Ensure gateway.auth.mode = "token" and token is set
    const auth = { ...gateway.auth };
    if (!auth.mode)
        auth.mode = "token";
    if (!auth.token)
        auth.token = updates.gatewayToken;
    gateway.auth = auth;
    next.gateway = gateway;
    return next;
}
