// Generated by purs version 0.12.5
"use strict";
var Data_Array = require("../Data.Array/index.js");
var Data_Decimal = require("../Data.Decimal/index.js");
var Data_Foldable = require("../Data.Foldable/index.js");
var Data_Functor = require("../Data.Functor/index.js");
var Data_List_Types = require("../Data.List.Types/index.js");
var Data_Monoid = require("../Data.Monoid/index.js");
var Data_NonEmpty = require("../Data.NonEmpty/index.js");
var Data_Quantity = require("../Data.Quantity/index.js");
var Data_Semigroup = require("../Data.Semigroup/index.js");
var Data_Units = require("../Data.Units/index.js");
var Insect_Format = require("../Insect.Format/index.js");
var Insect_Language = require("../Insect.Language/index.js");
var prettyVariable = function (name) {
    return [ Insect_Format.ident(name) ];
};
var prettyUnit = function (u) {
    return [ Insect_Format.unit(Data_Units.toString(u)) ];
};
var prettyScalar = function (n) {
    return [ Insect_Format.val(Data_Decimal.toString(n)) ];
};
var prettyQuantity = function (q) {
    var rec = Data_Quantity["prettyPrint'"](q);
    var space = (function () {
        if (rec.space) {
            return " ";
        };
        return "";
    })();
    return [ Insect_Format.val(rec.number), Insect_Format.text(space), Insect_Format.unit(rec.unit) ];
};
var prettyQuantity$prime = function (s) {
    return function (u) {
        return prettyQuantity(Data_Quantity["quantity'"](s)(u));
    };
};
var prettyOp = function (op) {
    var opToStr = function (v) {
        if (v instanceof Insect_Language.Add) {
            return " + ";
        };
        if (v instanceof Insect_Language.Sub) {
            return " - ";
        };
        if (v instanceof Insect_Language.Mul) {
            return " \xd7 ";
        };
        if (v instanceof Insect_Language.Div) {
            return " / ";
        };
        if (v instanceof Insect_Language.Pow) {
            return "^";
        };
        if (v instanceof Insect_Language.Mod) {
            return " % ";
        };
        if (v instanceof Insect_Language.ConvertTo) {
            return " \u279e ";
        };
        throw new Error("Failed pattern match at Insect.PrettyPrint (line 25, column 5 - line 26, column 5): " + [ v.constructor.name ]);
    };
    return [ Insect_Format.text(opToStr(op)) ];
};
var parens = function (m) {
    return Data_Semigroup.append(Data_Semigroup.semigroupArray)(Data_Array.cons(Insect_Format.text("("))(m))([ Insect_Format.text(")") ]);
};
var withParens$prime = function (v) {
    if (v instanceof Insect_Language.Unit) {
        return pretty(v);
    };
    if (v instanceof Insect_Language.Scalar) {
        return pretty(v);
    };
    if (v instanceof Insect_Language.Variable) {
        return pretty(v);
    };
    if (v instanceof Insect_Language.Apply) {
        return pretty(v);
    };
    return parens(pretty(v));
};
var withParens = function (v) {
    if (v instanceof Insect_Language.BinOp && (v.value0 instanceof Insect_Language.Mul && (v.value1 instanceof Insect_Language.Scalar && v.value2 instanceof Insect_Language.Unit))) {
        return pretty(v);
    };
    return withParens$prime(v);
};
var prettyApply = function (fn) {
    return function (xs) {
        return Data_Semigroup.append(Data_Semigroup.semigroupArray)([ Insect_Format["function"](fn), Insect_Format.text("(") ])(Data_Semigroup.append(Data_Semigroup.semigroupArray)(Data_Foldable.intercalate(Data_NonEmpty.foldableNonEmpty(Data_List_Types.foldableList))(Data_Monoid.monoidArray)([ Insect_Format.text(", ") ])(Data_Functor.map(Data_NonEmpty.functorNonEmpty(Data_List_Types.functorList))(pretty)(xs)))([ Insect_Format.text(")") ]));
    };
};
var pretty = function (v) {
    if (v instanceof Insect_Language.Scalar) {
        return prettyScalar(v.value0);
    };
    if (v instanceof Insect_Language.Unit) {
        return prettyUnit(v.value0);
    };
    if (v instanceof Insect_Language.Variable) {
        return prettyVariable(v.value0);
    };
    if (v instanceof Insect_Language.Factorial) {
        return Data_Semigroup.append(Data_Semigroup.semigroupArray)(withParens(v.value0))([ Insect_Format.text("!") ]);
    };
    if (v instanceof Insect_Language.Negate) {
        return Data_Array.cons(Insect_Format.text("-"))(withParens(v.value0));
    };
    if (v instanceof Insect_Language.Apply) {
        return prettyApply(v.value0)(v.value1);
    };
    if (v instanceof Insect_Language.BinOp && v.value0 instanceof Insect_Language.ConvertTo) {
        return Data_Semigroup.append(Data_Semigroup.semigroupArray)(pretty(v.value1))(Data_Semigroup.append(Data_Semigroup.semigroupArray)(prettyOp(Insect_Language.ConvertTo.value))(pretty(v.value2)));
    };
    if (v instanceof Insect_Language.BinOp && (v.value0 instanceof Insect_Language.Mul && (v.value1 instanceof Insect_Language.Scalar && v.value2 instanceof Insect_Language.Unit))) {
        return prettyQuantity$prime(v.value1.value0)(v.value2.value0);
    };
    if (v instanceof Insect_Language.BinOp && v.value0 instanceof Insect_Language.Mul) {
        var addP = function (ex) {
            if (ex instanceof Insect_Language.BinOp && ex.value0 instanceof Insect_Language.Pow) {
                return pretty(ex);
            };
            if (ex instanceof Insect_Language.BinOp && ex.value0 instanceof Insect_Language.Mul) {
                return pretty(ex);
            };
            return withParens(ex);
        };
        return Data_Semigroup.append(Data_Semigroup.semigroupArray)(addP(v.value1))(Data_Semigroup.append(Data_Semigroup.semigroupArray)(prettyOp(Insect_Language.Mul.value))(addP(v.value2)));
    };
    if (v instanceof Insect_Language.BinOp && v.value0 instanceof Insect_Language.Div) {
        var addPRight = function (ex) {
            if (ex instanceof Insect_Language.BinOp && ex.value0 instanceof Insect_Language.Pow) {
                return pretty(ex);
            };
            return withParens(ex);
        };
        var addPLeft = function (ex) {
            if (ex instanceof Insect_Language.BinOp && ex.value0 instanceof Insect_Language.Pow) {
                return pretty(ex);
            };
            if (ex instanceof Insect_Language.BinOp && ex.value0 instanceof Insect_Language.Mul) {
                return pretty(ex);
            };
            return withParens(ex);
        };
        return Data_Semigroup.append(Data_Semigroup.semigroupArray)(addPLeft(v.value1))(Data_Semigroup.append(Data_Semigroup.semigroupArray)(prettyOp(Insect_Language.Div.value))(addPRight(v.value2)));
    };
    if (v instanceof Insect_Language.BinOp && v.value0 instanceof Insect_Language.Add) {
        var addP = function (ex) {
            if (ex instanceof Insect_Language.BinOp && ex.value0 instanceof Insect_Language.Pow) {
                return pretty(ex);
            };
            if (ex instanceof Insect_Language.BinOp && ex.value0 instanceof Insect_Language.Mul) {
                return pretty(ex);
            };
            if (ex instanceof Insect_Language.BinOp && ex.value0 instanceof Insect_Language.Add) {
                return pretty(ex);
            };
            return withParens(ex);
        };
        return Data_Semigroup.append(Data_Semigroup.semigroupArray)(addP(v.value1))(Data_Semigroup.append(Data_Semigroup.semigroupArray)(prettyOp(Insect_Language.Add.value))(addP(v.value2)));
    };
    if (v instanceof Insect_Language.BinOp && v.value0 instanceof Insect_Language.Sub) {
        var addP = function (ex) {
            if (ex instanceof Insect_Language.BinOp && ex.value0 instanceof Insect_Language.Pow) {
                return pretty(ex);
            };
            if (ex instanceof Insect_Language.BinOp && ex.value0 instanceof Insect_Language.Mul) {
                return pretty(ex);
            };
            return withParens(ex);
        };
        return Data_Semigroup.append(Data_Semigroup.semigroupArray)(addP(v.value1))(Data_Semigroup.append(Data_Semigroup.semigroupArray)(prettyOp(Insect_Language.Sub.value))(addP(v.value2)));
    };
    if (v instanceof Insect_Language.BinOp) {
        return Data_Semigroup.append(Data_Semigroup.semigroupArray)(withParens$prime(v.value1))(Data_Semigroup.append(Data_Semigroup.semigroupArray)(prettyOp(v.value0))(withParens$prime(v.value2)));
    };
    throw new Error("Failed pattern match at Insect.PrettyPrint (line 82, column 1 - line 82, column 29): " + [ v.constructor.name ]);
};
module.exports = {
    pretty: pretty,
    prettyQuantity: prettyQuantity
};
