"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.doSync = doSync;
exports.doMigrate = doMigrate;
exports.doCrossProject = doCrossProject;
const utils_1 = require("../utils");
const project = (0, utils_1.getProjectName)();
function doSync(args) {
    (0, utils_1.printHeader)('Cell Sync');
    const sub = args[1] || 'status';
    if (sub === 'status') {
        (0, utils_1.httpGet)(`/sync/status?project=${project}`).then((result) => {
            console.log(utils_1.C.bold('  Sync Status:'));
            console.log(`    Pending: ${result.pending || 0}`);
            console.log(`    Last sync: ${result.lastSync || 'never'}`);
        }).catch(() => (0, utils_1.printWarning)('Failed to get sync status'));
        return;
    }
    if (sub === 'export') {
        (0, utils_1.httpGet)(`/sync/export?project=${project}`).then((result) => {
            if (result.payload) {
                const fs = require('fs');
                const path = require('path');
                const exportFile = path.join(process.cwd(), `.cell/export-${Date.now()}.json`);
                fs.writeFileSync(exportFile, JSON.stringify(result.payload, null, 2));
                (0, utils_1.printSuccess)(`Exported to: ${exportFile}`);
            }
            else {
                (0, utils_1.printWarning)('No data to export');
            }
        }).catch(() => (0, utils_1.printWarning)('Failed to export'));
        return;
    }
    if (sub === 'import') {
        const file = args[2] || '';
        if (!file) {
            (0, utils_1.printWarning)('Usage: cell sync import <file>');
            return;
        }
        const fs = require('fs');
        if (!fs.existsSync(file)) {
            (0, utils_1.printWarning)(`File not found: ${file}`);
            return;
        }
        const payload = JSON.parse(fs.readFileSync(file, 'utf-8'));
        (0, utils_1.httpPost)('/sync/import', { payload, targetProject: project }).then(() => {
            (0, utils_1.printSuccess)('Import completed');
        }).catch(() => (0, utils_1.printWarning)('Failed to import'));
        return;
    }
    // Default: show status
    (0, utils_1.httpGet)(`/sync/status?project=${project}`).then((result) => {
        console.log(JSON.stringify(result, null, 2));
    }).catch(() => (0, utils_1.printWarning)('Failed to get sync info'));
}
function doMigrate() {
    (0, utils_1.printHeader)('Cell Migrate');
    try {
        const { migrateJsonToSqlite } = require('../../../06-memory/database/database');
        const stats = migrateJsonToSqlite();
        (0, utils_1.printSuccess)('Migration completed');
        console.log(`  Patterns: ${stats.patterns}`);
        console.log(`  Scans: ${stats.scans}`);
        console.log(`  Events: ${stats.behaviorEvents}`);
        console.log(`  Total: ${stats.total}`);
    }
    catch (e) {
        (0, utils_1.printWarning)('Migration failed: ' + String(e));
    }
}
function doCrossProject(args) {
    (0, utils_1.printHeader)('Cell Cross-Project');
    const sub = args[1] || 'list';
    if (sub === 'list') {
        (0, utils_1.httpGet)('/cross-project/list').then((result) => {
            if (result.patterns && result.patterns.length > 0) {
                for (const p of result.patterns.slice(0, 10)) {
                    console.log(`  [${p.category}] ${p.rule}`);
                    (0, utils_1.printDim)(`    Project: ${p.project} | Language: ${p.language}`);
                }
            }
            else {
                (0, utils_1.printDim)('No cross-project patterns');
            }
        }).catch(() => (0, utils_1.printWarning)('Failed to list cross-project patterns'));
        return;
    }
    if (sub === 'share') {
        const category = args[2] || '';
        const rule = args[3] || '';
        if (!category || !rule) {
            (0, utils_1.printWarning)('Usage: cell cross-project share <category> <rule>');
            return;
        }
        (0, utils_1.httpPost)('/cross-project/share', {
            project,
            category,
            rule,
            language: args[4] || 'unknown',
        }).then(() => {
            (0, utils_1.printSuccess)('Pattern shared across projects');
        }).catch(() => (0, utils_1.printWarning)('Failed to share pattern'));
        return;
    }
    if (sub === 'insights') {
        (0, utils_1.httpGet)(`/cross-project/insights?project=${project}`).then((result) => {
            console.log(JSON.stringify(result, null, 2));
        }).catch(() => (0, utils_1.printWarning)('Failed to get cross-project insights'));
        return;
    }
    // Default: list
    (0, utils_1.httpGet)('/cross-project/list').then((result) => {
        console.log(JSON.stringify(result, null, 2));
    }).catch(() => (0, utils_1.printWarning)('Failed to get cross-project data'));
}
//# sourceMappingURL=sync.js.map