var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
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 { TapStreamReader, TapStreamWriter } from '../../frame/import-adapter';
var EnumConverter = /** @class */ (function () {
    function EnumConverter(mapping) {
        this.mapping = mapping;
    }
    EnumConverter.prototype.decode = function (value) {
        var literalValue = this.decodeToLiteral(value);
        var enumStringValue = this.mapping[literalValue];
        if (!enumStringValue) {
            throw new Error("Unknown litteral value " + literalValue + ".");
        }
        return literalValue;
    };
    EnumConverter.prototype.encode = function (value) {
        if (typeof value !== 'number') {
            throw Error("Illegal argument when encoding enum value. Expecting  number but found " + typeof value);
        }
        return this.encodeToLiteral(value);
    };
    return EnumConverter;
}());
export { EnumConverter };
var FixedSizeEnumConverter = /** @class */ (function (_super) {
    __extends(FixedSizeEnumConverter, _super);
    function FixedSizeEnumConverter(mapping, size) {
        if (size === void 0) { size = 1; }
        var _this = _super.call(this, mapping) || this;
        _this.mapping = mapping;
        _this.size = size;
        return _this;
    }
    FixedSizeEnumConverter.prototype.decodeToLiteral = function (value) {
        return TapStreamReader.fromArray(value).readU(this.size);
    };
    FixedSizeEnumConverter.prototype.encodeToLiteral = function (value) {
        return new TapStreamWriter(this.size).writeU(value, this.size).toBytes;
    };
    return FixedSizeEnumConverter;
}(EnumConverter));
export { FixedSizeEnumConverter };
var EnumListConverter = /** @class */ (function () {
    function EnumListConverter(size, mapping) {
        this.size = size;
        this.mapping = mapping;
    }
    EnumListConverter.prototype.decode = function (value) {
        var e_1, _a;
        var _this = this;
        var bits = this.decodeToLiteral(TapStreamReader.create(value));
        var results = [];
        var enumValues = Object.keys(this.mapping).filter(function (key) { return !isNaN(Number(_this.mapping[key])); });
        try {
            // console.log("numval", enumValues)
            for (var enumValues_1 = __values(enumValues), enumValues_1_1 = enumValues_1.next(); !enumValues_1_1.done; enumValues_1_1 = enumValues_1.next()) {
                var enumName = enumValues_1_1.value;
                var enumValue = this.mapping[enumName];
                var mask = parseInt(enumValue);
                if (mask == 0) {
                    if (bits == 0) {
                        results.push(enumValue);
                    }
                }
                else if ((bits & mask) == mask) {
                    results.push(enumValue);
                }
            }
        }
        catch (e_1_1) { e_1 = { error: e_1_1 }; }
        finally {
            try {
                if (enumValues_1_1 && !enumValues_1_1.done && (_a = enumValues_1.return)) _a.call(enumValues_1);
            }
            finally { if (e_1) throw e_1.error; }
        }
        return results;
    };
    EnumListConverter.prototype.encode = function (values) {
        var e_2, _a;
        var mask = 0;
        try {
            for (var values_1 = __values(values), values_1_1 = values_1.next(); !values_1_1.done; values_1_1 = values_1.next()) {
                var value = values_1_1.value;
                if (typeof value !== 'number') {
                    throw Error("Illegal argument when encoding enum value. Expecting number but found " + typeof value);
                }
                mask |= value;
            }
        }
        catch (e_2_1) { e_2 = { error: e_2_1 }; }
        finally {
            try {
                if (values_1_1 && !values_1_1.done && (_a = values_1.return)) _a.call(values_1);
            }
            finally { if (e_2) throw e_2.error; }
        }
        var result = new TapStreamWriter(this.size).writeU(mask, this.size)
            .toBytes;
        return result;
    };
    EnumListConverter.prototype.decodeToLiteral = function (stream) {
        return stream.readU(this.size);
    };
    return EnumListConverter;
}());
export { EnumListConverter };
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZW51bS1jb252ZXJ0ZXIuanM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