var __values = (this && this.__values) || function(o) {
    var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
    if (m) return m.call(o);
    if (o && typeof o.length === "number") return {
        next: function () {
            if (o && i >= o.length) o = void 0;
            return { value: o && o[i++], done: !o };
        }
    };
    throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
import { NumberConverter } from './number-converter';
function swapMap(val) {
    var ret = {};
    for (var key in val) {
        ret[val[key]] = key;
    }
    return ret;
}
var MultipleMaskConverter = /** @class */ (function () {
    // protected static _instance: UniqMaskDecoder;
    function MultipleMaskConverter(mapping) {
        this._mapping = mapping;
        // this._mapping[0x00] = (null as any as OutputType);
        // this._mapping[0x01] = (null as any as OutputType);
    }
    MultipleMaskConverter.prototype.decode = function (body) {
        return MultipleMaskConverter.decodeAll(body, this._mapping);
    };
    /**
     * TODO check that not use more than the first 7 bits
     * @param types
     */
    MultipleMaskConverter.prototype.encode = function (types) {
        return MultipleMaskConverter.encodeAll(types, this._mapping);
    };
    MultipleMaskConverter.encodeAll = function (values, mapping) {
        var e_1, _a;
        var _reverseMapping = swapMap(mapping);
        var result = 0x0;
        try {
            for (var values_1 = __values(values), values_1_1 = values_1.next(); !values_1_1.done; values_1_1 = values_1.next()) {
                var type = values_1_1.value;
                if (type in _reverseMapping) {
                    var mask = parseInt(_reverseMapping[type]);
                    result = result | mask;
                }
            }
        }
        catch (e_1_1) { e_1 = { error: e_1_1 }; }
        finally {
            try {
                if (values_1_1 && !values_1_1.done && (_a = values_1.return)) _a.call(values_1);
            }
            finally { if (e_1) throw e_1.error; }
        }
        return Uint8Array.from([result & 0xff]);
    };
    MultipleMaskConverter.decodeAll = function (value, mapping) {
        var results = [];
        if (typeof value !== 'number') {
            value = NumberConverter.fromOpaqueMsb(value, Math.min(value.length * 8, 32));
        }
        for (var key in mapping) {
            var mask = parseInt(key);
            if (mask == 0 && value == 0) {
                results.push(mapping[mask]);
            }
            else if ((mask & value) != 0) {
                results.push(mapping[mask]);
            }
        }
        return results;
    };
    return MultipleMaskConverter;
}());
export { MultipleMaskConverter };
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibXVsdGlwbGUtbWFzay1kZWNvZGVyLmpzIiwic291cmNlUm9vdCI6Im5nOi8vQGlvdGl6ZS90YXAvY2xpZW50L2ltcGwvIiwic291cmNlcyI6WyJsaWIvY29udmVydGVyL2JvZHkvbXVsdGlwbGUtbWFzay1kZWNvZGVyLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7Ozs7Ozs7Ozs7O0FBRUEsT0FBTyxFQUFFLGVBQWUsRUFBRSxNQUFNLG9CQUFvQixDQUFDO0FBRXJELFNBQVMsT0FBTyxDQUFDLEdBQTJCO0lBQ3hDLElBQUksR0FBRyxHQUEyQixFQUFFLENBQUM7SUFDckMsS0FBSyxJQUFJLEdBQUcsSUFBSSxHQUFHLEVBQUU7UUFDakIsR0FBRyxDQUFDLEdBQUcsQ0FBQyxHQUFHLENBQUMsQ0FBQyxHQUFHLEdBQUcsQ0FBQztLQUN2QjtJQUNELE9BQU8sR0FBRyxDQUFDO0FBQ2YsQ0FBQztBQUVEO0lBR0ksK0NBQStDO0lBRS9DLCtCQUFZLE9BQVk7UUFDcEIsSUFBSSxDQUFDLFFBQVEsR0FBRyxPQUFPLENBQUM7UUFDeEIscURBQXFEO1FBQ3JELHFEQUFxRDtJQUN6RCxDQUFDO0lBRU0sc0NBQU0sR0FBYixVQUFjLElBQWdCO1FBQzFCLE9BQU8scUJBQXFCLENBQUMsU0FBUyxDQUFhLElBQUksRUFBRSxJQUFJLENBQUMsUUFBUSxDQUFDLENBQUM7SUFDNUUsQ0FBQztJQUVEOzs7T0FHRztJQUNJLHNDQUFNLEdBQWIsVUFBYyxLQUFtQjtRQUM3QixPQUFPLHFCQUFxQixDQUFDLFNBQVMsQ0FBQyxLQUFLLEVBQUUsSUFBSSxDQUFDLFFBQVEsQ0FBQyxDQUFDO0lBQ2pFLENBQUM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