"use strict";
/**
 * 🚀 Share Command - Universal .faf Distribution System
 * Secure sharing of .faf files with automatic sanitization
 */
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.shareCommand = shareCommand;
const championship_style_1 = require("../utils/championship-style");
const fs = __importStar(require("fs/promises"));
const path = __importStar(require("path"));
const yaml_1 = require("../fix-once/yaml");
/**
 * Share .faf file with automatic sanitization
 */
async function shareCommand(fafPath, options = {}) {
    try {
        const startTime = Date.now();
        console.log(championship_style_1.FAF_COLORS.fafCyan(`${championship_style_1.FAF_ICONS.rocket} FAF Universal Sharing`));
        console.log(`${championship_style_1.FAF_COLORS.fafCyan('├─ ')}Secure distribution with auto-sanitization`);
        // Find .faf file
        const resolvedPath = await findFafFile(fafPath);
        // Load and parse .faf
        const fafContent = await fs.readFile(resolvedPath, 'utf-8');
        const fafData = (0, yaml_1.parse)(fafContent);
        // Sanitize for sharing
        const sanitizedData = options.private ? fafData : await sanitizeFafData(fafData, options);
        // Generate shareable version
        const shareResult = await generateShareableVersion(sanitizedData, options);
        // Display results
        await displayShareResults(shareResult, options);
        const duration = Date.now() - startTime;
        console.log();
        console.log(championship_style_1.FAF_COLORS.fafGreen(`${championship_style_1.FAF_ICONS.trophy} Share ready in ${duration}ms!`));
        console.log(`${championship_style_1.FAF_COLORS.fafCyan(`${championship_style_1.FAF_ICONS.magic_wand} Pro tip: `)}Add --anonymous to remove author info`);
    }
    catch (error) {
        console.error(championship_style_1.FAF_COLORS.fafOrange(`${championship_style_1.FAF_ICONS.shield} Share failed: ${error instanceof Error ? error.message : String(error)}`));
        process.exit(1);
    }
}
/**
 * Find .faf file in current directory or specified path
 */
async function findFafFile(fafPath) {
    if (fafPath) {
        return path.resolve(fafPath);
    }
    const currentDir = process.cwd();
    const defaultPath = path.join(currentDir, '.faf');
    try {
        await fs.access(defaultPath);
        return defaultPath;
    }
    catch {
        throw new Error('No .faf file found. Run `faf init` first or specify a path.');
    }
}
/**
 * Sanitize .faf data for public sharing
 */
async function sanitizeFafData(fafData, options) {
    const sanitized = JSON.parse(JSON.stringify(fafData)); // Deep clone
    // Remove sensitive information
    const sensitiveFields = [
        'ai_instructions.warnings',
        'preferences.commit_style',
        'human_context.who',
        'human_context.where',
        'human_context.when',
        'project.generated',
        'ai_scoring_details.system_date'
    ];
    sensitiveFields.forEach(field => {
        const keys = field.split('.');
        let current = sanitized;
        for (let i = 0; i < keys.length - 1; i++) {
            if (current[keys[i]]) {
                current = current[keys[i]];
            }
            else {
                break;
            }
        }
        if (current && current[keys[keys.length - 1]] !== undefined) {
            delete current[keys[keys.length - 1]];
        }
    });
    // Standardize author info
    if (sanitized.project) {
        sanitized.project.author = 'standard@faf.one';
    }
    // Remove author info if anonymous
    if (options.anonymous) {
        delete sanitized.project?.author;
        delete sanitized.human_context?.who;
    }
    // Add sharing metadata
    sanitized.sharing_metadata = {
        shared_at: new Date().toISOString(),
        shared_by: options.anonymous ? 'anonymous' : 'faf-cli',
        sanitized: !options.private,
        description: options.description || 'Shared .faf context',
        expires: calculateExpiration(options.expires || '7d'),
        format_version: '2.7.0'
    };
    return sanitized;
}
/**
 * Generate shareable version in requested format
 */
async function generateShareableVersion(sanitizedData, options) {
    const format = options.format || 'yaml';
    switch (format) {
        case 'json':
            return {
                format: 'json',
                content: JSON.stringify(sanitizedData, null, 2),
                filename: `shared-context-${generateShareId()}.json`,
                size: JSON.stringify(sanitizedData).length
            };
        case 'url':
            // For demo purposes - in production this would upload to a sharing service
            return {
                format: 'url',
                content: `https://fafcli.dev/share/${generateShareId()}`,
                filename: 'shareable-url.txt',
                size: sanitizedData ? JSON.stringify(sanitizedData).length : 0
            };
        default: // yaml
            const yamlContent = (0, yaml_1.stringify)(sanitizedData, { indent: 2 });
            return {
                format: 'yaml',
                content: yamlContent,
                filename: `shared-context-${generateShareId()}.faf`,
                size: yamlContent.length
            };
    }
}
/**
 * Display sharing results
 */
