/*
 * 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 { existsSync } from 'node:fs';
import { join } from 'node:path';
import { Hono } from 'hono';
import { ALL_PROFILES } from '../../profiles/index.js';
import { buildWorkspaceConfig, writeWorkspaceConfig } from '../../services/workspace-config.js';
import { setupLocalState } from '../../services/local-state.js';
import { FileWriter } from '../../services/file-writer.js';
import { getWorkspaceCwd } from '../middleware/workspace.js';
import { setupCursor } from '../../setup/cursor-setup.js';
import { setupClaude } from '../../setup/claude-setup.js';
import { setupCodex } from '../../setup/codex-setup.js';
import { setupVsCode } from '../../setup/vscode-setup.js';
import { setupWindsurf } from '../../setup/windsurf-setup.js';
import { setupJetBrains } from '../../setup/jetbrains-setup.js';
import { setupAgentforce } from '../../setup/agentforce-setup.js';
export function initRoute(cwd) {
    const app = new Hono();
    app.post('/', async (c) => {
        const dir = getWorkspaceCwd(c, cwd);
        const body = await c.req.json();
        const profiles = ALL_PROFILES.filter((p) => body.profiles.includes(p.id));
        if (profiles.length === 0) {
            return c.json({ error: 'At least one profile must be selected' }, 400);
        }
        const config = buildWorkspaceConfig({
            mode: body.mode,
            profiles,
            tools: body.tools,
        });
        if (!body.orchestration) {
            config.orchestration.enabled = false;
        }
        const writeResult = writeWorkspaceConfig(dir, config, { force: true });
        const generatedFiles = [writeResult.path];
        const trackingLog = (filePath) => {
            generatedFiles.push(filePath);
        };
        const noop = () => {
            /* intentional no-op */
        };
        const writer = new FileWriter({ force: true, log: trackingLog, warn: noop });
        setupLocalState(dir, writer);
        const tools = body.tools;
        const isSalesforceProject = existsSync(join(dir, 'sfdx-project.json'));
        if (tools.includes('cursor')) {
            await setupCursor({
                cwd: dir,
                profiles,
                writer,
                isSalesforceProject,
                promptScope: () => Promise.resolve('project'),
                logInfo: noop,
            });
        }
        if (tools.includes('claude')) {
            setupClaude({ cwd: dir, profiles, writer });
        }
        if (tools.includes('codex')) {
            setupCodex({ cwd: dir, profiles, writer, isSalesforceProject });
        }
        if (tools.includes('vscode')) {
            setupVsCode({ cwd: dir, profiles, writer, isSalesforceProject });
        }
        if (tools.includes('windsurf')) {
            setupWindsurf({ cwd: dir, profiles, writer });
        }
        if (tools.includes('jetbrains')) {
            setupJetBrains({ cwd: dir, profiles, writer });
        }
        if (tools.includes('agentforce')) {
            setupAgentforce({ cwd: dir, profiles, writer, isSalesforceProject });
        }
        return c.json({
            configFile: writeResult.path,
            generatedFiles,
            config,
        });
    });
    return app;
}
//# sourceMappingURL=init.js.map