"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.migrateKey = migrateKey;
exports.getMigrationStatus = getMigrationStatus;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const os = __importStar(require("os"));
const key_provider_1 = require("./key-provider");
// ============================================================
// key-migration.ts — Migrate old file keys to new provider
//
// Migration flow:
//   1. Check if new provider has key → done
//   2. Check old file key (~/.fivo/cell/keys/local.key) → migrate
//   3. Check project-local key (.cell/private/.archive.key) → migrate
//   4. Keep old files as backup (don't delete)
// ============================================================
const KEY_ID = 'fivocell-archive';
function getOldGlobalKeyPath() {
    return path.join(os.homedir(), '.fivo', 'cell', 'keys', 'local.key');
}
function getOldProjectKeyPath(projectDir) {
    return path.join(projectDir, '.cell', 'private', '.archive.key');
}
function migrateKey(projectDir) {
    const provider = (0, key_provider_1.getKeyProvider)();
    // 1. Check if provider already has the key
    const existing = (0, key_provider_1.getKey)(KEY_ID);
    if (existing) {
        return {
            migrated: false,
            source: 'os-store',
            provider: provider.name,
            message: `Key already exists in ${provider.name}`,
        };
    }
    // 2. Try old global file key
    const oldGlobalPath = getOldGlobalKeyPath();
    if (fs.existsSync(oldGlobalPath)) {
        try {
            const raw = fs.readFileSync(oldGlobalPath, 'utf-8').trim();
            const key = Buffer.from(raw, 'hex');
            if (key.length === 32) {
                (0, key_provider_1.setKey)(KEY_ID, key);
                return {
                    migrated: true,
                    source: 'global-file',
                    provider: provider.name,
                    message: `Migrated from ${oldGlobalPath} to ${provider.name}`,
                };
            }
        }
        catch { /* corrupt file, continue */ }
    }
    // 3. Try old project-local key
    const oldProjectPath = getOldProjectKeyPath(projectDir);
    if (fs.existsSync(oldProjectPath)) {
        try {
            const raw = fs.readFileSync(oldProjectPath, 'utf-8').trim();
            const key = Buffer.from(raw, 'hex');
            if (key.length === 32) {
                (0, key_provider_1.setKey)(KEY_ID, key);
                return {
                    migrated: true,
                    source: 'project-file',
                    provider: provider.name,
                    message: `Migrated from ${oldProjectPath} to ${provider.name}`,
                };
            }
        }
        catch { /* corrupt file, continue */ }
    }
    // 4. No existing key found — will be created on first use
    return {
        migrated: false,
        source: 'none',
        provider: provider.name,
        message: 'No existing key found. New key will be created on first use.',
    };
}
function getMigrationStatus() {
    const existing = (0, key_provider_1.getKey)(KEY_ID);
    const oldGlobal = fs.existsSync(getOldGlobalKeyPath());
    return {
        newProviderHas: !!existing,
        oldGlobalExists: oldGlobal,
        oldProjectExists: false, // checked per-project
        needsMigration: !existing && oldGlobal,
    };
}
//# sourceMappingURL=key-migration.js.map