// Generated by purs version 0.12.5
"use strict";
var Data_Boolean = require("../Data.Boolean/index.js");
var Data_Decimal = require("../Data.Decimal/index.js");
var Data_Eq = require("../Data.Eq/index.js");
var Data_Foldable = require("../Data.Foldable/index.js");
var Data_Function = require("../Data.Function/index.js");
var Data_Functor = require("../Data.Functor/index.js");
var Data_List = require("../Data.List/index.js");
var Data_List_NonEmpty = require("../Data.List.NonEmpty/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_Monoid_Additive = require("../Data.Monoid.Additive/index.js");
var Data_Newtype = require("../Data.Newtype/index.js");
var Data_NonEmpty = require("../Data.NonEmpty/index.js");
var Data_Ord = require("../Data.Ord/index.js");
var Data_Ordering = require("../Data.Ordering/index.js");
var Data_Pair = require("../Data.Pair/index.js");
var Data_Semigroup = require("../Data.Semigroup/index.js");
var Data_Semiring = require("../Data.Semiring/index.js");
var Data_Show = require("../Data.Show/index.js");
var Data_Tuple = require("../Data.Tuple/index.js");
var Decimal = (function () {
    function Decimal() {

    };
    Decimal.value = new Decimal();
    return Decimal;
})();
var Binary = (function () {
    function Binary() {

    };
    Binary.value = new Binary();
    return Binary;
})();
var Prefix = (function () {
    function Prefix(value0, value1) {
        this.value0 = value0;
        this.value1 = value1;
    };
    Prefix.create = function (value0) {
        return function (value1) {
            return new Prefix(value0, value1);
        };
    };
    return Prefix;
})();
var BaseUnit = function (x) {
    return x;
};
var Standard = (function () {
    function Standard() {

    };
    Standard.value = new Standard();
    return Standard;
})();
var NonStandard = (function () {
    function NonStandard(value0) {
        this.value0 = value0;
    };
    NonStandard.create = function (value0) {
        return new NonStandard(value0);
    };
    return NonStandard;
})();
var DerivedUnit = (function () {
    function DerivedUnit(value0) {
        this.value0 = value0;
    };
    DerivedUnit.create = function (value0) {
        return new DerivedUnit(value0);
    };
    return DerivedUnit;
})();
var unity$prime = {
    "short": "unity",
    "long": "unity",
    unitType: Standard.value
};
var unity = new DerivedUnit(Data_List_Types.Nil.value);
var split = function (v) {
    return function (v1) {
        if (v1 instanceof Data_List_Types.Nil) {
            return {
                yes: Data_List_Types.Nil.value,
                no: Data_List_Types.Nil.value
            };
        };
        if (v1 instanceof Data_List_Types.Cons) {
            var res = split(v)(v1.value1);
            var $72 = v(v1.value0);
            if ($72) {
                return {
                    yes: new Data_List_Types.Cons(v1.value0, res.yes),
                    no: res.no
                };
            };
            return {
                yes: res.yes,
                no: new Data_List_Types.Cons(v1.value0, res.no)
            };
        };
        throw new Error("Failed pattern match at Data.Units (line 199, column 1 - line 199, column 69): " + [ v.constructor.name, v1.constructor.name ]);
    };
};
var sortBy$prime = function (f) {
    return function (v) {
        var sorted = Data_List.sortBy(f)(new Data_List_Types.Cons(v.value0, v.value1));
        var nel = (function () {
            var v1 = Data_List.uncons(sorted);
            if (v1 instanceof Data_Maybe.Just) {
                return new Data_NonEmpty.NonEmpty(v1.value0.head, v1.value0.tail);
            };
            if (v1 instanceof Data_Maybe.Nothing) {
                return new Data_NonEmpty.NonEmpty(v.value0, v.value1);
            };
            throw new Error("Failed pattern match at Data.Units (line 222, column 11 - line 224, column 45): " + [ v1.constructor.name ]);
        })();
        return nel;
    };
};
var shortName = function (v) {
    return v["short"];
};
var runDerivedUnit = function (v) {
    return v.value0;
};
var prettyExponent = function (v) {
    if (v === -5.0) {
        return "\u207b\u2075";
    };
    if (v === -4.0) {
        return "\u207b\u2074";
    };
    if (v === -3.0) {
        return "\u207b\xb3";
    };
    if (v === -2.0) {
        return "\u207b\xb2";
    };
    if (v === -1.0) {
        return "\u207b\xb9";
    };
    if (v === 1.0) {
        return "";
    };
    if (v === 2.0) {
        return "\xb2";
    };
    if (v === 3.0) {
        return "\xb3";
    };
    if (v === 4.0) {
        return "\u2074";
    };
    if (v === 5.0) {
        return "\u2075";
    };
    return "^(" + (Data_Show.show(Data_Show.showNumber)(v) + ")");
};
var prefixName = function (v) {
    if (v.value0 instanceof Decimal) {
        var pn = function (v1) {
            if (v1 === -18.0) {
                return new Data_Maybe.Just("a");
            };
            if (v1 === -12.0) {
                return new Data_Maybe.Just("p");
            };
            if (v1 === -15.0) {
                return new Data_Maybe.Just("f");
            };
            if (v1 === -9.0) {
                return new Data_Maybe.Just("n");
            };
            if (v1 === -6.0) {
                return new Data_Maybe.Just("\xb5");
            };
            if (v1 === -3.0) {
                return new Data_Maybe.Just("m");
            };
            if (v1 === -2.0) {
                return new Data_Maybe.Just("c");
            };
            if (v1 === -1.0) {
                return new Data_Maybe.Just("d");
            };
            if (v1 === 0.0) {
                return new Data_Maybe.Just("");
            };
            if (v1 === 2.0) {
                return new Data_Maybe.Just("h");
            };
            if (v1 === 3.0) {
                return new Data_Maybe.Just("k");
            };
            if (v1 === 6.0) {
                return new Data_Maybe.Just("M");
            };
            if (v1 === 9.0) {
                return new Data_Maybe.Just("G");
            };
            if (v1 === 12.0) {
                return new Data_Maybe.Just("T");
            };
            if (v1 === 15.0) {
                return new Data_Maybe.Just("P");
            };
            if (v1 === 18.0) {
                return new Data_Maybe.Just("E");
            };
            return Data_Maybe.Nothing.value;
        };
        return pn(Data_Decimal.toNumber(v.value1));
    };
    if (v.value0 instanceof Binary) {
        var pn = function (v1) {
            if (v1 === 0.0) {
                return new Data_Maybe.Just("");
            };
            if (v1 === 10.0) {
                return new Data_Maybe.Just("Ki");
            };
            if (v1 === 20.0) {
                return new Data_Maybe.Just("Mi");
            };
            if (v1 === 30.0) {
                return new Data_Maybe.Just("Gi");
            };
            if (v1 === 40.0) {
                return new Data_Maybe.Just("Ti");
            };
            if (v1 === 50.0) {
                return new Data_Maybe.Just("Pi");
            };
            if (v1 === 60.0) {
                return new Data_Maybe.Just("Ei");
            };
            if (v1 === 70.0) {
                return new Data_Maybe.Just("Zi");
            };
            if (v1 === 80.0) {
                return new Data_Maybe.Just("Yi");
            };
            return Data_Maybe.Nothing.value;
        };
        return pn(Data_Decimal.toNumber(v.value1));
    };
    throw new Error("Failed pattern match at Data.Units (line 415, column 1 - line 415, column 35): " + [ v.constructor.name ]);
};
var toString = function (v) {
    var usSorted = Data_List.sortBy(Data_Ord.comparing(Data_Ord.ordNumber)(function (rec) {
        return -rec.exponent;
    }))(v.value0);
    var toNum = function (v1) {
        if (v1 instanceof Decimal) {
            return 10;
        };
        if (v1 instanceof Binary) {
            return 2;
        };
        throw new Error("Failed pattern match at Data.Units (line 466, column 5 - line 467, column 5): " + [ v1.constructor.name ]);
    };
    var splitted = Data_List.span(function (rec) {
        return rec.exponent >= 0.0;
    })(usSorted);
    var reverseExp = function (rec) {
        return {
            exponent: -rec.exponent,
            baseUnit: rec.baseUnit,
            prefix: rec.prefix
        };
    };
    var prefixName$prime = function (v1) {
        return Data_Maybe.fromMaybe(Data_Show.show(Data_Show.showInt)(toNum(v1.value0)) + ("^" + (Data_Show.show(Data_Decimal.showDecimal)(v1.value1) + "\xb7")))(prefixName(v1));
    };
    var withExp = function (v1) {
        return prefixName$prime(v1.prefix) + (shortName(v1.baseUnit) + prettyExponent(v1.exponent));
    };
    var positiveUsStr = Data_Foldable.intercalate(Data_List_Types.foldableList)(Data_Monoid.monoidString)("\xb7")(Data_Functor.map(Data_List_Types.functorList)(withExp)(splitted.init));
    var negativeUs = Data_List.sortBy(Data_Ord.comparing(Data_Ord.ordNumber)(function (v1) {
        return v1.exponent;
    }))(splitted.rest);
    var negativeUsStr = Data_Foldable.intercalate(Data_List_Types.foldableList)(Data_Monoid.monoidString)("\xb7")(Data_Functor.map(Data_List_Types.functorList)(withExp)(negativeUs));
    var negativeUsStr$prime = Data_Foldable.intercalate(Data_List_Types.foldableList)(Data_Monoid.monoidString)("\xb7")(Data_Functor.map(Data_List_Types.functorList)(function ($219) {
        return withExp(reverseExp($219));
    })(negativeUs));
    var unitString = (function () {
        if (splitted.init instanceof Data_List_Types.Nil) {
            return negativeUsStr;
        };
        if (negativeUs instanceof Data_List_Types.Nil) {
            return positiveUsStr;
        };
        if (negativeUs instanceof Data_List_Types.Cons && negativeUs.value1 instanceof Data_List_Types.Nil) {
            return positiveUsStr + ("/" + negativeUsStr$prime);
        };
        return positiveUsStr + ("/(" + (negativeUsStr$prime + ")"));
    })();
    return unitString;
};
var power = function (u) {
    return function (n) {
        var update = function (rec) {
            return {
                exponent: rec.exponent * n,
                baseUnit: rec.baseUnit,
                prefix: rec.prefix
            };
        };
        return DerivedUnit.create(Data_Functor.map(Data_List_Types.functorList)(update)(runDerivedUnit(u)));
    };
};
var noPrefix = new Prefix(Decimal.value, Data_Semiring.zero(Data_Decimal.semiringDecimal));
var removePrefix = function (v) {
    return DerivedUnit.create(Data_Functor.map(Data_List_Types.functorList)(function (v1) {
        return {
            prefix: noPrefix,
            baseUnit: v1.baseUnit,
            exponent: v1.exponent
        };
    })(v.value0));
};
var longName = function (v) {
    return v["long"];
};
var showBaseUnit = new Data_Show.Show(longName);
var showDerivedUnit = new Data_Show.Show(function (v) {
    var decPrf = function (v1) {
        return function (str) {
            if (v1 === -18.0) {
                return "(atto " + (str + ")");
            };
            if (v1 === -15.0) {
                return "(femto " + (str + ")");
            };
            if (v1 === -12.0) {
                return "(pico " + (str + ")");
            };
            if (v1 === -9.0) {
                return "(nano " + (str + ")");
            };
            if (v1 === -6.0) {
                return "(micro " + (str + ")");
            };
            if (v1 === -3.0) {
                return "(milli " + (str + ")");
            };
            if (v1 === 0.0) {
                return str;
            };
            if (v1 === 3.0) {
                return "(kilo " + (str + ")");
            };
            if (v1 === 6.0) {
                return "(mega " + (str + ")");
            };
            if (v1 === 9.0) {
                return "(giga " + (str + ")");
            };
            if (v1 === 12.0) {
                return "(tera " + (str + ")");
            };
            if (v1 === 15.0) {
                return "(peta " + (str + ")");
            };
            if (v1 === 18.0) {
                return "(exa " + (str + ")");
            };
            return "(decimalPrefix (" + (Data_Show.show(Data_Show.showNumber)(v1) + (") (" + (str + "))")));
        };
    };
    var binPrf = function (prefix) {
        return function (str) {
            return "(binaryPrefix (" + (Data_Show.show(Data_Show.showNumber)(prefix) + (") (" + (str + "))")));
        };
    };
    var addPrf = function (v1) {
        if (v1 instanceof Decimal) {
            return decPrf;
        };
        if (v1 instanceof Binary) {
            return binPrf;
        };
        throw new Error("Failed pattern match at Data.Units (line 345, column 7 - line 345, column 30): " + [ v1.constructor.name ]);
    };
    var show$prime = function (v1) {
        if (v1.exponent === 1.0) {
            return addPrf(v1.prefix.value0)(Data_Decimal.toNumber(v1.prefix.value1))(Data_Show.show(showBaseUnit)(v1.baseUnit));
        };
        if (Data_Eq.eq(Data_Decimal.eqDecimal)(v1.prefix.value1)(Data_Semiring.zero(Data_Decimal.semiringDecimal))) {
            return Data_Show.show(showBaseUnit)(v1.baseUnit) + (" .^ (" + (Data_Show.show(Data_Show.showNumber)(v1.exponent) + ")"));
        };
        if (Data_Boolean.otherwise) {
            return addPrf(v1.prefix.value0)(Data_Decimal.toNumber(v1.prefix.value1))(Data_Show.show(showBaseUnit)(v1.baseUnit)) + (" .^ (" + (Data_Show.show(Data_Show.showNumber)(v1.exponent) + ")"));
        };
        throw new Error("Failed pattern match at Data.Units (line 365, column 7 - line 366, column 49): " + [ v1.constructor.name ]);
    };
    var listString = function (v1) {
        if (v1 instanceof Data_List_Types.Nil) {
            return "unity";
        };
        if (v1 instanceof Data_List_Types.Cons && v1.value1 instanceof Data_List_Types.Nil) {
            return show$prime(v1.value0);
        };
        return "(" + (Data_Foldable.intercalate(Data_List_Types.foldableList)(Data_Monoid.monoidString)(" <> ")(Data_Functor.map(Data_List_Types.functorList)(show$prime)(v1)) + ")");
    };
    return listString(v.value0);
});
var isStandardUnit = function (v) {
    if (v.unitType instanceof Standard) {
        return true;
    };
    return false;
};
var groupBy$prime = function (v) {
    return function (v1) {
        if (v1 instanceof Data_List_Types.Nil) {
            return Data_List_Types.Nil.value;
        };
        if (v1 instanceof Data_List_Types.Cons) {
            var v2 = split(v(v1.value0))(v1.value1);
            return new Data_List_Types.Cons(new Data_NonEmpty.NonEmpty(v1.value0, v2.yes), groupBy$prime(v)(v2.no));
        };
        throw new Error("Failed pattern match at Data.Units (line 212, column 1 - line 212, column 67): " + [ v.constructor.name, v1.constructor.name ]);
    };
};
var fromBaseUnit = function ($220) {
    return DerivedUnit.create(Data_List.singleton((function (v) {
        return {
            prefix: noPrefix,
            baseUnit: v,
            exponent: 1.0
        };
    })($220)));
};
var makeNonStandard = function ($$long) {
    return function ($$short) {
        return function (factor) {
            return function (standardUnit) {
                return fromBaseUnit({
                    "short": $$short,
                    "long": $$long,
                    unitType: new NonStandard({
                        standardUnit: standardUnit,
                        factor: Data_Decimal.fromNumber(factor)
                    })
                });
            };
        };
    };
};
var makeStandard = function ($$long) {
    return function ($$short) {
        return fromBaseUnit({
            "short": $$short,
            "long": $$long,
            unitType: Standard.value
        });
    };
};
var eqPrefixBase = new Data_Eq.Eq(function (x) {
    return function (y) {
        if (x instanceof Decimal && y instanceof Decimal) {
            return true;
        };
        if (x instanceof Binary && y instanceof Binary) {
            return true;
        };
        return false;
    };
});
var withPrefix = function (base) {
    return function (p) {
        return function (v) {
            if (v.value0 instanceof Data_List_Types.Nil) {
                return DerivedUnit.create(Data_List.singleton({
                    prefix: new Prefix(base, Data_Decimal.fromNumber(p)),
                    baseUnit: unity$prime,
                    exponent: 1.0
                }));
            };
            var isPlaceholder = function (v1) {
                return v1.exponent === 1.0 && (Data_Eq.eq(eqPrefixBase)(base)(v1.prefix.value0) || Data_Eq.eq(Data_Decimal.eqDecimal)(v1.prefix.value1)(Data_Semiring.zero(Data_Decimal.semiringDecimal)));
            };
            var addPrefixExp = function (du) {
                return {
                    prefix: new Prefix(base, Data_Semiring.add(Data_Decimal.semiringDecimal)(Data_Decimal.fromNumber(p))(du.prefix.value1)),
                    baseUnit: du.baseUnit,
                    exponent: du.exponent
                };
            };
            return DerivedUnit.create((function () {
                var v1 = Data_List.findIndex(isPlaceholder)(v.value0);
                if (v1 instanceof Data_Maybe.Just) {
                    return Data_Maybe.fromMaybe(v.value0)(Data_List.modifyAt(v1.value0)(addPrefixExp)(v.value0));
                };
                if (v1 instanceof Data_Maybe.Nothing) {
                    return new Data_List_Types.Cons({
                        prefix: new Prefix(base, Data_Decimal.fromNumber(p)),
                        baseUnit: unity$prime,
                        exponent: 1.0
                    }, v.value0);
                };
                throw new Error("Failed pattern match at Data.Units (line 172, column 3 - line 175, column 91): " + [ v1.constructor.name ]);
            })());
        };
    };
};
var eqPrefix = new Data_Eq.Eq(function (v) {
    return function (v1) {
        return Data_Eq.eq(Data_Decimal.eqDecimal)(v.value1)(Data_Semiring.zero(Data_Decimal.semiringDecimal)) && Data_Eq.eq(Data_Decimal.eqDecimal)(v1.value1)(Data_Semiring.zero(Data_Decimal.semiringDecimal)) || Data_Eq.eq(eqPrefixBase)(v.value0)(v1.value0) && Data_Eq.eq(Data_Decimal.eqDecimal)(v.value1)(v1.value1);
    };
});
var simplify = function (v) {
    var merge = function (units) {
        return {
            prefix: (Data_List_NonEmpty.head(units)).prefix,
            baseUnit: (Data_List_NonEmpty.head(units)).baseUnit,
            exponent: Data_Foldable.sum(Data_List_Types.foldableNonEmptyList)(Data_Semiring.semiringNumber)(Data_Functor.map(Data_List_Types.functorNonEmptyList)(function (v1) {
                return v1.exponent;
            })(units))
        };
    };
    var go = function ($221) {
        return (function ($222) {
            return (function ($223) {
                return Data_List.filter(function (x) {
                    return !(x.exponent === 0.0);
                })(Data_Functor.map(Data_List_Types.functorList)(merge)($223));
            })(groupBy$prime(function (u1) {
                return function (u2) {
                    return Data_Eq.eq(eqBaseUnit)(u1.baseUnit)(u2.baseUnit) && Data_Eq.eq(eqPrefix)(u1.prefix)(u2.prefix);
                };
            })(Data_List.concat($222)));
        })(Data_Functor.map(Data_List_Types.functorList)(Data_List_NonEmpty.toList)(groupBy$prime(function (u1) {
            return function (u2) {
                return Data_Eq.eq(eqBaseUnit)(u1.baseUnit)(u2.baseUnit);
            };
        })($221)));
    };
    return new DerivedUnit(go(v.value0));
};
var eqUnitType = new Data_Eq.Eq(function (v) {
    return function (v1) {
        if (v instanceof Standard && v1 instanceof Standard) {
            return true;
        };
        if (v instanceof NonStandard && v1 instanceof NonStandard) {
            return Data_Eq.eq(eqDerivedUnit)(v.value0.standardUnit)(v1.value0.standardUnit) && Data_Eq.eq(Data_Decimal.eqDecimal)(v.value0.factor)(v1.value0.factor);
        };
        return false;
    };
});
var eqDerivedUnit = new Data_Eq.Eq(function (u1) {
    return function (u2) {
        var removeUnity = Data_List.filter(function (u) {
            return longName(u.baseUnit) !== "unity";
        });
        var prepare = function ($224) {
            return Data_List.sortBy(Data_Ord.comparing(Data_Ord.ordString)(function ($225) {
                return shortName((function (v) {
                    return v.baseUnit;
                })($225));
            }))(runDerivedUnit(simplify($224)));
        };
        var list2 = prepare(u2);
        var list2$prime = removeUnity(list2);
        var list1 = prepare(u1);
        var list1$prime = removeUnity(list1);
        var globalPrefix = function (us) {
            var toPair = function (v) {
                if (v.value0 instanceof Decimal) {
                    return new Data_Pair.Pair(v.value1, Data_Semiring.zero(Data_Decimal.semiringDecimal));
                };
                if (v.value0 instanceof Binary) {
                    return new Data_Pair.Pair(Data_Semiring.zero(Data_Decimal.semiringDecimal), v.value1);
                };
                throw new Error("Failed pattern match at Data.Units (line 335, column 11 - line 335, column 50): " + [ v.constructor.name ]);
            };
            var prefixPair = function (v) {
                return Data_Functor.map(Data_Pair.functorPair)(function (v1) {
                    return Data_Semiring.mul(Data_Decimal.semiringDecimal)(v1)(Data_Decimal.fromNumber(v.exponent));
                })(toPair(v.prefix));
            };
            return Data_Functor.map(Data_Pair.functorPair)(Data_Newtype.un(Data_Newtype.newtypeAdditive)(Data_Monoid_Additive.Additive))(Data_Foldable.fold(Data_List_Types.foldableList)(Data_Pair.monoidPair(Data_Monoid_Additive.monoidAdditive(Data_Decimal.semiringDecimal)))(Data_Functor.map(Data_List_Types.functorList)(function ($226) {
                return Data_Functor.map(Data_Pair.functorPair)(Data_Monoid_Additive.Additive)(prefixPair($226));
            })(us)));
        };
        return Data_Eq.eq(Data_List_Types.eqList(eqBaseUnit))(Data_Functor.map(Data_List_Types.functorList)(function (v) {
            return v.baseUnit;
        })(list1$prime))(Data_Functor.map(Data_List_Types.functorList)(function (v) {
            return v.baseUnit;
        })(list2$prime)) && (Data_Eq.eq(Data_List_Types.eqList(Data_Eq.eqNumber))(Data_Functor.map(Data_List_Types.functorList)(function (v) {
            return v.exponent;
        })(list1$prime))(Data_Functor.map(Data_List_Types.functorList)(function (v) {
            return v.exponent;
        })(list2$prime)) && Data_Eq.eq(Data_Pair.eqPair(Data_Decimal.eqDecimal))(globalPrefix(list1))(globalPrefix(list2)));
    };
});
var eqBaseUnit = new Data_Eq.Eq(function (v) {
    return function (v1) {
        return v["long"] === v1["long"] && (v["short"] === v1["short"] && Data_Eq.eq(eqUnitType)(v.unitType)(v1.unitType));
    };
});
var semigroupDerivedUnit = new Data_Semigroup.Semigroup(function (v) {
    return function (v1) {
        return simplify(new DerivedUnit(Data_Semigroup.append(Data_List_Types.semigroupList)(v.value0)(v1.value0)));
    };
});
var monoidDerivedUnit = new Data_Monoid.Monoid(function () {
    return semigroupDerivedUnit;
}, unity);
var divideUnits = function (du1) {
    return function (du2) {
        return Data_Semigroup.append(semigroupDerivedUnit)(du1)(power(du2)(-1.0));
    };
};
var decimalPrefix = withPrefix(Decimal.value);
var exa = decimalPrefix(18.0);
var femto = decimalPrefix(-15.0);
var giga = decimalPrefix(9.0);
var hecto = decimalPrefix(2.0);
var kilo = decimalPrefix(3.0);
var mega = decimalPrefix(6.0);
var micro = decimalPrefix(-6.0);
var milli = decimalPrefix(-3.0);
var nano = decimalPrefix(-9.0);
var peta = decimalPrefix(15.0);
var pico = decimalPrefix(-12.0);
var tera = decimalPrefix(12.0);
var deci = decimalPrefix(-1.0);
var conversionFactor = function (v) {
    if (v.unitType instanceof Standard) {
        return Data_Semiring.one(Data_Decimal.semiringDecimal);
    };
    if (v.unitType instanceof NonStandard) {
        return v.unitType.value0.factor;
    };
    throw new Error("Failed pattern match at Data.Units (line 123, column 3 - line 125, column 50): " + [ v.unitType.constructor.name ]);
};
var centi = decimalPrefix(-2.0);
var binaryPrefix = withPrefix(Binary.value);
var exbi = binaryPrefix(60.0);
var gibi = binaryPrefix(30.0);
var kibi = binaryPrefix(10.0);
var mebi = binaryPrefix(20.0);
var pebi = binaryPrefix(50.0);
var tebi = binaryPrefix(40.0);
var yobi = binaryPrefix(80.0);
var zebi = binaryPrefix(70.0);
var baseToStandard = function (v) {
    if (v.unitType instanceof Standard) {
        return fromBaseUnit(v);
    };
    if (v.unitType instanceof NonStandard) {
        return v.unitType.value0.standardUnit;
    };
    throw new Error("Failed pattern match at Data.Units (line 117, column 3 - line 119, column 56): " + [ v.unitType.constructor.name ]);
};
var splitByDimension = function (v) {
    var standardUnit = function ($227) {
        return baseToStandard((function (v1) {
            return v1.baseUnit;
        })($227));
    };
    var removeExponent = function (v1) {
        if (v1.value0 instanceof Data_List_Types.Cons && v1.value0.value1 instanceof Data_List_Types.Nil) {
            return DerivedUnit.create(Data_List.singleton({
                exponent: 1.0,
                baseUnit: v1.value0.value0.baseUnit,
                prefix: v1.value0.value0.prefix
            }));
        };
        return v1;
    };
    var standardUnitWithoutExponent = function ($228) {
        return removeExponent(standardUnit($228));
    };
    var heuristic = function (b1) {
        return function (b2) {
            return Data_Semigroup.append(Data_Ordering.semigroupOrdering)(Data_Ord.compare(Data_Ord.ordBoolean)(isStandardUnit(b1.baseUnit))(isStandardUnit(b2.baseUnit)))(Data_Ord.compare(Data_Ord.ordNumber)(b2.exponent)(b1.exponent));
        };
    };
    var exponentWRT = function (base) {
        return function (u) {
            var removedExponent = function (u$prime) {
                var v1 = standardUnit(u$prime);
                if (v1.value0 instanceof Data_List_Types.Cons && v1.value0.value1 instanceof Data_List_Types.Nil) {
                    return v1.value0.value0.exponent;
                };
                return 1.0;
            };
            return (u.exponent * removedExponent(u)) / removedExponent(base);
        };
    };
    var reduce = function (us$prime) {
        var us = sortBy$prime(Data_Function.flip(heuristic))(us$prime);
        var first = Data_List_NonEmpty.head(us);
        var exp = Data_Foldable.sum(Data_List_Types.foldableNonEmptyList)(Data_Semiring.semiringNumber)(Data_Functor.map(Data_List_Types.functorNonEmptyList)(exponentWRT(first))(us));
        var convertTo = DerivedUnit.create(Data_List.singleton({
            exponent: exp,
            baseUnit: first.baseUnit,
            prefix: first.prefix
        }));
        return new Data_Tuple.Tuple(convertTo, DerivedUnit.create(Data_List_NonEmpty.toList(us)));
    };
    var transform = function ($229) {
        return Data_Functor.map(Data_List_Types.functorList)(reduce)(groupBy$prime(Data_Function.on(Data_Eq.eq(eqDerivedUnit))(standardUnitWithoutExponent))($229));
    };
    return transform(v.value0);
};
var toStandardUnit = function (v) {
    var convert = function (v1) {
        var toNum = function (v2) {
            if (v2 instanceof Decimal) {
                return Data_Decimal.fromNumber(10.0);
            };
            if (v2 instanceof Binary) {
                return Data_Decimal.fromNumber(2.0);
            };
            throw new Error("Failed pattern match at Data.Units (line 408, column 11 - line 409, column 11): " + [ v2.constructor.name ]);
        };
        var standardUnit = baseToStandard(v1.baseUnit);
        var factor = conversionFactor(v1.baseUnit);
        var exponent$prime = Data_Decimal.fromNumber(v1.exponent);
        return new Data_Tuple.Tuple(power(standardUnit)(v1.exponent), Data_Decimal.pow(Data_Semiring.mul(Data_Decimal.semiringDecimal)(Data_Decimal.pow(toNum(v1.prefix.value0))(v1.prefix.value1))(factor))(exponent$prime));
    };
    var converted = Data_Functor.map(Data_List_Types.functorList)(convert)(v.value0);
    var units$prime = Data_Foldable.foldMap(Data_List_Types.foldableList)(monoidDerivedUnit)(Data_Tuple.fst)(converted);
    var conv = Data_Foldable.product(Data_List_Types.foldableList)(Data_Decimal.semiringDecimal)(Data_Functor.map(Data_List_Types.functorList)(Data_Tuple.snd)(converted));
    return new Data_Tuple.Tuple(units$prime, conv);
};
var baseRepresentation = function (du) {
    if (Data_Eq.eq(eqDerivedUnit)(du)(unity)) {
        return Data_List.singleton(du);
    };
    if (Data_Boolean.otherwise) {
        var replace = function (u) {
            var v = runDerivedUnit(u);
            if (v instanceof Data_List_Types.Cons && v.value1 instanceof Data_List_Types.Nil) {
                var b$prime = (function () {
                    var $216 = v["value0"]["baseUnit"]["long"] === "gram";
                    if ($216) {
                        return {
                            baseUnit: {
                                "long": "kilogram",
                                "short": "kg",
                                unitType: v.value0.baseUnit.unitType
                            },
                            exponent: v.value0.exponent,
                            prefix: v.value0.prefix
                        };
                    };
                    return v.value0;
                })();
                return new DerivedUnit(Data_List.singleton(b$prime));
            };
            return u;
        };
        var du$prime = Data_Tuple.fst(toStandardUnit(du));
        var us = Data_Functor.map(Data_List_Types.functorList)(function ($230) {
            return replace(Data_Tuple.snd($230));
        })(splitByDimension(du$prime));
        return us;
    };
    throw new Error("Failed pattern match at Data.Units (line 291, column 1 - line 291, column 52): " + [ du.constructor.name ]);
};
var atto = decimalPrefix(-18.0);
module.exports = {
    decimalPrefix: decimalPrefix,
    binaryPrefix: binaryPrefix,
    removePrefix: removePrefix,
    simplify: simplify,
    splitByDimension: splitByDimension,
    baseRepresentation: baseRepresentation,
    makeStandard: makeStandard,
    makeNonStandard: makeNonStandard,
    toStandardUnit: toStandardUnit,
    prefixName: prefixName,
    toString: toString,
    power: power,
    divideUnits: divideUnits,
    unity: unity,
    atto: atto,
    femto: femto,
    pico: pico,
    nano: nano,
    micro: micro,
    centi: centi,
    deci: deci,
    hecto: hecto,
    milli: milli,
    kilo: kilo,
    mega: mega,
    giga: giga,
    tera: tera,
    peta: peta,
    exa: exa,
    kibi: kibi,
    mebi: mebi,
    gibi: gibi,
    tebi: tebi,
    pebi: pebi,
    exbi: exbi,
    zebi: zebi,
    yobi: yobi,
    eqPrefix: eqPrefix,
    eqDerivedUnit: eqDerivedUnit,
    showDerivedUnit: showDerivedUnit,
    semigroupDerivedUnit: semigroupDerivedUnit,
    monoidDerivedUnit: monoidDerivedUnit
};
