"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.renderForm = renderForm;
const tslib_1 = require("tslib");
const ink_1 = require("ink");
const react_1 = tslib_1.__importDefault(require("react"));
const Application_1 = require("../components/Application");
const useKeypress_1 = require("../hooks/useKeypress");
async function renderForm(UserComponent, props, { stdin, stdout, stderr }) {
    let returnedValue;
    const useSubmit = (value) => {
        const { exit } = (0, ink_1.useApp)();
        (0, useKeypress_1.useKeypress)({ active: true }, (ch, key) => {
            if (key.name !== `return`)
                return;
            returnedValue = value;
            exit();
        }, [
            exit,
            value,
        ]);
    };
    const { waitUntilExit } = (0, ink_1.render)(react_1.default.createElement(Application_1.Application, null,
        react_1.default.createElement(UserComponent, { ...props, useSubmit: useSubmit })), {
        stdin: stdin,
        stdout: stdout,
        stderr: stderr,
    });
    await waitUntilExit();
    return returnedValue;
}
