"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.addWorkerCommand = addWorkerCommand;
exports.addWorkersCommand = addWorkersCommand;
exports.addTunnelCommand = addTunnelCommand;
exports.addFilesCommand = addFilesCommand;
exports.addCloudWorkerCommand = addCloudWorkerCommand;
const worker_1 = require("../worker");
const tunnel_1 = require("../tunnel");
const fileSync_1 = require("../fileSync");
const workerRegistry_1 = require("../workerRegistry");
function addWorkerCommand(program) {
    program
        .command("worker")
        .description("Start worker process and optionally register current directory")
        .option("--register", "Register current directory as a worker path")
        .option("--share", "Share this worker with your organization (allows other users to use it)")
        .option("--unshare", "Make this worker private (only you can use it)")
        .option("--sandbox", "Run worker in a Docker container for isolation")
        .option("--no-sandbox", "Run worker directly on host (disable sandbox mode)")
        .option("--passkey", "Set up passkey authentication for this worker")
        .option("--passkey-reset", "Remove passkey authentication requirement")
        .action(async (options) => {
        const { setupServices } = await Promise.resolve().then(() => __importStar(require("./services")));
        await setupServices();
        await (0, worker_1.worker)(options);
    });
}
function addWorkersCommand(program) {
    program
        .command("workers")
        .description("Manage and start all registered workers")
        .option("--list", "List all registered worker paths")
        .option("--unregister <path>", "Unregister a worker path")
        .option("--clear", "Clear all registered worker paths")
        .action(async (options) => {
        try {
            if (options.list) {
                const workers = await (0, workerRegistry_1.listWorkerPaths)();
                if (workers.length === 0) {
                    console.log("No workers registered.");
                    console.log("\nTo register a worker, run 'knowhow worker --register' from the worker directory.");
                }
                else {
                    console.log(`Registered workers (${workers.length}):`);
                    workers.forEach((workerPath, index) => {
                        console.log(`  ${index + 1}. ${workerPath}`);
                    });
                }
                return;
            }
            if (options.unregister) {
                await (0, workerRegistry_1.unregisterWorkerPath)(options.unregister);
                return;
            }
            if (options.clear) {
                await (0, workerRegistry_1.clearWorkerRegistry)();
                return;
            }
            const { setupServices } = await Promise.resolve().then(() => __importStar(require("./services")));
            await setupServices();
            await (0, workerRegistry_1.startAllWorkers)();
        }
        catch (error) {
            console.error("Error managing workers:", error);
            process.exit(1);
        }
    });
}
function addTunnelCommand(program) {
    program
        .command("tunnel")
        .description("Start a minimal worker with tunnel enabled: exposes local ports to the cloud. " +
        "Registers essential tools (unlock, lock, listAllowedPorts) so the backend is aware of the worker and ports. " +
        "If passkey auth is configured, the tunnel is locked until unlocked via tool call or WebSocket auth protocol.")
        .option("--share", "Share this tunnel with your organization (allows other users to use it)")
        .option("--unshare", "Make this tunnel private (only you can use it)")
        .action(async (options) => {
        console.log("🌐 Starting tunnel (minimal worker) mode...");
        console.log(`   Tools: ${tunnel_1.TUNNEL_MINIMAL_TOOLS.join(", ")}`);
        await (0, worker_1.worker)({
            ...options,
            allowedTools: tunnel_1.TUNNEL_MINIMAL_TOOLS,
        });
    });
}
function addFilesCommand(program) {
    program
        .command("files")
        .description("Sync files between local filesystem and Knowhow FS (uses fileMounts config)")
        .option("--upload", "Force upload direction for all mounts")
        .option("--download", "Force download direction for all mounts")
        .option("--config <path>", "Path to knowhow.json", "./knowhow.json")
        .option("--dry-run", "Print what would be synced without doing it")
        .action(async (options) => {
        try {
            await (0, fileSync_1.fileSync)(options);
        }
        catch (error) {
            console.error("Error syncing files:", error);
            process.exit(1);
        }
    });
}
function addCloudWorkerCommand(program) {
    program
        .command("cloudworker")
        .description("Create or sync a cloud worker with your local knowhow config")
        .option("--init", "Initialize config.files entries based on what exists in .knowhow/ (run once before --push)")
        .option("--create", "Create a new cloud worker with synced config and files")
        .option("--push <uid>", "Push/sync local config and files to an existing cloud worker")
        .option("--pull <id>", "Pull the latest workerConfigJson from a cloud worker and update local config")
        .option("--name <name>", "Name for the cloud worker (used with --create)")
        .option("--dry-run", "Print what would be synced without doing it")
        .action(async (options) => {
        try {
            const { cloudWorker, pullCloudWorkerConfig, initCloudWorker } = await Promise.resolve().then(() => __importStar(require("../cloudWorker")));
            if (options.init) {
                await initCloudWorker({ dryRun: options.dryRun });
                return;
            }
            if (options.pull) {
                await pullCloudWorkerConfig({ id: options.pull });
            }
            else {
                await cloudWorker(options);
            }
        }
        catch (error) {
            console.error("Error running cloudworker:", error);
            process.exit(1);
        }
    });
}
//# sourceMappingURL=workers.js.map