"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;
    };
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.OpRollbackCommand = void 0;
const commander_1 = require("commander");
const chalk_1 = __importDefault(require("chalk"));
const fs = __importStar(require("fs-extra"));
const path = __importStar(require("path"));
const OperationExecutor_1 = require("../services/OperationExecutor");
exports.OpRollbackCommand = new commander_1.Command("op:rollback")
    .description("Rollback (undo) a previous operation")
    .argument("<operation-id>", "ID of the operation to rollback")
    .action(async (operationId) => {
    const opsDir = path.join(process.cwd(), ".mycontext", "operations");
    const opPath = path.join(opsDir, `${operationId}.json`);
    if (!(await fs.pathExists(opPath))) {
        // Try loading from operation history files
        const files = await fs.readdir(opsDir).catch(() => []);
        const match = files.find(f => f.includes(operationId));
        if (!match) {
            console.error(chalk_1.default.red(`Operation "${operationId}" not found`));
            process.exit(1);
        }
    }
    const record = await fs.readJson((await fs.pathExists(opPath))
        ? opPath
        : path.join(opsDir, (await fs.readdir(opsDir)).find(f => f.includes(operationId))));
    const op = record.operation || record;
    const executor = new OperationExecutor_1.OperationExecutor();
    const success = await executor.rollback(op);
    if (success) {
        console.log(chalk_1.default.green(`\n  ✓ Rolled back: ${op.id}\n`));
    }
    else {
        console.error(chalk_1.default.red(`\n  ✗ Rollback failed for: ${op.id}\n`));
        process.exit(1);
    }
});
//# sourceMappingURL=op-rollback.js.map