import Ember from 'ember';

export default Ember.Component.extend({

    init () {
        this._super(...arguments);
        let parameters = this.get('parameters');
        this.set('filters', Object.keys(parameters).map((key, idx, arr) => {
            return {
                "key": key,
                "value": parameters[key]
            }
        }).filter((item) => {
            if (item.key == "page") {
                return false;
            }
            return true;
        }));
    },

    actions: {

        removeFilter(filter) {
            let queryParams = {};
            queryParams[filter.key] = undefined;
            this.attrs.transitionToFacet("search", queryParams);
        },

        transitionToFacet(parameter, parameterValue) {
            let queryParams = {};
            queryParams[parameter] = parameterValue;
            this.attrs.transitionToFacet("search", queryParams);
        }

    }

});

import Ember from 'ember';

var ID_FILTERS = ['contributors', 'funders', 'publishers'];

export default Ember.Component.extend({
    filters: null,
    init () {
      this._super(...arguments);
      let parameters = this.get('parameters');
      var filters = Object.keys(parameters).map((key) => {
        return {
          "key": key,
          "value": parameters[key]
        }
      }).filter((item) => {
        return item.key !== "page";
      });

      // Find and format all query params that use ids for the value
      var ids = filters.filter((item) => {
          return ID_FILTERS.indexOf(item.key) !== -1;
      }).map((param) => {
          return {key: param.value, doc_count: 0}
      });

      // Fetch display names
      if (ids.length !== 0) {
        this.fetchAgentDetails(ids).then((data) => {
          for (var i = 0; i < data.length; i++) {
            for (var filter = 0; filter < filters.length; filter++) {
              if (filters[filter].value === data[i].id) {
                  Ember.set(filters[filter], 'value', data[i].name);
              }
            }
          }
        });
      }
      this.set('filters', filters);
    },

    fetchAgentDetails: async function(data) {
        let agent_details = await Ember.$.ajax({
            url: 'https://dev-labs.cos.io/bulk_get_agents',
            crossDomain: true,
            data: JSON.stringify(data),
            type: 'POST',
            contentType: 'application/json'
        });
        return JSON.parse(agent_details);
    },

    actions: {

        removeFilter(filter) {
            let queryParams = {};
            queryParams[filter.key] = undefined;
            this.attrs.transitionToFacet("search", queryParams);
        },

        transitionToFacet(parameter, parameterValue) {
            let queryParams = {};
            queryParams[parameter] = parameterValue;
            this.attrs.transitionToFacet("search", queryParams);
        }

    }

});

Replace tag ids with display name
Replace tag ids with display name

The ids for contributors, funders and publishers
are used to filter the search results page. However,
the tag that gets displayed should show the name of
the contributor/funder/publisher instead of the id
used for filtering.

Make a request to the api gateway bulk_agents endpoint
that makes a bulk request to SHARE to get record data.
Then replace the tag ids with their correct display names.
