"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.loadDeveloperStyle = loadDeveloperStyle;
exports.saveDeveloperStyle = saveDeveloperStyle;
exports.loadProjectStyle = loadProjectStyle;
exports.saveProjectStyle = saveProjectStyle;
exports.loadCommunication = loadCommunication;
exports.saveCommunication = saveCommunication;
exports.getStyleProfile = getStyleProfile;
exports.updateDeveloperPreference = updateDeveloperPreference;
exports.addVerificationCommand = addVerificationCommand;
exports.addProductRule = addProductRule;
exports.addProjectConvention = addProjectConvention;
exports.initDefaultStyle = initDefaultStyle;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const database_1 = require("../database/database");
const DEFAULT_DEVELOPER_STYLE = {
    communication: 'Hinglish, simple, direct',
    coding: 'small minimal changes',
    verification: ['npx tsc --noEmit', 'npm test -- --runInBand'],
    privacy: ['never store secrets', 'no browser tracking', 'no keystroke tracking'],
    product_rules: ['OSS/local separate from cloud', 'unlimited memory, filtered context'],
    preferences: {},
};
const DEFAULT_PROJECT_STYLE = {
    architecture: 'feature-based walls',
    naming: 'kebab-case files, camelCase functions',
    testing: 'jest with maxWorkers: 1',
    stack: ['TypeScript', 'Node.js', 'SQLite', 'Express'],
    conventions: ['require paths from cli/ use ../../XX-wall/', 'run tsc after every change'],
    entry_points: ['src/index.ts'],
    module_system: 'commonjs',
};
function getCellDir(projectDir) {
    return path.join(projectDir, '.cell');
}
function getStyleDir(projectDir) {
    return path.join(getCellDir(projectDir), 'memory', 'style');
}
function ensureDirs(projectDir) {
    const dir = getStyleDir(projectDir);
    if (!fs.existsSync(dir))
        fs.mkdirSync(dir, { recursive: true });
}
function getDeveloperStylePath(projectDir) {
    return path.join(getStyleDir(projectDir), 'developer-style.json');
}
function getProjectStylePath(projectDir) {
    return path.join(getStyleDir(projectDir), 'project-style.json');
}
function getCommunicationPath(projectDir) {
    return path.join(getStyleDir(projectDir), 'communication.md');
}
function loadDeveloperStyle(projectDir) {
    const p = getDeveloperStylePath(projectDir);
    if (!fs.existsSync(p))
        return { ...DEFAULT_DEVELOPER_STYLE, preferences: {} };
    try {
        return JSON.parse(fs.readFileSync(p, 'utf-8'));
    }
    catch {
        return { ...DEFAULT_DEVELOPER_STYLE, preferences: {} };
    }
}
function saveDeveloperStyle(projectDir, style) {
    ensureDirs(projectDir);
    fs.writeFileSync(getDeveloperStylePath(projectDir), JSON.stringify(style, null, 2));
    // Also save to SQLite
    const db = (0, database_1.getDb)();
    const project = path.basename(projectDir);
    db.prepare(`
    INSERT OR REPLACE INTO style_profiles (project, profile_type, profile_json, updated_at)
    VALUES (?, 'developer', ?, datetime('now'))
  `).run(project, JSON.stringify(style));
}
function loadProjectStyle(projectDir) {
    const p = getProjectStylePath(projectDir);
    if (!fs.existsSync(p))
        return { ...DEFAULT_PROJECT_STYLE };
    try {
        return JSON.parse(fs.readFileSync(p, 'utf-8'));
    }
    catch {
        return { ...DEFAULT_PROJECT_STYLE };
    }
}
function saveProjectStyle(projectDir, style) {
    ensureDirs(projectDir);
    fs.writeFileSync(getProjectStylePath(projectDir), JSON.stringify(style, null, 2));
    const db = (0, database_1.getDb)();
    const project = path.basename(projectDir);
    db.prepare(`
    INSERT OR REPLACE INTO style_profiles (project, profile_type, profile_json, updated_at)
    VALUES (?, 'project', ?, datetime('now'))
  `).run(project, JSON.stringify(style));
}
function loadCommunication(projectDir) {
    const p = getCommunicationPath(projectDir);
    if (!fs.existsSync(p))
        return '# Communication Style\n\nHinglish, simple, direct. No over-explanation.';
    return fs.readFileSync(p, 'utf-8');
}
function saveCommunication(projectDir, content) {
    ensureDirs(projectDir);
    fs.writeFileSync(getCommunicationPath(projectDir), content);
}
function getStyleProfile(projectDir) {
    return {
        project: path.basename(projectDir),
        developer: loadDeveloperStyle(projectDir),
        project_style: loadProjectStyle(projectDir),
        updated_at: new Date().toISOString(),
    };
}
function updateDeveloperPreference(projectDir, key, value) {
    const style = loadDeveloperStyle(projectDir);
    style.preferences[key] = value;
    saveDeveloperStyle(projectDir, style);
}
function addVerificationCommand(projectDir, command) {
    const style = loadDeveloperStyle(projectDir);
    if (!style.verification.includes(command)) {
        style.verification.push(command);
        saveDeveloperStyle(projectDir, style);
    }
}
function addProductRule(projectDir, rule) {
    const style = loadDeveloperStyle(projectDir);
    if (!style.product_rules.includes(rule)) {
        style.product_rules.push(rule);
        saveDeveloperStyle(projectDir, style);
    }
}
function addProjectConvention(projectDir, convention) {
    const style = loadProjectStyle(projectDir);
    if (!style.conventions.includes(convention)) {
        style.conventions.push(convention);
        saveProjectStyle(projectDir, style);
    }
}
function initDefaultStyle(projectDir) {
    if (!fs.existsSync(getDeveloperStylePath(projectDir))) {
        saveDeveloperStyle(projectDir, DEFAULT_DEVELOPER_STYLE);
    }
    if (!fs.existsSync(getProjectStylePath(projectDir))) {
        saveProjectStyle(projectDir, DEFAULT_PROJECT_STYLE);
    }
    if (!fs.existsSync(getCommunicationPath(projectDir))) {
        saveCommunication(projectDir, '# Communication Style\n\nHinglish, simple, direct. No over-explanation.');
    }
}
//# sourceMappingURL=style-memory.js.map