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.");
};
var PipeConverterBuilder = /** @class */ (function () {
    function PipeConverterBuilder(aliases) {
        if (aliases === void 0) { aliases = {}; }
        this.aliases = aliases;
    }
    PipeConverterBuilder.prototype.pipe = function (converter) {
        return new PipeConverter([], this.aliases).pipe(converter);
    };
    return PipeConverterBuilder;
}());
export { PipeConverterBuilder };
/**
 * Pipe multiple converter one after another
 */
var PipeConverter = /** @class */ (function () {
    function PipeConverter(converters, aliases) {
        if (aliases === void 0) { aliases = {}; }
        this.converters = converters;
        this.aliases = aliases;
    }
    PipeConverter.prototype.decode = function (body) {
        var e_1, _a;
        var output = body;
        try {
            for (var _b = __values(this.converters), _c = _b.next(); !_c.done; _c = _b.next()) {
                var converter = _c.value;
                if (typeof converter == 'function') {
                    output = converter(output);
                }
                else {
                    output = converter.decode(output);
                }
            }
        }
        catch (e_1_1) { e_1 = { error: e_1_1 }; }
        finally {
            try {
                if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
            }
            finally { if (e_1) throw e_1.error; }
        }
        return output;
    };
    PipeConverter.prototype.encode = function (value) {
        var e_2, _a;
        var output = value;
        try {
            for (var _b = __values(this.converters.reverse()), _c = _b.next(); !_c.done; _c = _b.next()) {
                var converter = _c.value;
                if (typeof converter == 'function') {
                    output = converter(output);
                }
                else {
                    output = converter.encode(output);
                }
            }
        }
        catch (e_2_1) { e_2 = { error: e_2_1 }; }
        finally {
            try {
                if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
            }
            finally { if (e_2) throw e_2.error; }
        }
        return output;
    };
    PipeConverter.prototype.pipe = function (converter) {
        if (typeof converter === 'string') {
            if (!(converter in this.aliases)) {
                throw new Error("Illegal argument, missing converter id: " + converter);
            }
            converter = this.aliases[converter];
        }
        this.converters.push(converter);
        return this;
    };
    PipeConverter.pipe = function (converter) {
        return new PipeConverter([converter], {});
    };
    return PipeConverter;
}());
export { PipeConverter };
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoicGlwZS1jb252ZXJ0ZXIuanMiLCJzb3VyY2VSb290Ijoibmc6Ly9AaW90aXplL3RhcC9jbGllbnQvaW1wbC8iLCJzb3VyY2VzIjpbImxpYi9jb252ZXJ0ZXIvYm9keS9waXBlLWNvbnZlcnRlci50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7Ozs7Ozs7OztBQU9BO0lBQ0ksOEJBQ1csT0FBNEQ7UUFBNUQsd0JBQUEsRUFBQSxZQUE0RDtRQUE1RCxZQUFPLEdBQVAsT0FBTyxDQUFxRDtJQUNuRSxDQUFDO0lBRUUsbUNBQUksR0FBWCxVQUFlLFNBQW9EO1FBQy9ELE9BQU8sSUFBSSxhQUFhLENBQUksRUFBRSxFQUFFLElBQUksQ0FBQyxPQUFPLENBQUMsQ0FBQyxJQUFJLENBQUksU0FBZ0IsQ0FBQyxDQUFDO0lBQzVFLENBQUM7SUFDTCwyQkFBQztBQUFELENBQUMsQUFSRCxJQVFDOztBQUVEOztHQUVHO0FBQ0g7SUFDSSx1QkFDVyxVQUF5QyxFQUN6QyxPQUE0RDtRQUE1RCx3QkFBQSxFQUFBLFlBQTREO1FBRDVELGVBQVUsR0FBVixVQUFVLENBQStCO1FBQ3pDLFlBQU8sR0FBUCxPQUFPLENBQXFEO0lBQ25FLENBQUM7SUFFTCw4QkFBTSxHQUFOLFVBQU8sSUFBZ0I7O1FBQ25CLElBQUksTUFBTSxHQUFRLElBQUksQ0FBQzs7WUFDdkIsS0FBc0IsSUFBQSxLQUFBLFNBQUEsSUFBSSxDQUFDLFVBQVUsQ0FBQSxnQkFBQSw0QkFBRTtnQkFBbEMsSUFBSSxTQUFTLFdBQUE7Z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