"use strict";
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;
};
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
    result["default"] = mod;
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
var react_1 = __importStar(require("react"));
var commons_1 = require("@universal-login/commons");
var EmojiPlaceholders_1 = require("./EmojiPlaceholders");
var EmojiInput_1 = require("./EmojiInput");
exports.EmojiForm = function (_a) {
    var hideHeader = _a.hideHeader, className = _a.className, notifications = _a.notifications, onDenyClick = _a.onDenyClick, setPublicKey = _a.setPublicKey;
    var _b = __read(react_1.useState([]), 2), enteredCode = _b[0], setEnteredCode = _b[1];
    var _c = __read(react_1.useState(undefined), 2), soleAddress = _c[0], setSoleAddress = _c[1];
    react_1.useEffect(function () { return updateSoleAddress(commons_1.filterNotificationByCodePrefix(notifications, enteredCode)); }, [notifications, enteredCode]);
    var isInputValid = commons_1.isValidCode(enteredCode, soleAddress);
    react_1.useEffect(function () {
        if (isInputValid) {
            hideHeader && hideHeader();
            commons_1.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_1.default.createElement("div", { id: "emojis" }, isInputValid ? (react_1.default.createElement(react_1.default.Fragment, null,
        react_1.default.createElement("div", { className: "correct-input" },
            react_1.default.createElement("p", { className: "correct-input-title" }, "Correct!"),
            react_1.default.createElement(EmojiPlaceholders_1.EmojiPlaceholders, { enteredCode: enteredCode, publicKey: soleAddress, className: className })),
        react_1.default.createElement("p", { className: "correct-input-confirmation-text" }, "Confirm connecting new device"))) : (react_1.default.createElement("div", { className: "approve-device-form" },
        react_1.default.createElement(EmojiInput_1.EmojiInput, { value: enteredCode, onChange: setEnteredCode, publicKey: soleAddress, className: className }),
        react_1.default.createElement("div", { className: "emojis-form-reject-wrapper" },
            react_1.default.createElement("button", { className: "emojis-form-reject", id: "reject", onClick: onDenyClick }, "Deny"))))));
};
//# sourceMappingURL=EmojiForm.js.map