var __read = (this && this.__read) || function (o, n) {
    var m = typeof Symbol === "function" && o[Symbol.iterator];
    if (!m) return o;
    var i = m.call(o), r, ar = [], e;
    try {
        while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
    }
    catch (error) { e = { error: error }; }
    finally {
        try {
            if (r && !r.done && (m = i["return"])) m.call(i);
        }
        finally { if (e) throw e.error; }
    }
    return ar;
};
import React, { useEffect, useState } from 'react';
import { filterNotificationByCodePrefix, isValidCode, ensureNotFalsy, } from '@universal-login/commons';
import { EmojiPlaceholders } from './EmojiPlaceholders';
import { EmojiInput } from './EmojiInput';
export var EmojiForm = function (_a) {
    var hideHeader = _a.hideHeader, className = _a.className, notifications = _a.notifications, onDenyClick = _a.onDenyClick, setPublicKey = _a.setPublicKey;
    var _b = __read(useState([]), 2), enteredCode = _b[0], setEnteredCode = _b[1];
    var _c = __read(useState(undefined), 2), soleAddress = _c[0], setSoleAddress = _c[1];
    useEffect(function () { return updateSoleAddress(filterNotificationByCodePrefix(notifications, enteredCode)); }, [notifications, enteredCode]);
    var isInputValid = isValidCode(enteredCode, soleAddress);
    useEffect(function () {
        if (isInputValid) {
            hideHeader && hideHeader();
            ensureNotFalsy(soleAddress, Error, 'No matching keys');
            setPublicKey(soleAddress);
        }
    }, [isInputValid]);
    var updateSoleAddress = function (addresses) {
        if (addresses.length > 1) {
            setSoleAddress(undefined);
        }
        else if (addresses.length === 1) {
            setSoleAddress(addresses[0]);
        }
    };
    return (React.createElement("div", { id: "emojis" }, isInputValid ? (React.createElement(React.Fragment, null,
        React.createElement("div", { className: "correct-input" },
            React.createElement("p", { className: "correct-input-title" }, "Correct!"),
            React.createElement(EmojiPlaceholders, { enteredCode: enteredCode, publicKey: soleAddress, className: className })),
        React.createElement("p", { className: "correct-input-confirmation-text" }, "Confirm connecting new device"))) : (React.createElement("div", { className: "approve-device-form" },
        React.createElement(EmojiInput, { value: enteredCode, onChange: setEnteredCode, publicKey: soleAddress, className: className }),
        React.createElement("div", { className: "emojis-form-reject-wrapper" },
            React.createElement("button", { className: "emojis-form-reject", id: "reject", onClick: onDenyClick }, "Deny"))))));
};
//# sourceMappingURL=EmojiForm.js.map