/**
 * modules/auth/loginTypeSelector.js — Preferencia metadata.loginType por usuario.
 */
(function (g) {
    g.modules = g.modules || {};
    g.modules.auth = g.modules.auth || {};

    var VALID = ['password', 'pin', 'pattern', 'webauthn'];

    g.modules.auth.loginTypeSelector = {
        VALID: VALID,

        normalize: function (t) {
            var x = String(t || 'password').toLowerCase();
            return VALID.indexOf(x) >= 0 ? x : 'password';
        },

        setPreferred: async function (userId, loginType) {
            var store = g.modules.users.userStore;
            var u = await store.findById(userId);
            if (!u) throw new Error('not_found');
            u.metadata = u.metadata || {};
            u.metadata.loginType = this.normalize(loginType);
            await store.upsert(u);
            return u.metadata.loginType;
        },
    };
})(typeof window !== 'undefined' ? window : this);
