import { getFigmaConfig, saveFigmaWebhook } from '../../storage/figma-store.js';
import { registerFigmaWebhook, deregisterFigmaWebhook, } from '../../engine/figma/webhook-manager.js';
import { FIGMA_NOT_CONNECTED } from './shared.js';
export async function handleConfigureFigmaWebhook(input) {
    const config = await getFigmaConfig(input.projectPath);
    if (config === null) {
        return FIGMA_NOT_CONNECTED;
    }
    let registration;
    try {
        registration = await registerFigmaWebhook(input.teamId, input.callbackUrl, config.accessToken);
    }
    catch (err) {
        const msg = err instanceof Error ? err.message : String(err);
        return {
            content: [{ type: 'text', text: `Error registering webhook: ${msg}` }],
            isError: true,
        };
    }
    await saveFigmaWebhook(input.projectPath, registration);
    return {
        content: [
            {
                type: 'text',
                text: `Figma webhook registered.\n\nWebhook ID: ${registration.webhookId}\nCallback: ${registration.endpoint}\nPasscode: ${registration.passcode}\n\nStore the passcode securely — it is used to validate incoming webhook events.`,
            },
        ],
    };
}
export async function handleRemoveFigmaWebhook(input) {
    const config = await getFigmaConfig(input.projectPath);
    if (config === null) {
        return FIGMA_NOT_CONNECTED;
    }
    try {
        await deregisterFigmaWebhook(input.webhookId, config.accessToken);
    }
    catch (err) {
        const msg = err instanceof Error ? err.message : String(err);
        return { content: [{ type: 'text', text: `Error removing webhook: ${msg}` }], isError: true };
    }
    await saveFigmaWebhook(input.projectPath, null);
    return { content: [{ type: 'text', text: `Webhook ${input.webhookId} removed.` }] };
}
//# sourceMappingURL=webhooks.js.map