import { parseSlackContextKey, slackContextKey } from "../shared/context-key.js";
export function slackRequestFromMessage(event) {
    const threadTs = event.thread_ts && event.thread_ts !== event.ts ? event.thread_ts : undefined;
    return {
        contextKey: slackContextKey({ teamId: event.team, channelId: event.channel, threadTs }),
        context: { channelId: "slack", chatId: event.channel, ...(threadTs ? { topicId: threadTs } : {}), userId: event.user, username: event.username },
        userId: event.user ?? "unknown",
        username: event.username,
        teamId: event.team,
        channelId: event.channel,
        channelName: event.channel,
        isDirectMessage: event.channel_type === "im" || event.channel.startsWith("D"),
        source: "message",
    };
}
export function slackRequestFromSlashCommand(command, respond) {
    return {
        contextKey: slackContextKey({ teamId: command.team_id, channelId: command.channel_id }),
        context: { channelId: "slack", chatId: command.channel_id, userId: command.user_id, username: command.user_name },
        userId: command.user_id,
        username: command.user_name,
        teamId: command.team_id,
        channelId: command.channel_id,
        channelName: command.channel_name,
        isDirectMessage: command.channel_name === "directmessage" || command.channel_id.startsWith("D"),
        source: "slash",
        respond,
    };
}
export function slackRequestFromAction(body, respond) {
    const channelId = body.channel?.id ?? "";
    const teamId = body.team?.id;
    const threadTs = body.message?.thread_ts && body.message.thread_ts !== body.message?.ts ? body.message.thread_ts : undefined;
    return {
        contextKey: slackContextKey({ teamId, channelId, threadTs }),
        context: { channelId: "slack", chatId: channelId, ...(threadTs ? { topicId: threadTs } : {}), userId: body.user?.id, username: body.user?.username },
        userId: body.user?.id ?? "unknown",
        username: body.user?.username,
        teamId,
        channelId,
        channelName: body.channel?.name,
        isDirectMessage: channelId.startsWith("D"),
        source: "action",
        respond,
    };
}
export function stripSlackMention(text) {
    return text.replace(/^<@[^>]+>\s*/, "");
}
export function isSlackTeamAllowed(config, teamId) {
    return !teamId || config.slackAllowedTeamIds.length === 0 || config.slackAllowedTeamIds.includes(teamId);
}
export function isSlackChannelAllowedByEnv(config, channelId) {
    return config.slackAllowedChannelIds.length === 0 || config.slackAllowedChannelIds.includes(channelId);
}
export function canSendSystemMessagesToSlackContext(userStore, contextKey) {
    if (!userStore.hasAdminUser()) {
        return false;
    }
    const parsed = parseSlackContextKey(contextKey);
    if (!parsed) {
        return false;
    }
    return userStore.snapshot().slackChannels.some((channel) => channel.enabled &&
        channel.channelId === parsed.channelId &&
        (channel.teamId ?? "") === (parsed.teamId ?? ""));
}
