"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.slackCommand = void 0;
const helpers_1 = require("./helpers");
exports.slackCommand = {
    command: 'slack',
    describe: 'Connect Amplitude to Slack',
    builder: (yargs) => yargs,
    handler: () => {
        void (async () => {
            // Dynamic imports may land named exports on `.default` under tsx
            // CJS/ESM interop. This helper normalises that.
            const cjs = (mod) => (mod.default ?? mod);
            try {
                const { getStoredUser, getStoredToken } = cjs(await import('../utils/ampli-settings.js'));
                const { readAmpliConfig } = cjs(await import('../lib/ampli-config.js'));
                const { fetchSlackInstallUrl, fetchSlackConnectionStatus } = cjs(await import('../lib/api.js'));
                const { OUTBOUND_URLS } = cjs(await import('../lib/constants.js'));
                const opn = (await import('opn')).default;
                const storedUser = getStoredUser();
                const zone = storedUser?.zone ?? 'us';
                const storedToken = getStoredToken(storedUser?.id, zone);
                // The App API validates access_tokens, not id_tokens.
                const accessToken = storedToken?.accessToken;
                // Read orgId from project-level ampli.json
                const ampliConfig = readAmpliConfig(process.cwd());
                const orgId = ampliConfig.ok ? ampliConfig.config.OrgId : undefined;
                if (!accessToken || !orgId) {
                    (0, helpers_1.setUI)(new helpers_1.LoggingUI());
                    (0, helpers_1.getUI)().log.info('No Amplitude session found. Run `npx @amplitude/wizard` first to log in and set up your project.');
                    process.exit(1);
                }
                // Check if Slack is already connected before prompting install.
                const isConnected = await fetchSlackConnectionStatus(accessToken, zone, orgId);
                if (isConnected) {
                    (0, helpers_1.setUI)(new helpers_1.LoggingUI());
                    (0, helpers_1.getUI)().log.info('Slack is already connected to your Amplitude workspace.');
                    process.exit(0);
                }
                const settingsUrl = OUTBOUND_URLS.slackSettings(zone, orgId);
                let url = settingsUrl;
                // Try to get the direct Slack OAuth URL from the App API.
                const directUrl = await fetchSlackInstallUrl(accessToken, zone, orgId, settingsUrl);
                if (directUrl)
                    url = directUrl;
                (0, helpers_1.setUI)(new helpers_1.LoggingUI());
                (0, helpers_1.getUI)().log.info(`Opening Slack integration: ${url}`);
                await opn(url, { wait: false });
            }
            catch {
                (0, helpers_1.setUI)(new helpers_1.LoggingUI());
                const { getCloudUrlFromRegion } = cjs(await import('../utils/urls.js'));
                const opn = (await import('opn')).default;
                const url = `${getCloudUrlFromRegion('us')}/analytics/settings/profile`;
                (0, helpers_1.getUI)().log.info(`Opening Amplitude Settings to connect Slack: ${url}`);
                await opn(url, { wait: false });
            }
        })();
    },
};
