import { Meteor } from 'meteor/meteor';
import { check } from 'meteor/check';
import { HTTP } from 'meteor/http';
import Brands from '../collections/brands.js';
Meteor.methods({
    'getBrands' () {

        this.unblock();
        if (this.userId) {
            try {
                const result = HTTP.call('GET', `${Meteor.settings.worklistUrl}/HISIntegration.svc/rest/GetHISBrandsRest`);
                let brandsHttpResult = JSON.parse(result.content);
                Brands.remove({});
                brandsHttpResult.Items.forEach((brand) => {
                    Brands.insert({
                        ...brand
                    });
                });
                return true
            } catch (e) {
                // Got a network error, timeout, or HTTP error in the 400 or 500 range.
                return false;
            }
        }
    },
    "getBrandsName" () {

        this.unblock();
        if (this.userId) {
            try {
                const result = HTTP.call('GET', `${Meteor.settings.worklistUrl}/HISIntegration.svc/rest/GetHISBrandsRest`);
                let brandsHttpResult = JSON.parse(result.content);
                return brandsHttpResult.Items.map(brandObj => {
                    return brandObj.Name
                })
            } catch (e) {
                // Got a network error, timeout, or HTTP error in the 400 or 500 range.
                return false;
            }
        }

    }
});