/*
 * 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 { join } from 'node:path';
import { generateA4dBaseGuidelines, generateA4dMcpSettings } from '../generators/agentforce-generator.js';
import { generateSfStandards } from '../generators/mdc-generator.js';
import { generateSubAgentProtocol } from '../generators/mdc-generator.js';
import { toA4dContent } from '../generators/shared.js';
import { getPortableSkillSections } from '../generators/skill-generator.js';
import { generateBaseWorkflows } from '../generators/workflow-generator.js';
import { ensureDir } from '../services/file-writer.js';
import { PLUGIN_VERSION } from '../version.js';
export function setupAgentforce(opts) {
    const { cwd, profiles, writer, isSalesforceProject } = opts;
    const a4dDir = join(cwd, '.a4drules');
    const workflowsDir = join(a4dDir, 'workflows');
    ensureDir(a4dDir);
    writer.write(join(cwd, 'a4d_mcp_settings.json'), generateA4dMcpSettings());
    writer.write(join(a4dDir, '00-base-guidelines.md'), generateA4dBaseGuidelines(PLUGIN_VERSION));
    if (isSalesforceProject) {
        writer.write(join(a4dDir, '01-salesforce-standards.md'), toA4dContent(generateSfStandards(PLUGIN_VERSION), PLUGIN_VERSION));
    }
    profiles.forEach((profile, i) => {
        const index = String(i + 2).padStart(2, '0');
        writer.write(join(a4dDir, `${index}-${profile.ruleFile.replace('.mdc', '.md')}`), toA4dContent(profile.ruleContent(), PLUGIN_VERSION));
    });
    writer.write(join(a4dDir, '99-sub-agent-protocol.md'), toA4dContent(generateSubAgentProtocol(profiles, PLUGIN_VERSION), PLUGIN_VERSION));
    if (isSalesforceProject) {
        ensureDir(workflowsDir);
        for (const [filename, content] of Object.entries(generateBaseWorkflows(PLUGIN_VERSION))) {
            writer.write(join(workflowsDir, filename), content);
        }
        for (const profile of profiles) {
            if (!profile.workflows)
                continue;
            for (const [filename, content] of Object.entries(profile.workflows(PLUGIN_VERSION))) {
                writer.write(join(workflowsDir, filename), content);
            }
        }
    }
    const skillSections = getPortableSkillSections(profiles.map((p) => p.id));
    if (skillSections.length > 0) {
        ensureDir(workflowsDir);
        for (const section of skillSections) {
            const filename = section.title
                .toLowerCase()
                .replace(/[^a-z0-9]+/g, '-')
                .replace(/-$/, '') + '.md';
            writer.write(join(workflowsDir, filename), `<!-- setup-agents: ${PLUGIN_VERSION} -->\n${section.body}`);
        }
    }
}
//# sourceMappingURL=agentforce-setup.js.map