import { check } from 'meteor/check';
import { Mongo } from 'meteor/mongo'
import Collections from '../../collections/worklist4hislink.js';
import { Counts } from 'meteor/tmeasday:publish-counts';

Meteor.publish("worklist4hislink", function({ brand, limit, query }) {

    if (brand == null) {
        return [];
    }

    if (!this.userId) {
        return this.ready();
    }

    const userid = this.userId;
    check(brand, String);
    check(limit, Number);
    if (query == null)
        query = {};
    try {
        // Counts.publish(this, 'brandCount', Collections[brand].find(query), { noReady: true, fastCount: true });
        // else
        //     Counts.publish(this, 'brandCount', Collections[brand].find(), { noReady: true, fastCount: true });
        // let Worklist = new Mongo.Collection(brand);
        // console.log(Worklist.find({ _userid: userid, _devicemap: devicemap }).fetch())
        // if (query == null)
        //     return Collections[brand].find({}, {
        //         disableOplog: true,
        //         // pollingThrottleMs: 1000,
        //         pollingIntervalMs: 1000,
        //         limit: limit
        //     });
        // else
        return Collections[brand].find(query, {
            disableOplog: true,
            // pollingThrottleMs: 1000,
            pollingIntervalMs: 1000,
            limit: limit
        });
    } catch (e) {
        // Counts.publish(this, 'brandCount', Collections[brand].find({}), { noReady: false });
        this.error(new Meteor.Error(e, 'error'));
        return [];
    }


});

Meteor.publish("brandCount", function({ brand, query }) {
    if (!this.userId) {
        return this.ready();
    }
    if (query == null)
        query = {}
    if (brand == null) {
        return [];
    }
    try {
        Counts.publish(this, 'brandCount', Collections[brand].find(query), { fastCount: true });
    } catch {
        Counts.publish(this, 'brandCount', Collections[brand].find({}), { fastCount: true });
    }
})