/// <reference path="leaf.ts" />
var Leaf;
(function (Leaf) {
    var ErrorDoc = (function () {
        function ErrorDoc() {
            this.errors = {};
        }
        ErrorDoc.create = function () {
            return new ErrorDoc();
        };
        ErrorDoc.prototype.define = function (name, base, defaultProperty) {
            var Base = Error;
            if (typeof base === "string") {
                var _ = this[base];
                if (typeof _ === "function") {
                    Base = _;
                }
            }
            else if (typeof base === "function") {
                Base = base;
            }
            else if (base) {
                console.warn("Unknown base error", base);
            }
            var E = createError(name, Base, defaultProperty);
            this.errors[name] = E;
            return this;
        };
        ErrorDoc.prototype.generate = function () {
            return this.errors;
        };
        return ErrorDoc;
    }());
    Leaf.ErrorDoc = ErrorDoc;
    function createError(name, base, defaultProperty) {
        var E = function (message, detail) {
            if (typeof message === "object") {
                if (!detail) {
                    detail = message;
                    message = name;
                }
                else {
                    try {
                        message = JSON.stringify(message, null, 4);
                    }
                    catch (e) {
                        message = name + "Warning: recursive error property" + e.toString();
                    }
                }
            }
            this.name = name;
            this.message = message;
            if (typeof defaultProperty === "function") {
                var _ = base;
                base = defaultProperty;
                defaultProperty = _;
            }
            if (!detail)
                detail = {};
            if (!defaultProperty)
                defaultProperty = {};
            for (var prop in defaultProperty) {
                this[prop] = defaultProperty[prop];
            }
            for (var prop in detail) {
                this[prop] = detail[prop];
            }
        };
        var Base = base;
        E.prototype = new Base(name, {});
        return E;
    }
    Leaf.createError = createError;
    Leaf.Errors = ErrorDoc.create().define("Timeout")
        .define("Abort")
        .define("IOError")
        .define("NetworkError")
        .generate();
})(Leaf || (Leaf = {}));
//# sourceMappingURL=errors.js.map