
var SimpleStore = require('fh-wfm-simple-store'),
  config = require('./config.js');

/**
 * Creates and initializes a store to be used by the app modules.
 * @param {String} dataSetId  String used as an identifier for the store
 * @param {Object|null} seedData  Object to be saved in the store initially
 * @param {Object} mediator Object used for listening to a topic.
 */
function initStore(dataSetId, seedData, mediator) {
  var dataStore = new SimpleStore(dataSetId);

  /**
   * Check if existing data exists. If no existing data is found, initialize with the given seed data. Otherwise,
   * continue to listen to topic.
   *
   * TODO: Check for existing data may be removed in production as applications are likely to start with no
   * initial data. Instead, initialize store with: dataStore.init();
   */
  dataStore.list().then(function(storeData) {
    if (!storeData || storeData.length === 0) {
      //Check if seed data is given, otherwise initialize store with no initial data.
      if (seedData) {
        dataStore.init(seedData).then(function() {
          dataStore.listen(config.get('dataTopicPrefix'), mediator);
        });
      } else {
        dataStore.init().then(function() {
          dataStore.listen(config.get('dataTopicPrefix'), mediator);
        });
      }
    } else {
      dataStore.listen(config.get('dataTopicPrefix'), mediator);
    }
  });
}

module.exports = {
  init: initStore
};


var SimpleStore = require('fh-wfm-simple-store'),
  config = require('./config.js');

/**
 * Creates and initializes a store to be used by the app modules.
 * @param {String} dataSetId  String used as an identifier for the store
 * @param {Object|null} seedData  Object to be saved in the store initially
 * @param {Object} mediator Object used for listening to a topic.
 */
function initStore(dataSetId, seedData, mediator) {
  var dataStore = new SimpleStore(dataSetId);

  /**
   * Check if existing data exists. If no existing data is found, initialize with the given seed data. Otherwise,
   * continue to listen to topic.
   *
   * TODO: Check for existing data may be removed in production as applications are likely to start with no
   * initial data. Instead, initialize store with: dataStore.init();
   */
  dataStore.list().then(function(storeData) {
    if (!storeData || storeData.length === 0) {
      //Check if seed data is given, otherwise initialize store with no initial data.
      //array store needs to be initialized with an empty array if there is no seed data given.
      if (!seedData && process.env.WFM_USE_MEMORY_STORE === "true") {
        seedData = [];
      }

      dataStore.init(seedData).then(function() {
        dataStore.listen(config.get('dataTopicPrefix'), mediator);
      });
    } else {
      dataStore.listen(config.get('dataTopicPrefix'), mediator);
    }
  });
}

module.exports = {
  init: initStore
};

Initialize array store with an empty array