<commit_before>
import {EventEmitter} from 'events';

import {Setting} from './index';
import {App} from '../App';

import {Model} from 'sequelize';
import {Instance} from 'sequelize';

export class SettingManager extends EventEmitter {

  private app: App;

  private facade: Setting.Facade;
  private settingModel: Model<any, any>;

  constructor (facade: Setting.Facade) {
    super();

    this.facade = facade;
    this.app = this.facade.app;
  }

  public async loadSettings () {
    return;
  }

  public async startSettings () {
    this.settingModel = this.app.models['Setting'];

    // Save to settings for next start.
    this.app.settings.setSetting(this.app, 'test.key', true);

    this.clearSettings(this.app);
    this.clearSettings(this.app.plugins['@maniajs/plugin-karma']);
    return;
  }

  public async isDefined (context: any): Promise<boolean> {
    let settings = await this.settingModel.findAndCount({
      where: {
        context: { $eq: this.getContextString(context) }
      }
    });
    return settings && settings.count > 0;
  }

  /**
   * Define settings for context (plugin or core/app).
   * @param context
   * @param schema
   */
  public async defineSettings (context: any, schema: SettingSchema) {
    context = this.getContextString(context);

    // Prepare all settings in database.
    for (let setting of schema.settings) {
      setting.value = null;
      await this.settingModel.create(setting);
    }
  }

  /**
   * Get all plugin settings.
   * @param context Plugin class instance or app instance for global.
   */
  public async getAllSettings (context: any): Promise<Setting[]> {
    let settings: any[] = await this.settingModel.find({
      where: { context: { $eq: this.getContextString(context) } }
    });

    return settings.map((setting) => {
      return this.parseSetting(setting);
    });
  }

  /**
   * Set all settings for context. (Will not delete existing and non replacing settings!).
   * @param context Plugin instance or app instance.
   * @param settings
   */
  public async setAllSettings (context: any, settings: Setting[]): Promise<any> {
    for (let setting of settings) {
      await this.replaceSetting(context, setting);
    }
  }

  /**
   * Set setting value.
   * @param context
   * @param key Provide the string key, or if you want to supply the foreignKey too, supply an object with
   *            'key' and 'foreignKey' in it.
   * @param value
   */
  public async setSetting (context: any, key: string | {key: string, foreignKey?: number}, value: any): Promise<any> {
    let where: any = {$and: []};
    where.$and.push({
      context: {$eq: this.getContextString(context)}
    });

    if (typeof key === 'string') {
      where.$and.push({
        key: {$eq: key}
      });
    } else {
      where.$and.push({
        key: {$eq: key.key}
      });
      if (key.foreignKey) {
        where.$and.push({
          foreignKey: {$eq: key.foreignKey}
        });
      }
    }

    let setting = await this.settingModel.findOne({
      where
    });

    if (! setting) {
      throw new Error('Setting key is not yet defined in the context store!');
    }

    setting.set('value', value);
    await setting.save();
  }

  /**
   * Get setting.
   * @param context
   * @param key
   * @param foreignKey
   */
  public async getSetting (context: any, key?: string | {key: string, foreignKey?: number}): Promise<Setting> {
    let where: any = {$and : []};
    where.$and.push({<commit_msg>Adding comments to parameters to explain the contents<commit_after>
import {EventEmitter} from 'events';

import {Setting} from './index';
import {App} from '../App';

import {Model} from 'sequelize';
import {Instance} from 'sequelize';

export class SettingManager extends EventEmitter {

  private app: App;

  private facade: Setting.Facade;
  private settingModel: Model<any, any>;

  constructor (facade: Setting.Facade) {
    super();

    this.facade = facade;
    this.app = this.facade.app;
  }

  public async loadSettings () {
    return;
  }

  public async startSettings () {
    this.settingModel = this.app.models['Setting'];

    // Save to settings for next start.
    this.app.settings.setSetting(this.app, 'test.key', true);

    this.clearSettings(this.app);
    this.clearSettings(this.app.plugins['@maniajs/plugin-karma']);
    return;
  }

  public async isDefined (context: any): Promise<boolean> {
    let settings = await this.settingModel.findAndCount({
      where: {
        context: { $eq: this.getContextString(context) }
      }
    });
    return settings && settings.count > 0;
  }

  /**
   * Define settings for context (plugin or core/app).
   * @param context
   * @param schema
   */
  public async defineSettings (context: any, schema: SettingSchema) {
    context = this.getContextString(context);

    // Prepare all settings in database.
    for (let setting of schema.settings) {
      setting.value = null;
      await this.settingModel.create(setting);
    }
  }

  /**
   * Get all plugin settings.
   * @param context Plugin class instance or app instance for global.
   */
  public async getAllSettings (context: any): Promise<Setting[]> {
    let settings: any[] = await this.settingModel.find({
      where: { context: { $eq: this.getContextString(context) } }
    });

    return settings.map((setting) => {
      return this.parseSetting(setting);
    });
  }

  /**
   * Set all settings for context. (Will not delete existing and non replacing settings!).
   * @param context Plugin instance or app instance.
   * @param settings
   */
  public async setAllSettings (context: any, settings: Setting[]): Promise<any> {
    for (let setting of settings) {
      await this.replaceSetting(context, setting);
    }
  }

  /**
   * Set setting value.
   * @param context Please provide the Plugin instance, or another core instance, this is dependent and will
   *                specify your unique store environment
   * @param key Provide the string key, or if you want to supply the foreignKey too, supply an object with
   *            'key' and 'foreignKey' in it.
   * @param value
   */
  public async setSetting (context: any, key: string | {key: string, foreignKey?: number}, value: any): Promise<any> {
    let where: any = {$and: []};
    where.$and.push({
      context: {$eq: this.getContextString(context)}
    });

    if (typeof key === 'string') {
      where.$and.push({
        key: {$eq: key}
      });
    } else {
      where.$and.push({
        key: {$eq: key.key}
      });
      if (key.foreignKey) {
        where.$and.push({
          foreignKey: {$eq: key.foreignKey}
        });
      }
    }

    let setting = await this.settingModel.findOne({
      where
    });

    if (! setting) {
      throw new Error('Setting key is not yet defined in the context store!');
    }

    setting.set('value', value);
    await setting.save();
  }

  /**
   * Get setting.
   * @param context Please provide the Plugin instance, or another core instance, this is dependent and will
   *                specify your unique store environment
   * @param key Provide the string key, or if you want to supply the foreignKey too, supply an object with
   *            'key' and 'foreignKey' in it.
   */
  public async getSetting (context: any, key?: string | {key: string, foreignKey?: number}): Promise<Setting> {
    let where: any = {$and : []};
    where.$and.push({
{'fraction_non_alphanumeric': 0.12988412826731338, 'fraction_numerical': 0.00026946914578280785, 'mean_word_length': 2.741431451612903, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2336429', 'n_tokens_mistral': 2199, 'n_tokens_neox': 2158, 'n_words': 680}