/*
 * 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 { writeFileSync } from 'node:fs';
import { dirname } from 'node:path';
import { Messages } from '@salesforce/core';
import { Flags, SfCommand } from '@salesforce/sf-plugins-core';
import { ensureDir } from '../../../services/file-writer.js';
import { getTargetPath, getRulesForProfile, loadRegistry, renderForTarget, } from '../../../services/rule-registry.js';
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
const messages = Messages.loadMessages('@jterrats/setup-agents', 'setup-agents.rules.inject');
const TARGETS = ['claude', 'cursor', 'codex', 'vscode', 'windsurf'];
export default class RulesInject extends SfCommand {
    static summary = messages.getMessage('summary');
    static description = messages.getMessage('description');
    static examples = messages.getMessages('examples');
    static flags = {
        target: Flags.string({
            char: 't',
            summary: messages.getMessage('flags.target.summary'),
            description: messages.getMessage('flags.target.description'),
            required: true,
            options: TARGETS,
        }),
        profile: Flags.string({
            char: 'p',
            summary: messages.getMessage('flags.profile.summary'),
            description: messages.getMessage('flags.profile.description'),
        }),
        rule: Flags.string({
            char: 'r',
            summary: messages.getMessage('flags.rule.summary'),
            description: messages.getMessage('flags.rule.description'),
        }),
        'dry-run': Flags.boolean({
            summary: messages.getMessage('flags.dry-run.summary'),
            description: messages.getMessage('flags.dry-run.description'),
            default: false,
        }),
    };
    async run() {
        const { flags } = await this.parse(RulesInject);
        const cwd = process.cwd();
        const target = flags.target;
        const dryRun = flags['dry-run'];
        const allRules = loadRegistry(cwd);
        let rules;
        if (flags.profile) {
            rules = getRulesForProfile(allRules, flags.profile);
        }
        else {
            rules = allRules;
        }
        if (flags.rule) {
            rules = rules.filter((r) => r.id === flags.rule);
        }
        if (rules.length === 0) {
            this.log(messages.getMessage('info.noRules'));
            return { injected: [], dryRun };
        }
        const injected = [];
        for (const rule of rules) {
            const rendered = renderForTarget(rule, target);
            const targetPath = getTargetPath(cwd, rule, target);
            if (dryRun) {
                this.log(messages.getMessage('info.dryRun', [rule.id, targetPath]));
                this.log(rendered);
                this.log('');
            }
            else {
                ensureDir(dirname(targetPath));
                writeFileSync(targetPath, rendered, 'utf-8');
                this.log(messages.getMessage('info.injected', [rule.id, targetPath]));
            }
            injected.push({ id: rule.id, path: targetPath });
        }
        if (dryRun) {
            this.log(messages.getMessage('info.dryRunDone', [String(injected.length), target]));
        }
        else {
            this.log(messages.getMessage('info.done', [String(injected.length), target]));
        }
        return { injected, dryRun };
    }
}
//# sourceMappingURL=inject.js.map