// src/runtime/RemoteRuntimeControl.ts

import type {
  IRuntimeControl,
  ISpawnComponentOptions,
  ISpawnResult,
} from './RuntimeControl';
import type { IMatrixContext } from '../engine/core/IMatrixContext';
import { RequestReply } from '../engine/remoting/RequestReply';

/**
 * Remote implementation of RuntimeControl.
 *
 * Assumes there is a remote runtime control service bound at `mount` that
 * exposes request/reply operations:
 *
 *   - control.Spawn(ISpawnComponentOptions) -> { mount: string }
 *   - control.Destroy({ mount: string })   -> void
 */
export class RemoteRuntimeControl implements IRuntimeControl {
  constructor(
    public readonly name: string,
    public readonly mount: string,
    private readonly context: IMatrixContext
  ) {}

  async spawnComponent(opts: ISpawnComponentOptions): Promise<ISpawnResult> {
    return RequestReply.execute<ISpawnComponentOptions, ISpawnResult>(
      this.context,
      this.mount,
      'control.Spawn',
      opts,
      { timeoutMs: 15_000 }
    );
  }

  async destroyComponent(mount: string): Promise<void> {
    await RequestReply.execute<{ mount: string }, void>(
      this.context,
      this.mount,
      'control.Destroy',
      { mount },
      { timeoutMs: 15_000 }
    );
  }
}
