"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
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;
}());
exports.PipeConverterBuilder = 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 output = body;
        for (var _i = 0, _a = this.converters; _i < _a.length; _i++) {
            var converter = _a[_i];
            if (typeof converter == "function") {
                output = converter(output);
            }
            else {
                output = converter.decode(output);
            }
        }
        return output;
    };
    PipeConverter.prototype.encode = function (value) {
        var output = value;
        for (var _i = 0, _a = this.converters.reverse(); _i < _a.length; _i++) {
            var converter = _a[_i];
            if (typeof converter == "function") {
                output = converter(output);
            }
            else {
                output = converter.encode(output);
            }
        }
        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;
}());
exports.PipeConverter = PipeConverter;
