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 __spread = (this && this.__spread) || function () {
    for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i]));
    return ar;
};
import { WalletSuggestionAction } from '@universal-login/commons';
export var getSuggestion = function (suggestions, actions, source) {
    var filteredCreations = actions.includes(WalletSuggestionAction.create) ? suggestions.creations : [];
    var filteredConnections = actions.includes(WalletSuggestionAction.connect) ? suggestions.connections : [];
    if (source.length > 0 && source.length < 3) {
        return { kind: 'KeepTyping' };
    }
    else if (isTakenOrInvalid(actions, filteredCreations, source)) {
        return { kind: 'TakenOrInvalid' };
    }
    else if (isInvalidForSuggestion(actions, filteredConnections, source)) {
        return { kind: 'InvalidForConnection' };
    }
    else if (isNone(filteredCreations, filteredConnections, source)) {
        return { kind: 'None' };
    }
    else if (isSingleCreation(filteredCreations, filteredConnections)) {
        return { kind: 'Creation', name: filteredCreations[0] };
    }
    else if (isSingleConnection(filteredCreations, filteredConnections)) {
        return { kind: 'Connection', name: filteredConnections[0] };
    }
    else {
        return {
            kind: 'Available',
            suggestions: __spread(filteredCreations.map(function (name) { return ({ kind: 'Creation', name: name }); }), filteredConnections.map(function (name) { return ({ kind: 'Connection', name: name }); })),
        };
    }
};
var isSingleCreation = function (creations, connections) {
    return creations.length === 1 && connections.length === 0;
};
var isSingleConnection = function (creations, connections) {
    return creations.length === 0 && connections.length === 1;
};
var isNone = function (creations, connections, source) {
    return source.length === 0 || (creations.length === 0 && connections.length === 0);
};
var isTakenOrInvalid = function (actions, creations, source) {
    var isOnlyCreateAction = actions.includes(WalletSuggestionAction.create) && actions.length === 1;
    return creations.length === 0 && isOnlyCreateAction && source.length > 0;
};
var isInvalidForSuggestion = function (actions, connections, source) {
    var isOnlyConnectAction = actions.includes(WalletSuggestionAction.connect) && !actions.includes(WalletSuggestionAction.create) && actions.length === 1;
    return isOnlyConnectAction && connections.length === 0 && source.length > 0;
};
//# sourceMappingURL=getSuggestion.js.map