import { Router } from 'express';
import * as store from '../storage/hook-store.js';
const router = Router({ mergeParams: true });
router.get('/', (req, res) => {
    try {
        res.json(store.listHooks(req.params.id));
    }
    catch (error) {
        res.status(500).json({ error: error.message });
    }
});
router.get('/:name', (req, res) => {
    try {
        const hook = store.getHook(req.params.id, req.params.name);
        if (!hook) {
            res.status(404).json({ error: 'Hook not found' });
            return;
        }
        res.json(hook);
    }
    catch (error) {
        res.status(500).json({ error: error.message });
    }
});
router.post('/', (req, res) => {
    try {
        const config = req.body;
        if (!config.name) {
            res.status(400).json({ error: 'name required' });
            return;
        }
        store.saveHook(req.params.id, config);
        res.status(201).json(config);
    }
    catch (error) {
        res.status(400).json({ error: error.message });
    }
});
router.put('/:name', (req, res) => {
    try {
        const config = req.body;
        config.name = req.params.name;
        store.saveHook(req.params.id, config);
        res.json(config);
    }
    catch (error) {
        res.status(400).json({ error: error.message });
    }
});
router.delete('/:name', (req, res) => {
    try {
        store.deleteHook(req.params.id, req.params.name);
        res.status(204).send();
    }
    catch (error) {
        res.status(400).json({ error: error.message });
    }
});
router.post('/upload', (req, res) => {
    try {
        const { content } = req.body;
        if (!content || typeof content !== 'string') {
            res.status(400).json({ error: 'content (string) required' });
            return;
        }
        const config = store.uploadHook(req.params.id, content);
        res.status(201).json(config);
    }
    catch (error) {
        res.status(400).json({ error: error.message });
    }
});
router.post('/:name/apply', (req, res) => {
    try {
        const { targetWorkspaceId } = req.body;
        if (!targetWorkspaceId) {
            res.status(400).json({ error: 'targetWorkspaceId required' });
            return;
        }
        store.applyToWorkspace(req.params.id, req.params.name, targetWorkspaceId);
        res.json({ ok: true });
    }
    catch (error) {
        res.status(400).json({ error: error.message });
    }
});
export default router;
//# sourceMappingURL=hooks.js.map