const app = require('vn-loopback/server/server');
const UserError = require('vn-loopback/util/user-error');

module.exports = function(Self, options) {
    require('../methods/account-synchronizer/test')(Self);

    Self.once('attached', function() {
        app.models.AccountConfig.addSynchronizer(Self);
    });

    /**
     * Mixin for user synchronizers.
     *
     * @property {Array<Model>} $
     * @property {Object} accountConfig
     * @property {Object} mailConfig
     */
    let Mixin = {
        /**
         * Initalizes the synchronizer.
         */
        async init() {},

        /**
         * Deinitalizes the synchronizer.
         */
        async deinit() {},

        /**
         * Get users to synchronize.
         *
         * @param {Set} usersToSync Set where users are added
         */
        async getUsers(usersToSync) {},

        /**
         * Synchronizes a user.
         *
         * @param {Object} info User information
         * @param {String} userName The user name
         * @param {String} password Thepassword
         */
        async syncUser(info, userName, password) {},

        /**
         * Synchronizes user groups.
         *
         * @param {Object} info User information
         * @param {String} userName The user name
         */
        async syncUserGroups(info, userName) {},

        /**
         * Synchronizes roles.
         */
        async syncRoles() {},

        /**
         * Tests synchronizer configuration.
         */
        async test() {
            try {
                await this.init();
                await this.deinit();
            } catch (e) {
                let err = new UserError(e.message);
                err.name = e.name;
                throw err;
            }
        }
    };

    for (let method in Mixin) {
        if (!Self.prototype[method])
            Self.prototype[method] = Mixin[method];
    }
};