'use strict';
var Rx = require('./rx');
var {partitionN} = require('./underscore_ext');

var binSize = 1000 * 1000;

var errors = [];

var docQuery = samples =>  Rx.Observable.ajax({
        url: '/api/ties/documents/list',
        headers: {'Content-Type': 'text/plain' },
        body: JSON.stringify({patientIds: samples}),
        responseType: 'text',
        method: 'POST'
    }).map(({response}) => JSON.parse(response).results).catch(() => {
        errors.push(samples[0]);
        return Rx.Observable.of([]);
    });

var concatBin = next =>
    acc => next.map(r => acc.concat(r));

function chaseTail(bins, req, i = 0, acc) {
    return bins.length === i ? acc :
        chaseTail(bins, req, i + 1,
            acc ? acc.flatMap(concatBin(req(bins[i]))) :
            req(bins[i]));
}

var badSamples = ["TCGA-BH-A0B4", "TCGA-BH-A0AY", "TCGA-BH-A0BV", "TCGA-BH-A0DZ", "TCGA-BH-A0BD"];
var filterSamples = l => l.filter(v => badSamples.indexOf(v) === -1);


// XXX get patient list from server, instead of doing string replace.
var docs = samples =>
    chaseTail(
        partitionN(filterSamples(samples), binSize),
        docQuery);

//    .map(r => object(
//                pluck(r, 'patientId'),
//                pluck(r, 'docs').map(docs => pluck(docs, 'id'))));

var doc = id =>
	Rx.Observable.ajax({
		url: `/api/ties/documents/${id}`,
		headers: {'Content-Type': 'text/plain' },
		responseType: 'text',
		method: 'GET'
	}).map(result => JSON.parse(result.response));

// XXX Currently, no way to limit by sample id?
var matches = (patients, term) =>
	Rx.Observable.ajax({
		url: '/api/ties/query/',
        body: {
            concept: term,
            limit: 1000 * 1000, // try to defeat limit
        },
		responseType: 'text',
		method: 'POST'
	}).map(result => JSON.parse(result.response).results);

var concepts = term =>
	Rx.Observable.ajax({
		url: `/api/ties/search/?term=${encodeURIComponent(term)}`,
		responseType: 'text',
		method: 'GET'
	}).map(result => JSON.parse(result.response));

module.exports = {
	docs,
	doc,
	matches,
    concepts
};

@@ -36,102 +36,8 @@
 x');
-%0Avar %7BpartitionN%7D = require('./underscore_ext');%0A%0Avar binSize = 1000 * 1000;%0A%0Avar errors = %5B%5D;
 %0A%0Ava
@@ -41,21 +41,17 @@
 %0Avar doc
-Query
+s
  = sampl
@@ -335,855 +335,8 @@
 lts)
-.catch(() =%3E %7B%0A        errors.push(samples%5B0%5D);%0A        return Rx.Observable.of(%5B%5D);%0A    %7D);%0A%0Avar concatBin = next =%3E%0A    acc =%3E next.map(r =%3E acc.concat(r));%0A%0Afunction chaseTail(bins, req, i = 0, acc) %7B%0A    return bins.length === i ? acc :%0A        chaseTail(bins, req, i + 1,%0A            acc ? acc.flatMap(concatBin(req(bins%5Bi%5D))) :%0A            req(bins%5Bi%5D));%0A%7D%0A%0Avar badSamples = %5B%22TCGA-BH-A0B4%22, %22TCGA-BH-A0AY%22, %22TCGA-BH-A0BV%22, %22TCGA-BH-A0DZ%22, %22TCGA-BH-A0BD%22%5D;%0Avar filterSamples = l =%3E l.filter(v =%3E badSamples.indexOf(v) === -1);%0A%0A%0A// XXX get patient list from server, instead of doing string replace.%0Avar docs = samples =%3E%0A    chaseTail(%0A        partitionN(filterSamples(samples), binSize),%0A        docQuery);%0A%0A//    .map(r =%3E object(%0A//                pluck(r, 'patientId'),%0A//                pluck(r, 'docs').map(docs =%3E pluck(docs, 'id'))))
 ;%0A%0Av

Drop workaround for bad samples in TIES.