"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.CrossProjectSync = void 0;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
class CrossProjectSync {
    store;
    syncPath;
    constructor(store, cellDir) {
        this.store = store;
        this.syncPath = path.join(cellDir, 'cross-project');
        this.ensureDirectory(this.syncPath);
    }
    checkCompatibility(sourcePatterns, targetProjectId) {
        const adapted = [];
        const targetConfig = this.loadProjectConfig(targetProjectId);
        for (const pattern of sourcePatterns) {
            const score = this.computeCompatibility(pattern, targetConfig);
            if (score >= 0.5) {
                adapted.push({
                    original: pattern,
                    adapted: { ...pattern, patternId: `adapted_${pattern.patternId}` },
                    compatibilityScore: score,
                });
            }
        }
        return adapted.sort((a, b) => b.compatibilityScore - a.compatibilityScore);
    }
    generateAdaptedPatterns(sourceProjectId, targetProjectId) {
        const sourceProjectDir = path.join(this.syncPath, sourceProjectId);
        const sourcePatterns = this.getProjectPatterns(sourceProjectId);
        return this.checkCompatibility(sourcePatterns, targetProjectId);
    }
    saveProjectPatterns(projectId) {
        const model = this.store.exportTasteModel();
        const projectDir = path.join(this.syncPath, projectId);
        this.ensureDirectory(projectDir);
        fs.writeFileSync(path.join(projectDir, 'cell.json'), JSON.stringify(model, null, 2), 'utf-8');
    }
    getProjectPatterns(projectId) {
        const projectDir = path.join(this.syncPath, projectId);
        const tastePath = path.join(projectDir, 'cell.json');
        if (!fs.existsSync(tastePath))
            return [];
        try {
            const model = JSON.parse(fs.readFileSync(tastePath, 'utf-8'));
            return model.patterns;
        }
        catch {
            return [];
        }
    }
    getSyncStats() {
        if (!fs.existsSync(this.syncPath))
            return { projects: 0, totalPatterns: 0 };
        const projects = fs.readdirSync(this.syncPath).filter((f) => {
            const stat = fs.statSync(path.join(this.syncPath, f));
            return stat.isDirectory();
        });
        let totalPatterns = 0;
        for (const project of projects) {
            const patterns = this.getProjectPatterns(project);
            totalPatterns += patterns.length;
        }
        return { projects: projects.length, totalPatterns };
    }
    computeCompatibility(pattern, targetConfig) {
        let score = 0.5;
        if (pattern.category === 'variable_declaration' && targetConfig['language'] === 'typescript') {
            score += 0.2;
        }
        if (pattern.category === 'type_annotation' && targetConfig['language'] === 'typescript') {
            score += 0.3;
        }
        if (pattern.category === 'module_system' && targetConfig['module']?.includes('ES')) {
            score += 0.15;
        }
        return Math.min(1, score);
    }
    loadProjectConfig(projectId) {
        const configPath = path.join(this.syncPath, projectId, 'config.json');
        if (fs.existsSync(configPath)) {
            try {
                return JSON.parse(fs.readFileSync(configPath, 'utf-8'));
            }
            catch {
                return {};
            }
        }
        return {};
    }
    ensureDirectory(dir) {
        if (!fs.existsSync(dir)) {
            fs.mkdirSync(dir, { recursive: true });
        }
    }
}
exports.CrossProjectSync = CrossProjectSync;
//# sourceMappingURL=cross-project-sync.js.map