/*
 * 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 { Messages } from '@salesforce/core';
import { Flags, SfCommand } from '@salesforce/sf-plugins-core';
import { buildMetadataIndex } from '../../services/metadata-index/index.js';
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
const messages = Messages.loadMessages('@jterrats/setup-agents', 'setup-agents.index');
const VALID_TYPES = [
    'ApexClass',
    'ApexTrigger',
    'LightningComponentBundle',
    'CustomObject',
    'CustomField',
    'Flow',
    'PermissionSet',
    'CustomLabel',
];
export default class Index extends SfCommand {
    static summary = messages.getMessage('summary');
    static description = messages.getMessage('description');
    static examples = messages.getMessages('examples');
    static flags = {
        full: Flags.boolean({
            summary: messages.getMessage('flags.full.summary'),
            description: messages.getMessage('flags.full.description'),
            default: false,
        }),
        format: Flags.string({
            summary: messages.getMessage('flags.format.summary'),
            options: ['json', 'md'],
            default: 'json',
        }),
        include: Flags.string({
            summary: messages.getMessage('flags.include.summary'),
            description: messages.getMessage('flags.include.description'),
        }),
        exclude: Flags.string({
            summary: messages.getMessage('flags.exclude.summary'),
            description: messages.getMessage('flags.exclude.description'),
        }),
        'source-dir': Flags.string({
            summary: messages.getMessage('flags.source-dir.summary'),
            default: 'force-app',
        }),
    };
    async run() {
        const { flags } = await this.parse(Index);
        const cwd = process.cwd();
        const mode = flags.full ? 'full' : 'git-diff';
        const format = flags.format ?? 'json';
        const sourceDir = flags['source-dir'] ?? 'force-app';
        const include = this.parseTypeList(flags.include);
        const exclude = this.parseTypeList(flags.exclude);
        this.log(messages.getMessage('info.starting', [mode, sourceDir]));
        const { index, outputPath } = buildMetadataIndex(cwd, { sourceDir, mode, include, exclude, format }, (msg) => this.warn(msg));
        this.log(messages.getMessage('info.complete', [
            String(index.stats.totalEntries),
            String(index.stats.totalDependencies),
            String(index.stats.orphanEntries),
        ]));
        this.log(messages.getMessage('info.output', [outputPath]));
        return {
            outputPath,
            stats: index.stats,
            mode: index.mode,
            sourceRoot: index.sourceRoot,
            cwd,
        };
    }
    parseTypeList(value) {
        if (!value)
            return undefined;
        const types = value.split(',').map((t) => t.trim());
        const invalid = types.filter((t) => !VALID_TYPES.includes(t));
        if (invalid.length > 0) {
            this.warn(messages.getMessage('warn.invalidTypes', [invalid.join(', '), VALID_TYPES.join(', ')]));
        }
        return types.filter((t) => VALID_TYPES.includes(t));
    }
}
//# sourceMappingURL=index.js.map