/*
 * Copyright 2026, Jaime Terrats.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import { Hono } from 'hono';
import { readWorkspaceConfig, validateWorkspaceConfig, writeWorkspaceConfig, getWorkspaceConfigPath, } from '../../services/workspace-config.js';
import { getWorkspaceCwd } from '../middleware/workspace.js';
export function configRoutes(cwd) {
    const app = new Hono();
    app.get('/', (c) => {
        const dir = getWorkspaceCwd(c, cwd);
        const config = readWorkspaceConfig(dir);
        return c.json(config ?? { exists: false });
    });
    app.put('/', async (c) => {
        const dir = getWorkspaceCwd(c, cwd);
        const body = await c.req.json();
        const validation = validateWorkspaceConfig(body);
        if (!validation.valid) {
            return c.json({ written: false, errors: validation.errors }, 400);
        }
        const result = writeWorkspaceConfig(dir, body, { force: true });
        return c.json({ written: result.written, path: result.path });
    });
    app.get('/path', (c) => {
        const dir = getWorkspaceCwd(c, cwd);
        return c.json({ path: getWorkspaceConfigPath(dir) });
    });
    return app;
}
//# sourceMappingURL=config.js.map