async function displayShareResults(shareResult, options) {
    console.log();
    console.log(`┌─────────────────────────────────────────┐`);
    console.log(`│ ${championship_style_1.FAF_COLORS.fafCyan('📤 Share Results')}                     │`);
    console.log(`└─────────────────────────────────────────┘`);
    console.log();
    console.log(`${championship_style_1.FAF_COLORS.fafCyan('├─ ')}${championship_style_1.FAF_COLORS.fafGreen('Format:')} ${shareResult.format.toUpperCase()}`);
    console.log(`${championship_style_1.FAF_COLORS.fafCyan('├─ ')}${championship_style_1.FAF_COLORS.fafGreen('Size:')} ${formatBytes(shareResult.size)}`);
    console.log(`${championship_style_1.FAF_COLORS.fafCyan('├─ ')}${championship_style_1.FAF_COLORS.fafGreen('Filename:')} ${shareResult.filename}`);
    if (options.expires) {
        console.log(`${championship_style_1.FAF_COLORS.fafCyan('├─ ')}${championship_style_1.FAF_COLORS.fafGreen('Expires:')} ${options.expires}`);
    }
    if (options.password) {
        console.log(`${championship_style_1.FAF_COLORS.fafCyan('├─ ')}${championship_style_1.FAF_COLORS.fafGreen('Protected:')} Password required`);
    }
    if (options.anonymous) {
        console.log(`${championship_style_1.FAF_COLORS.fafCyan('├─ ')}${championship_style_1.FAF_COLORS.fafGreen('Anonymous:')} Author info removed`);
    }
    console.log(`${championship_style_1.FAF_COLORS.fafCyan('├─ ')}${championship_style_1.FAF_COLORS.fafGreen('Sanitized:')} ${options.private ? 'No (--private)' : 'Yes'}`);
    console.log();
    console.log(championship_style_1.FAF_COLORS.fafCyan('📋 Shareable Content:'));
    console.log('─'.repeat(50));
    if (shareResult.format === 'url') {
        console.log(championship_style_1.FAF_COLORS.fafGreen(shareResult.content));
    }
    else {
        // Show first few lines of content
        const lines = shareResult.content.split('\n').slice(0, 15);
        lines.forEach(line => {
            console.log(championship_style_1.FAF_COLORS.fafCyan('   ') + line);
        });
        if (shareResult.content.split('\n').length > 15) {
            console.log(championship_style_1.FAF_COLORS.fafOrange('   ... (truncated for display)'));
        }
    }
    console.log('─'.repeat(50));
    // Save to file
    const outputPath = path.join(process.cwd(), shareResult.filename);
    await fs.writeFile(outputPath, shareResult.content, 'utf-8');
    console.log();
    console.log(`${championship_style_1.FAF_COLORS.fafGreen('☑️ Saved to:')} ${outputPath}`);
}
/**
 * Calculate expiration date
 */
function calculateExpiration(expires) {
    const now = new Date();
    const match = expires.match(/(\d+)([hdw])/);
    if (!match) {
        return new Date(now.getTime() + 7 * 24 * 60 * 60 * 1000).toISOString(); // Default 7 days
    }
    const value = parseInt(match[1]);
    const unit = match[2];
    let milliseconds = 0;
    switch (unit) {
        case 'h':
            milliseconds = value * 60 * 60 * 1000;
            break;
        case 'd':
            milliseconds = value * 24 * 60 * 60 * 1000;
            break;
        case 'w':
            milliseconds = value * 7 * 24 * 60 * 60 * 1000;
            break;
    }
    return new Date(now.getTime() + milliseconds).toISOString();
}
/**
 * Generate unique share ID
 */
function generateShareId() {
    return Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
}
/**
 * Format bytes to human readable
 */
function formatBytes(bytes) {
    if (bytes === 0) {
        return '0 Bytes';
    }
    const k = 1024;
    const sizes = ['Bytes', 'KB', 'MB'];
    const i = Math.floor(Math.log(bytes) / Math.log(k));
    return `${parseFloat((bytes / Math.pow(k, i)).toFixed(2))} ${sizes[i]}`;
}
//# sourceMappingURL=share.js.map