import Ember from 'ember';

import config from '../config/environment';

export default Ember.Service.extend({
    host: config.HOST,
    namespace: 'solr',
    core: 'dina',
    
    parseResponse (data) {
        return {
            records: data.response.docs.mapBy('primary_id'),
            totalRows: data.response.numFound
        };
    },
    
    select (query, {entityType, fq, rows}) {
        const service = this;
        let url = `${this.host}/${this.namespace}/${this.core}/select?q=${query}&fl=primary_id&wt=json&indent=true`;

        let fqString = '';
        if (entityType) {
            fqString += `&fq=entity_type%3A${entityType}`;
        }

        for(let key in fq) {
            fqString += `&fq=${key}%3D${fq[key]}`;
        }
        
        url += fqString;
        
        if (rows) {
            url += `&rows=${rows}`;
        }

        return new Ember.RSVP.Promise(function (resolve, reject) {
            Ember.$.getJSON(url).success((data)=>{
                let {records, totalRows} = service.parseResponse(data);
                resolve({records, totalRows});
            }).fail((error)=>{
                reject(error);
            });
        });
    },
    updateIndex () {
        const url = `${this.host}/${this.namespace}/${this.core}/dataimport?command=delta-import&commit=1&wt=json`;
        Ember.$.getJSON(url);
    }
});

@@ -607,31 +607,18 @@
       fq
-String += %60&fq=
+%5B'
 entity_t
@@ -624,13 +624,13 @@
 type
-%253A$%7B
+'%5D = 
 enti
@@ -635,18 +635,16 @@
 tityType
-%7D%60
 ;%0A      
@@ -718,9 +718,9 @@
 y%7D%253
-D
+A
 $%7Bfq

Use colon as separator in solr query