{"size": 2279, "ext": "js", "max_stars_repo_path": "src/main/modules/config.js", "max_stars_repo_name": "DrEVILish/PlexTunes", "max_stars_repo_stars_event_min_datetime": "2019-10-13T15:55:43.000Z", "max_stars_repo_stars_event_max_datetime": "2019-11-06T23:44:53.000Z", "max_issues_repo_path": "src/main/modules/config.js", "max_issues_repo_name": "DrEVILish/PlexTunes", "max_issues_repo_issues_event_min_datetime": "2020-07-17T03:37:34.000Z", "max_issues_repo_issues_event_max_datetime": "2021-05-08T23:42:06.000Z", "max_forks_repo_path": "src/main/modules/config.ts", "max_forks_repo_name": "Akintunde102/simpleExplorer", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 2, "max_issues_count": 4, "max_forks_count": null, "avg_line_length": 23.0202020202, "max_line_length": 88, "alphanum_fraction": 0.6138657306}
/**
 * Essential module for creating/loading the app config
 */

import * as electron from 'electron';
import * as path from 'path';
import teeny from 'teeny-conf';

import Module from './module';
import { Config, Repeat, SortBy, SortOrder } from '../../shared/types/interfaces';

const { app } = electron;

class ConfigModule extends Module {
  protected workArea: Electron.Rectangle;
  protected conf: teeny | undefined;

  constructor () {
    super();

    this.workArea = electron.screen.getPrimaryDisplay().workArea;
  }

  async load () {
    const defaultConfig: Record<string, any> = this.getDefaultConfig();
    const pathUserData = app.getPath('userData');

    this.conf = new teeny(path.join(pathUserData, 'config.json'), defaultConfig);

    // Check if config update
    let configChanged = false;

    (Object.keys(defaultConfig) as (keyof Config)[]).forEach((key) => {
      if (this.conf && this.conf.get(key) === undefined) {
        this.conf.set(key, defaultConfig[key]);
        configChanged = true;
      }
    });

    // save config if changed
    if (configChanged) this.conf.save();
  }

  getDefaultConfig (): Config {
    const config: Config = {
      theme: 'light',
      audioVolume: 1,
      audioPlaybackRate: 1,
      audioOutputDevice: 'default',
      audioMuted: false,
      audioShuffle: false,
      audioRepeat: Repeat.NONE,
      librarySort: {
        by: SortBy.ARTIST,
        order: SortOrder.ASC
      },
      // musicFolders: [],
      sleepBlocker: false,
      autoUpdateChecker: true,
      minimizeToTray: false,
      displayNotifications: true,
      bounds: {
        width: 1000,
        height: 600,
        x: Math.round(this.workArea.width / 2),
        y: Math.round(this.workArea.height / 2)
      }
    };

    return config;
  }

  getConfig (): Config {
    if (!this.conf) {
      throw (new Error('Config not loaded'));
    }

    return this.conf.get() as Config; // Maybe possible to type TeenyConf with Generics?
  }

  get (key: keyof Config) {
    if (!this.conf) {
      throw (new Error('Config not loaded'));
    }

    return this.conf.get(key);
  }

  reload () {
    if (!this.conf) {
      throw (new Error('Config not loaded'));
    }

    this.conf.reload();
  }
}

export default ConfigModule;
