// Generated by purs version 0.12.5
"use strict";
var Control_Applicative = require("../Control.Applicative/index.js");
var Control_Bind = require("../Control.Bind/index.js");
var Data_Either = require("../Data.Either/index.js");
var Data_Foldable = require("../Data.Foldable/index.js");
var Data_Functor = require("../Data.Functor/index.js");
var Data_List = require("../Data.List/index.js");
var Data_List_Types = require("../Data.List.Types/index.js");
var Data_Maybe = require("../Data.Maybe/index.js");
var Data_Monoid = require("../Data.Monoid/index.js");
var Data_Show = require("../Data.Show/index.js");
var Effect = require("../Effect/index.js");
var Effect_Aff = require("../Effect.Aff/index.js");
var Effect_Class = require("../Effect.Class/index.js");
var Effect_Exception = require("../Effect.Exception/index.js");
var Test_Unit = require("../Test.Unit/index.js");
var Test_Unit_Console = require("../Test.Unit.Console/index.js");
var indent = function (v) {
    if (v === 0) {
        return Data_Monoid.mempty(Data_Monoid.monoidString);
    };
    return "  " + indent(v - 1 | 0);
};
var indent$prime = function ($30) {
    return indent(Data_List.length($30));
};
var printLive = function (tst) {
    var runSuiteItem = function (path) {
        return function (v) {
            if (v instanceof Data_Either.Left) {
                return Effect_Class.liftEffect(Effect_Aff.monadEffectAff)(function __do() {
                    Test_Unit_Console.print(indent$prime(path))();
                    Test_Unit_Console.print("\u2192 Suite: ")();
                    Test_Unit_Console.printLabel(v.value0)();
                    return Data_Functor["void"](Effect.functorEffect)(Test_Unit_Console.print("\x0a"))();
                });
            };
            if (v instanceof Data_Either.Right) {
                return Control_Bind.discard(Control_Bind.discardUnit)(Effect_Aff.bindAff)(Effect_Class.liftEffect(Effect_Aff.monadEffectAff)(function __do() {
                    Test_Unit_Console.print(indent$prime(path))();
                    Test_Unit_Console.savePos();
                    Test_Unit_Console.print("\u2192 Running: ")();
                    Test_Unit_Console.printLabel(v.value0.value0)();
                    return Test_Unit_Console.restorePos();
                }))(function () {
                    return Control_Bind.bind(Effect_Aff.bindAff)(Effect_Aff.attempt(v.value0.value1))(function (v1) {
                        return Data_Functor["void"](Effect_Aff.functorAff)((function () {
                            if (v1 instanceof Data_Either.Right) {
                                return Effect_Class.liftEffect(Effect_Aff.monadEffectAff)(function __do() {
                                    Test_Unit_Console.eraseLine();
                                    Test_Unit_Console.printPass("\u2713 Passed: ")();
                                    Test_Unit_Console.printLabel(v.value0.value0)();
                                    return Test_Unit_Console.print("\x0a")();
                                });
                            };
                            if (v1 instanceof Data_Either.Left) {
                                return Effect_Class.liftEffect(Effect_Aff.monadEffectAff)(function __do() {
                                    Test_Unit_Console.eraseLine();
                                    Test_Unit_Console.printFail("\u2620 Failed: ")();
                                    Test_Unit_Console.printLabel(v.value0.value0)();
                                    Test_Unit_Console.print(" because ")();
                                    Test_Unit_Console.printFail(Effect_Exception.message(v1.value0))();
                                    return Test_Unit_Console.print("\x0a")();
                                });
                            };
                            throw new Error("Failed pattern match at Test.Unit.Output.Fancy (line 42, column 14 - line 57, column 1): " + [ v1.constructor.name ]);
                        })());
                    });
                });
            };
            throw new Error("Failed pattern match at Test.Unit.Output.Fancy (line 28, column 5 - line 34, column 5): " + [ path.constructor.name, v.constructor.name ]);
        };
    };
    return Test_Unit.walkSuite(runSuiteItem)(tst);
};
var printErrors = function (tests) {
    return function (skCount) {
        var printHeader = function (level) {
            return function (path) {
                var v = Data_List.uncons(path);
                if (v instanceof Data_Maybe.Nothing) {
                    return Test_Unit_Console.print(indent(level));
                };
                if (v instanceof Data_Maybe.Just) {
                    return function __do() {
                        Test_Unit_Console.print(indent(level) + ("In \"" + (v.value0.head + "\":\x0a")))();
                        return printHeader(level + 1 | 0)(v.value0.tail)();
                    };
                };
                throw new Error("Failed pattern match at Test.Unit.Output.Fancy (line 79, column 34 - line 83, column 41): " + [ v.constructor.name ]);
            };
        };
        var printError = function (err) {
            return function __do() {
                Data_Maybe.maybe(Test_Unit_Console.printFail(Effect_Exception.message(err)))(Test_Unit_Console.printFail)(Effect_Exception.stack(err))();
                return Test_Unit_Console.print("\x0a")();
            };
        };
        var printItem = function (v) {
            return function __do() {
                printHeader(0)(v.value0)();
                printError(v.value1)();
                return Test_Unit_Console.print("\x0a")();
            };
        };
        var list = Data_Foldable.traverse_(Effect.applicativeEffect)(Data_List_Types.foldableList)(printItem);
        return Control_Bind.bind(Effect_Aff.bindAff)(Test_Unit.collectResults(tests))(function (v) {
            var skMsg = (function () {
                if (skCount === 0) {
                    return "";
                };
                if (skCount === 1) {
                    return " (1 test skipped)";
                };
                return " (" + (Data_Show.show(Data_Show.showInt)(skCount) + " tests skipped)");
            })();
            var errors = Test_Unit.keepErrors(v);
            return Effect_Class.liftEffect(Effect_Aff.monadEffectAff)((function () {
                var v1 = Data_List.length(errors);
                if (v1 === 0) {
                    return Test_Unit_Console.printPass("\x0aAll " + (Data_Show.show(Data_Show.showInt)(Data_List.length(v)) + (" tests passed" + (skMsg + "! \ud83c\udf89\x0a"))));
                };
                if (v1 === 1) {
                    return function __do() {
                        Test_Unit_Console.printFail("\x0a1 test failed" + (skMsg + ":\x0a\x0a"))();
                        return list(errors)();
                    };
                };
                return function __do() {
                    Test_Unit_Console.printFail("\x0a" + (Data_Show.show(Data_Show.showInt)(v1) + (" tests failed" + (skMsg + ":\x0a\x0a"))))();
                    return list(errors)();
                };
            })());
        });
    };
};
var runTest = function (suite) {
    return Control_Bind.bind(Effect_Aff.bindAff)(printLive(suite))(function (v) {
        return Control_Bind.discard(Control_Bind.discardUnit)(Effect_Aff.bindAff)(printErrors(v)(Test_Unit.countSkippedTests(suite)))(function () {
            return Control_Applicative.pure(Effect_Aff.applicativeAff)(v);
        });
    });
};
module.exports = {
    runTest: runTest
};
