import { mountHostInputForm, } from "../interactive/components/host-input-form-mount.js";
/** Host-side controller that keeps form input/state entirely in the terminal process. */
export class InputFormHostController {
    constructor(runtime, ui) {
        this.mounted = new Map();
        this.runtime = runtime;
        this.ui = ui;
        this.unsubscribe = runtime.onEngineMessage((message) => this.handleMessage(message));
    }
    dispose() {
        this.unsubscribe();
        this.disposeAll();
    }
    handleMessage(message) {
        if (message.type === "engine_ready") {
            this.disposeAll();
            return;
        }
        if (message.type !== "engine_input_form_open")
            return;
        this.open(message.componentId, { title: message.title, fields: message.fields });
    }
    open(componentId, request) {
        if (this.mounted.has(componentId))
            return;
        const mount = mountHostInputForm(this.ui, request, {
            onSubmit: (values) => {
                this.mounted.delete(componentId);
                this.runtime.sendEngineCommand({ type: "engine_input_form_submit", componentId, values });
            },
            onCancel: () => {
                this.mounted.delete(componentId);
                this.runtime.sendEngineCommand({ type: "engine_input_form_cancel", componentId });
            },
        });
        this.mounted.set(componentId, mount);
    }
    disposeAll() {
        for (const mount of this.mounted.values())
            mount.close();
        this.mounted.clear();
    }
}
//# sourceMappingURL=input-form-host.js.map