/*
 * 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 { PROFILE_DOC_SOURCES, listSyncableProfiles, syncProfileDocs, } from '../../../services/docs-sync.js';
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
const messages = Messages.loadMessages('@jterrats/setup-agents', 'setup-agents.docs.sync');
export default class DocsSync extends SfCommand {
    static summary = messages.getMessage('summary');
    static description = messages.getMessage('description');
    static examples = messages.getMessages('examples');
    static flags = {
        profile: Flags.string({
            char: 'p',
            summary: messages.getMessage('flags.profile.summary'),
            description: messages.getMessage('flags.profile.description'),
        }),
        force: Flags.boolean({
            summary: messages.getMessage('flags.force.summary'),
            description: messages.getMessage('flags.force.description'),
            default: false,
        }),
        'max-pages': Flags.integer({
            summary: messages.getMessage('flags.max-pages.summary'),
            description: messages.getMessage('flags.max-pages.description'),
            default: 5,
            min: 1,
            max: 20,
        }),
    };
    async run() {
        const { flags } = await this.parse(DocsSync);
        const cwd = process.cwd();
        const force = flags.force;
        const maxPages = flags['max-pages'];
        const profilesToSync = flags.profile
            ? [flags.profile]
            : listSyncableProfiles();
        const results = [];
        for (const profile of profilesToSync) {
            if (!PROFILE_DOC_SOURCES[profile]) {
                this.log(messages.getMessage('info.no-sources', [profile]));
                continue;
            }
            this.log(messages.getMessage('info.syncing', [profile]));
            // eslint-disable-next-line no-await-in-loop
            const result = await syncProfileDocs(cwd, profile, { force, maxPagesPerSource: maxPages });
            this.logSyncResult(result, profile);
            results.push(result);
        }
        const totalSynced = results.reduce((sum, r) => sum + r.synced, 0);
        const totalFailed = results.reduce((sum, r) => sum + r.failed, 0);
        const totalFiles = results.reduce((sum, r) => sum + r.files.length, 0);
        this.log('');
        this.log(messages.getMessage('info.complete', [totalSynced, totalFailed, totalFiles]));
        return { results, totalSynced, totalFailed, totalFiles };
    }
    logSyncResult(result, profile) {
        if (result.synced > 0) {
            for (const file of result.files)
                this.log(`  ✓ ${file}`);
        }
        if (result.errors.length > 0) {
            for (const err of result.errors)
                this.log(`  ✗ ${err}`);
        }
        if (result.synced === 0 && result.failed === 0) {
            this.log(messages.getMessage('info.skipped', [profile]));
        }
    }
}
//# sourceMappingURL=sync.js.map