import { BaseExecutionContext } from '../../execution/base.js';
export class RemoteExecutionContext extends BaseExecutionContext {
    id;
    environment = 'remote';
    target;
    connected = false;
    capabilities;
    commandHandler;
    constructor(options) {
        super();
        this.id = options.id;
        this.target = options.target;
        this.capabilities = options.capabilities ?? ['network'];
        this.commandHandler = options.commandHandler;
    }
    async doInitialize() {
        this.validateTarget(this.target);
        this.log.info(`Remote context initialized for ${this.target.type}://${this.target.host}`);
    }
    async doTeardown() {
        if (this.connected) {
            this.connected = false;
            this.emit({
                type: 'remote_disconnected',
                contextId: this.id,
                host: this.target.host,
            });
        }
    }
    async connect() {
        this.connected = true;
        this.emit({
            type: 'remote_connected',
            contextId: this.id,
            target: this.target,
        });
        this.log.info(`Remote connected: ${this.target.type}://${this.target.host}:${this.target.port ?? 'default'}`);
    }
    async disconnect() {
        if (!this.connected)
            return;
        this.connected = false;
        this.emit({
            type: 'remote_disconnected',
            contextId: this.id,
            host: this.target.host,
        });
        this.log.info(`Remote disconnected: ${this.target.host}`);
    }
    isConnected() {
        return this.connected;
    }
    getTarget() {
        return { ...this.target };
    }
    getConnectionString() {
        const port = this.target.port ? `:${this.target.port}` : '';
        return `${this.target.type}://${this.target.host}${port}`;
    }
    getCapabilities() {
        return [...this.capabilities];
    }
    hasCapability(cap) {
        return this.capabilities.includes(cap);
    }
    setCommandHandler(handler) {
        this.commandHandler = handler;
    }
    async executeRemote(command, options) {
        if (!this.commandHandler) {
            throw new Error(`No remote command handler configured for context "${this.id}". Set one via setCommandHandler() before calling executeRemote().`);
        }
        if (!this.connected) {
            throw new Error(`Remote context "${this.id}" is not connected. Call connect() first.`);
        }
        return this.commandHandler.executeRemote(command, options);
    }
    toConfig() {
        return {
            id: this.id,
            environment: 'remote',
            target: { ...this.target },
            capabilities: this.capabilities,
        };
    }
    validateTarget(target) {
        if (!target.host) {
            throw new Error('Remote target must have a host');
        }
        if (!['ssh', 'rdp', 'api'].includes(target.type)) {
            throw new Error(`Unsupported remote target type: "${target.type}"`);
        }
    }
}
//# sourceMappingURL=remote.js.map