"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AlertFiltersData = exports.AlertFilter = void 0;
const AsyncLock = require('async-lock');
const ts_generic_collections_linq_1 = require("ts-generic-collections-linq");
/**
 * @class
 * @name AlertFilter
 * @public
 * @description
 *      This class Define a filter used when an alert is created to notify only some devices. <br>
 */
class AlertFilter {
    constructor(id, name, companyId, tags) {
        /**
         * @public
         * @readonly
         * @property {string} id The ID of the Filter
         * @instance
         */
        this.id = id;
        /**
         * @public
         * @readonly
         * @property {string} name The Name of the Filter
         * @instance
         */
        this.name = name;
        /**
         * @public
         * @readonly
         * @property {string} companyId The company identifier that owns this filter
         * @instance
         */
        this.companyId = companyId;
        /**
         * @public
         * @readonly
         * @property {List<string>} tags The tag's list to apply
         * @instance
         */
        this.tags = tags ? tags : new ts_generic_collections_linq_1.List();
    }
}
exports.AlertFilter = AlertFilter;
/**
 * @class
 * @name AlertFiltersData
 * @description
 *      This class represents a Structure used to retrieve several AlertFilters objects from server. <br>
 */
class AlertFiltersData {
    constructor(limit = 0) {
        //public data: List<AlertFilter>
        this.alertFilters = new ts_generic_collections_linq_1.Dictionary();
        this.lockKey = "LOCK_AlertFilter";
        this.lockEngine = new AsyncLock({ timeout: 5000, maxPending: 1000 });
        /**
         * @public
         * @readonly
         * @property {List<string>} data The List of AlertFilter found
         * @instance
         */
        // this.data = new List<AlertFilter>();
        /**
         * @public
         * @readonly
         * @property {number} total The Total number of items available
         * @instance
         */
        this.total = 0;
        /**
         * @public
         * @readonly
         * @property {number} limit The Number of items asked
         * @instance
         */
        this.limit = limit;
        /**
         * @public
         * @readonly
         * @property {number} offset The Offset used
         * @instance
         */
        this.offset = 0;
    }
    //region Lock
    lock(fn) {
        let that = this;
        let opts = undefined;
        return that.lockEngine.acquire(that.lockKey, async function () {
            // that._logger.log("debug", LOG_ID + "(lock) lock the ", that.lockKey);
            //that._logger.log("internal", LOG_ID + "(lock) lock the ", that.lockKey);
            return await fn(); // async work
        }, opts).then((result) => {
            // that._logger.log("debug", LOG_ID + "(lock) release the ", that.lockKey);
            //that._logger.log("internal", LOG_ID + "(lock) release the ", that.lockKey, ", result : ", result);
            return result;
        });
    }
    //endregion
    // region Add/get/first/last/remove AlertFilter
    addAlertFilter(alertFilter) {
        let that = this;
        return new Promise((resolve, reject) => {
            that.lock(() => {
                // Treatment in the lock
                if ((!that.alertFilters.containsKey(alertFilter.id))) {
                    that.total++;
                    that.alertFilters.add(alertFilter.id, alertFilter);
                    //needToAsk = true;
                    return alertFilter;
                }
                return;
            }).then((result) => {
                return resolve(result);
            }).catch((result) => {
                resolve(undefined);
            });
        });
    }
    async removeBubbleToJoin(alertFilter) {
        let that = this;
        return new Promise((resolve, reject) => {
            that.lock(() => {
                // Treatment in the lock
                if ((that.alertFilters.containsKey(alertFilter.id))) {
                    that.alertFilters.remove((item) => {
                        return alertFilter.id === item.key;
                    });
                    //needToAsk = true;
                }
            }).then((result) => {
                resolve(result);
            }).catch((result) => {
                resolve(undefined);
            });
        });
    }
    async getAlertFilter() {
        let that = this;
        return new Promise((resolve, reject) => {
            that.lock(() => {
                // Treatment in the lock
                let item = that.alertFilters.elementAt(0);
                if (!item)
                    return;
                let id = item.key;
                let alertFilter = item.value;
                that.alertFilters.remove((item) => {
                    return id === item.key;
                });
                return alertFilter;
            }).then((result) => {
                resolve(result);
            }).catch((result) => {
                resolve(undefined);
            });
        });
    }
    first() {
        let that = this;
        return new Promise((resolve, reject) => {
            that.lock(() => {
                // Treatment in the lock
                let item = that.alertFilters.elementAt(0);
                return item.value;
            }).then((result) => {
                return resolve(result);
            }).catch((result) => {
                resolve(undefined);
            });
        });
    }
    last() {
        let that = this;
        return new Promise((resolve, reject) => {
            that.lock(() => {
                // Treatment in the lock
                let item = that.alertFilters.elementAt(that.total);
                return item.value;
            }).then((result) => {
                //that._logger.log("internal", LOG_ID + "(addBubbleToJoin) Succeed - Jid : ", result);
                return resolve(result);
            }).catch((result) => {
                //that._logger.log("internal", LOG_ID + "(addBubbleToJoin) Failed - Jid : ", result);
                resolve(undefined);
            });
        });
    }
}
exports.AlertFiltersData = AlertFiltersData;
module.exports = { AlertFilter, AlertFiltersData };
//# sourceMappingURL=AlertFilter.js.map