// The MIT License (MIT)
//
// Copyright (c) 2021-2024 Camptocamp SA
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import { BehaviorSubject } from 'rxjs';
export var UserState;
(function (UserState) {
    /* eslint-disable no-unused-vars */
    UserState["LOGGED_IN"] = "logged in";
    UserState["LOGGED_OUT"] = "logged out";
    UserState["DISCONNECTED"] = "disconnected";
    UserState["READY"] = "ready";
    UserState["NOT_INITIALIZED"] = "not initialized";
})(UserState || (UserState = {}));
// The name should be t to be collected by i18next
function t(msg) {
    return msg;
}
export const loginMessageRequired = t('Some layers in this link are not accessible to unauthenticated users. ' +
    'Please log in to see whole data.');
/**
 * Object used to expose the login user information.
 *
 * Example of usage:
 * ```js
 *    (window as any).gmfapi.store.user.getProperties().subscribe({
 *      next: (user: User) => {
 *        ...
 *      },
 *    })
 * ```
 */
export class UserModel {
    constructor() {
        this.properties_ = new BehaviorSubject(this.getEmptyUserProperties());
        this.loginMessage_ = new BehaviorSubject('');
        this.state_ = UserState.NOT_INITIALIZED;
    }
    /**
     * @returns the observable user's properties.
     */
    getProperties() {
        return this.properties_;
    }
    /**
     * @returns the current user state.
     */
    getState() {
        return this.state_;
    }
    /**
     * @returns The observable login message.
     */
    getLoginMessage() {
        return this.loginMessage_;
    }
    /**
     * Set the current login message
     *
     * @param messageState The new login message.
     */
    setLoginMessage(messageState) {
        this.loginMessage_.next(messageState);
    }
    /**
     * Set the current User's properties and state.
     *
     * @param properties The new user
     * @param state The new state
     */
    setUser(properties, state) {
        const isValid = this.checkUserProperties_(properties);
        if (!isValid || state === null) {
            return;
        }
        this.state_ = state;
        this.properties_.next(properties);
    }
    /**
     * @returns an empty user.
     */
    getEmptyUserProperties() {
        return {
            ...{
                email: null,
                is_intranet: null,
                functionalities: null,
                is_password_changed: null,
                roles: null,
                username: null,
                otp_key: null,
                otp_uri: null,
                two_factor_totp_secret: null,
            },
        };
    }
    /**
     * Check if the user has at least all required properties.
     *
     * @param properties The new user
     * @returns true if the properties are correct.
     * @private
     */
    checkUserProperties_(properties) {
        if (properties === null || properties === undefined) {
            console.error('New properties of the user must be an object');
            return false;
        }
        let isValid = true;
        const keys = Object.keys(this.getEmptyUserProperties());
        keys.forEach((key) => {
            const newKeys = Object.keys(properties);
            if (!newKeys.includes(key)) {
                console.error(`User is missing property ${key}`);
                isValid = false;
            }
        });
        return isValid;
    }
}
// Export default user instantiated (to use it as a singleton).
const user = new UserModel();
export default user;
//# sourceMappingURL=user.js.map