"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.mapSlackErrorToActivityEvent = exports.isSlackErrorResponse = exports.SlackClient = void 0;
const web_api_1 = require("@slack/web-api");
class SlackClient {
    constructor({ botToken }) {
        this.client = new web_api_1.WebClient(botToken);
    }
    async postMessage({ channel, text, }) {
        return await this.client.chat.postMessage({
            channel,
            text,
        });
    }
}
exports.SlackClient = SlackClient;
const isSlackErrorResponse = (error) => {
    return (typeof error === 'object' &&
        error !== null &&
        'code' in error &&
        'data' in error &&
        typeof error.data === 'object' &&
        error.data !== null &&
        'error' in error.data);
};
exports.isSlackErrorResponse = isSlackErrorResponse;
const mapSlackErrorToActivityEvent = (error) => {
    var _a;
    const errorMessage = error.data.error;
    const errorMessages = {
        channel_not_found: 'The specified channel was not found. Make sure the channel exists and the bot has been invited to it.',
        not_in_channel: 'The bot is not a member of the channel. Invite the bot to the channel using /invite @botname.',
        is_archived: 'The channel has been archived and cannot receive messages.',
        msg_too_long: 'The message text is too long.',
        no_text: 'No message text was provided.',
        invalid_auth: 'The bot token is invalid or has been revoked.',
        account_inactive: 'The Slack workspace associated with this token has been deleted.',
        token_revoked: 'The bot token has been revoked.',
        missing_scope: 'The bot token is missing the required chat:write scope.',
    };
    const friendlyMessage = (_a = errorMessages[errorMessage]) !== null && _a !== void 0 ? _a : `Slack API error: ${errorMessage}`;
    return {
        date: new Date().toISOString(),
        text: { en: friendlyMessage },
        error: {
            category: 'SERVER_ERROR',
            message: friendlyMessage,
        },
    };
};
exports.mapSlackErrorToActivityEvent = mapSlackErrorToActivityEvent;
//# sourceMappingURL=slackClient.js.map