"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.configurationRepository = exports.GlobalConfiguration = void 0;

var _lodash = _interopRequireDefault(require("lodash"));

var _logger = require("../logger");

var _repository = require("./repository");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

class ConfigurationRepository extends (0, _repository.repository)({
  tableName: 'configuration',
  primaryColumnName: 'id',
  booleanColumnNames: ['enableRegistration']
}) {
  async update(value) {
    GlobalConfiguration.update(value);
    return await super.update(value);
  }

}

const configurationRepository = new ConfigurationRepository();
exports.configurationRepository = configurationRepository;

class GlobalConfiguration {
  static _configuration = {
    enableRegistration: true
  };
  static listeners = [];

  static update(value) {
    const previousConfiguration = _lodash.default.cloneDeep(this._configuration);

    this._configuration = { ...this._configuration,
      ...value
    };
    value && this.listeners.forEach(async ({
      key,
      handler
    }) => {
      if (key in value && value[key] !== previousConfiguration[key]) {
        try {
          await handler(value[key], previousConfiguration[key]);
        } catch (error) {
          _logger.logger.error(error);
        }
      }
    });
  }

  static get() {
    return this._configuration;
  }

  static get configuration() {
    return this._configuration;
  }

  static set configuration(value) {
    this._configuration = value;
  }

  static subscribe(key, handler) {
    this.listeners.push({
      key: key,
      handler: handler
    });
  }

}

exports.GlobalConfiguration = GlobalConfiguration;
//# sourceMappingURL=globalSettings.js.map