/**
 * Copyright 2016 Open Permissions Platform Coalition
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy
 * of the License at http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

'use strict';
import defaultsDeep from 'lodash.defaultsdeep';
import 'isomorphic-fetch';
import {parseResponse} from './helper';

getOrganisation.cache = {};
/**
 * Get an organisation using it's ID
 */
function getOrganisation(result, orgUrl, defaults) {
  if (!result.organisation_id) { return Promise.resolve(result); }

  const orgId = result.organisation_id;

  let request = getOrganisation.cache[orgId];
  if (!request) {
    request = fetch(`${orgUrl}/${orgId}`)
      .then(parseResponse);

    getOrganisation.cache[orgId] = request;
  }

  return request.then(response => defaultsDeep({}, response.data, result, defaults));
}

export default {
  /**
   * Parse a source id/source id type to get licensor information
   */
  parseLink: function (id, options={}) {
    const headers = new Headers();
    headers.append('Content-Type', 'application/json');
    const init = {
      method: 'POST',
      headers: headers,
      body: JSON.stringify(id)
    };

    const linksUrl = `${options.accounts}/links`;
    return fetch(linksUrl, init)
      .then(parseResponse)
      .then(response => Promise.all(response.data.map(link =>
        getOrganisation(link, `${options.accounts}/organisations`, options.defaults))))
  },

  /**
   * Parse a set of source_ids to get reference links
   */
  parseLinks: function (ids, options={}) {
    return Promise.all(ids.map(id => this.parseLink(id, options)))
      .then(values => {
        let links = [].concat.apply([], values);
        return Promise.resolve(links)
      });
  }
};

@@ -740,515 +740,55 @@
 r';%0A
-%0AgetOrganisation.cache = %7B%7D;%0A/**%0A * Get an organisation using it's ID%0A */%0Afunction getOrganisation(result, orgUrl, defaults) %7B%0A  if (!result.organisation_id) %7B return Promise.resolve(result); %7D%0A%0A  const orgId = result.organisation_id;%0A%0A  let request = getOrganisation.cache%5BorgId%5D;%0A  if (!request) %7B%0A    request = fetch(%60$%7BorgUrl%7D/$%7BorgId%7D%60)%0A      .then(parseResponse);%0A%0A    getOrganisation.cache%5BorgId%5D = request;%0A  %7D%0A%0A  return request.then(response =%3E defaultsDeep(%7B%7D, response.data, result, defaults));%0A%7D
+import %7BgetOrganisation%7D from './organisation';
 %0A%0Aex
@@ -1250,28 +1250,104 @@
 onse =%3E 
-Promise.all(
+%7B%0A        const orgUrl =  %60$%7Boptions.accounts%7D/organisations%60;%0A        const promises = 
 response
@@ -1363,16 +1363,18 @@
 (link =%3E
+ %7B
 %0A       
@@ -1374,16 +1374,25 @@
         
+  return 
 getOrgan
@@ -1407,67 +1407,183 @@
 link
-, %60$%7Boptions.accounts%7D/organisations%60, options.defaults))))
+.organisation_id,orgUrl)%0A            .then(response =%3E defaultsDeep(response.data, link, options.defaults %7C%7C %7B%7D));%0A        %7D);%0A%0A        return Promise.all(promises);%0A      %7D);
 %0A  %7D
@@ -1786,30 +1786,8 @@
 s =%3E
- %7B%0A        let links =
  %5B%5D.
@@ -1814,55 +1814,8 @@
 ues)
-;%0A        return Promise.resolve(links)%0A      %7D
 );%0A 

Refactor link.js to use organisation/getOrganisation