import Dispatcher from '../core/Dispatcher';
import { SET_ALL_PRODUCTS, SET_ALL_PUBLISHER_PRODUCTS, SET_ALL_PUBLISHER_INFO, ADD_TO_LIBRARY, SET_LIBRARY } from '../constants/ActionTypes';
import path from 'path';

export default {
  setLibrary(library) {
    let ownedProducts = [];
    let ownedContents = [];

    library.ownedProducts.items.forEach((product) => {
      ownedProducts.push(path.basename(product.href));
    });

    library.ownedContents.items.forEach((content) => {
      ownedContents.push(path.basename(content.href));
    })
    Dispatcher.dispatch({
      actionType: SET_LIBRARY,
      ownedProducts: ownedProducts,
      ownedContents: ownedContents,
    })
  },
  addToLibrary(contentId) {
    Dispatcher.dispatch({
      actionType: ADD_TO_LIBRARY,
      contentId: contentId,
    })
  },
  setAllProducts(response) {
    let products = {};
    let contents = {};
    let versions = {};
    response.items.forEach((product, index) => {
      products[product.id] = product;
      product.contents.items.forEach((content) => {
        content['productId'] = product.id;
        contents[content.id] = content;
        content.versions.forEach((version) => {
          version['contentId'] = content.id;
          version['name'] = version.version.name + ' ' + version.version.value;
          versions[version.id] = version;
        });
      });
    });

    Dispatcher.dispatch({
      actionType: SET_ALL_PRODUCTS,
      products: products,
      contents: contents,
      versions: versions,
    });
  },
  setAllPublisherProducts(information) {
    let products = {};
    let contents = {};
    let versions = {};

    information.publishers.items.forEach((publisher) => {
      publisher.products.forEach((product) => {
        products[product.id] = product;
        product.contents.items.forEach((content) => {
          content['productId'] = product.id;
          contents[content.id] = content;
          content.versions.forEach((version) => {
            version['contentId'] = content.id;
            version['name'] = version.version.name + ' ' + version.version.value;
            versions[version.id] = version;
          })
        });
      });
    });

    Dispatcher.dispatch({
      actionType: SET_ALL_PUBLISHER_PRODUCTS,
      products: products,
      contents: contents,
      versions: versions,
      information: information,
    });
  },
  setAllPublisherInfo(information) {
    Dispatcher.dispatch({
      actionType: SET_ALL_PUBLISHER_INFO,
      information: information,
    });
  }
}

@@ -1253,37 +1253,8 @@
 '%5D =
- version.version.name + ' ' +
  ver
@@ -2027,37 +2027,8 @@
 '%5D =
- version.version.name + ' ' +
  ver

fix null version issue