"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.PrivacyManager = void 0;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const os = __importStar(require("os"));
const FORBIDDEN_FIELDS = new Set([
    'source_code', 'file_path', 'file_content', 'file_name',
    'prompt', 'response', 'ai_response', 'repo_name',
    'company_name', 'project_name', 'customer_name',
    'secret', 'secrets', 'api_key', 'api_keys', 'password',
    'token', 'auth_token', 'refresh_token', 'private_key',
    'pii', 'email', 'phone', 'address', 'ssn',
    'environment_vars', 'env_vars', 'stack_trace', 'raw_stack',
    'variable_name', 'function_name', 'api_name', 'endpoint_name',
    'class_name', 'interface_name', 'type_name', 'enum_name',
    'business_logic', 'domain_terms', 'comment_text',
    'error_message', 'log_message', 'db_schema',
    'username', 'display_name', 'handle',
]);
const CELL_DIR = path.join(os.homedir(), '.fivo', 'cell');
const PRIVACY_FILE = path.join(CELL_DIR, 'privacy-mode.json');
const MAX_PAYLOAD_BYTES = 300;
class PrivacyManager {
    mode;
    constructor() {
        this.mode = this.loadMode();
    }
    loadMode() {
        try {
            if (fs.existsSync(PRIVACY_FILE)) {
                const data = JSON.parse(fs.readFileSync(PRIVACY_FILE, 'utf-8'));
                if (data.mode === 'private' || data.mode === 'community' || data.mode === 'organization') {
                    return data.mode;
                }
            }
        }
        catch { }
        return 'private';
    }
    saveMode() {
        try {
            if (!fs.existsSync(CELL_DIR)) {
                fs.mkdirSync(CELL_DIR, { recursive: true });
            }
            fs.writeFileSync(PRIVACY_FILE, JSON.stringify({ mode: this.mode }), 'utf-8');
        }
        catch { }
    }
    getMode() {
        return this.mode;
    }
    setMode(mode) {
        this.mode = mode;
        this.saveMode();
        return this.mode;
    }
    getSharingPreview() {
        return {
            language: 'typescript',
            framework: 'express',
            database: 'postgres',
            workflow: 'jwt_refresh_auth',
            outcome: 'success',
            confidence: 0.94,
            team_size_bucket: '11-50',
            project_type_bucket: 'saas',
            industry_bucket: 'fintech',
        };
    }
    isBlocked(field) {
        return FORBIDDEN_FIELDS.has(field.toLowerCase());
    }
    validatePayload(payload) {
        const blockedFields = [];
        const payloadKeys = Object.keys(payload);
        for (const key of payloadKeys) {
            if (this.isBlocked(key)) {
                blockedFields.push(key);
            }
        }
        if (blockedFields.length > 0) {
            return { valid: false, blockedFields, reason: `Blocked fields found: ${blockedFields.join(', ')}` };
        }
        const payloadStr = JSON.stringify(payload);
        const bytes = Buffer.byteLength(payloadStr, 'utf-8');
        if (bytes > MAX_PAYLOAD_BYTES) {
            return { valid: false, blockedFields: [], reason: `Payload too large: ${bytes} bytes (max ${MAX_PAYLOAD_BYTES})` };
        }
        if (this.mode === 'private') {
            return { valid: false, blockedFields: [], reason: 'Mode is private — no sharing allowed' };
        }
        return { valid: true, blockedFields: [] };
    }
    getMaxPayloadBytes() {
        return MAX_PAYLOAD_BYTES;
    }
    getForbiddenFields() {
        return Array.from(FORBIDDEN_FIELDS).sort();
    }
}
exports.PrivacyManager = PrivacyManager;
//# sourceMappingURL=privacy-manager.js.map