import { getSlackConfig, appendDelivery } from '../../../storage/slack-store.js';
import { buildSpecPayload, dispatchToSlack } from '../../../engine/slack-dispatcher.js';
async function handler(ctx) {
    const { projectPath, spec, newStatus, currentStatus } = ctx;
    if (!projectPath) {
        return;
    }
    try {
        const config = await getSlackConfig(projectPath);
        if (config?.enabled !== true) {
            return;
        }
        const payload = buildSpecPayload(spec.id, spec.title, currentStatus, newStatus);
        const delivery = {
            id: `dlv-${Date.now()}`,
            sentAt: new Date().toISOString(),
            status: 'sent',
            specId: spec.id,
            eventType: 'status_change',
        };
        try {
            await dispatchToSlack(config.webhookUrl, payload);
        }
        catch (err) {
            delivery.status = 'failed';
            delivery.error = err instanceof Error ? err.message : String(err);
        }
        await appendDelivery(projectPath, delivery);
    }
    catch {
        /* best-effort — never throw */
    }
}
export const slackNotifyHook = {
    id: 'slack-notify',
    triggers: [], // any status change
    requiresProjectPath: true,
    label: 'Slack notification on status change (SPEC-292)',
    handler,
};
//# sourceMappingURL=slack-notify.hook.js.map