function Guid(g) {
    var arr = new Array();
    if (typeof(g) == "string") {
        initByString(arr, g);
    }
    else {
        initByOther(arr);
    }
    this.toString = function (format) {
        if (typeof(format) == "string") {
            if (format == "N" || format == "D" || format == "B" || format == "P") {
                return toStringWithFormat(arr, format);
            }
            else {
                return toStringWithFormat(arr, "D");
            }
        }
        else {
            return toStringWithFormat(arr, "D");
        }
    };
    function initByString(arr, g) {
        g = g.replace(/\{|\(|\)|\}|-/g, "");
        g = g.toLowerCase();
        if (g.length != 32 || g.search(/[^0-9,a-f]/i) != -1) {
            initByOther(arr);
        }
        else {
            for (var i = 0; i < g.length; i++) {
                arr.push(g.charAt(i));
            }
        }
    }
    function initByOther(arr) {
        var i = 32;
        while (i--) {
            arr.push("0");
        }
    }
    
    function toStringWithFormat(arr, format) {
        switch (format) {
            case "N":
                return arr.toString().replace(/,/g, "");
            case "D":
                var str = arr.slice(0, 8) + "-" + arr.slice(8, 12) + "-" + arr.slice(12, 16) + "-" + arr.slice(16, 20) + "-" + arr.slice(20, 32);
                str = str.replace(/,/g, "");
                return str;
            case "B":
                var str = toStringWithFormat(arr, "D");
                str = "{" + str + "}";
                return str;
            case "P":
                var str = toStringWithFormat(arr, "D");
                str = "(" + str + ")";
                return str;
            default:
                return new Guid();
        }
    }
}
export default function (type) {
    var g = "";
    var i = 32;
    if (!type) {
        type = "N";
    }
    while (i--) {
        g += Math.floor(Math.random() * 16.0).toString(16);
    }
    return new Guid(g).toString(type);
};