// Generated by purs version 0.12.5
"use strict";
var Control_Bind = require("../Control.Bind/index.js");
var Data_Array = require("../Data.Array/index.js");
var Data_Either = require("../Data.Either/index.js");
var Data_Map = require("../Data.Map/index.js");
var Data_Ord = require("../Data.Ord/index.js");
var Data_Semigroup = require("../Data.Semigroup/index.js");
var Data_Set = require("../Data.Set/index.js");
var Data_Show = require("../Data.Show/index.js");
var Data_Unfoldable = require("../Data.Unfoldable/index.js");
var Insect_Environment = require("../Insect.Environment/index.js");
var Insect_Format = require("../Insect.Format/index.js");
var Insect_Interpreter = require("../Insect.Interpreter/index.js");
var Insect_Parser = require("../Insect.Parser/index.js");
var Text_Parsing_Parser = require("../Text.Parsing.Parser/index.js");
var supportedUnits = (function () {
    var toStrs = function (v) {
        return v.value1;
    };
    var toArray = function (v) {
        return Control_Bind.bind(Control_Bind.bindArray)(v.value0)(toStrs);
    };
    return Data_Array.sort(Data_Ord.ordString)(Data_Semigroup.append(Data_Semigroup.semigroupArray)(toArray(Insect_Parser.normalUnitDict))(Data_Semigroup.append(Data_Semigroup.semigroupArray)(toArray(Insect_Parser.imperialUnitDict))([ "d", "t" ])));
})();
var msgTypeToString = function (v) {
    if (v instanceof Insect_Interpreter.Info) {
        return "info";
    };
    if (v instanceof Insect_Interpreter["Error"]) {
        return "error";
    };
    if (v instanceof Insect_Interpreter.Value) {
        return "value";
    };
    if (v instanceof Insect_Interpreter.ValueSet) {
        return "value-set";
    };
    throw new Error("Failed pattern match at Insect (line 42, column 1 - line 42, column 39): " + [ v.constructor.name ]);
};
var repl = function (fmt) {
    return function (env) {
        return function (userInput) {
            var v = Insect_Parser.parseInsect(env)(userInput);
            if (v instanceof Data_Either.Left) {
                var pos = Text_Parsing_Parser.parseErrorPosition(v.value0);
                return {
                    msg: Insect_Format.format(fmt)([ Insect_Format.optional(Insect_Format.text("  ")), Insect_Format.error("Parse error at position " + (Data_Show.show(Data_Show.showInt)(pos.column) + ": ")), Insect_Format.text(Text_Parsing_Parser.parseErrorMessage(v.value0)) ]),
                    msgType: "error",
                    newEnv: env
                };
            };
            if (v instanceof Data_Either.Right) {
                var ans = Insect_Interpreter.runInsect(env)(v.value0);
                if (ans.msg instanceof Insect_Interpreter.Message) {
                    return {
                        msgType: msgTypeToString(ans.msg.value0),
                        msg: Insect_Format.format(fmt)(ans.msg.value1),
                        newEnv: ans.newEnv
                    };
                };
                if (ans.msg instanceof Insect_Interpreter.MQuit) {
                    return {
                        msgType: "quit",
                        msg: "",
                        newEnv: ans.newEnv
                    };
                };
                if (ans.msg instanceof Insect_Interpreter.MClear) {
                    return {
                        msgType: "clear",
                        msg: "",
                        newEnv: ans.newEnv
                    };
                };
                throw new Error("Failed pattern match at Insect (line 68, column 10 - line 80, column 36): " + [ ans.msg.constructor.name ]);
            };
            throw new Error("Failed pattern match at Insect (line 53, column 3 - line 80, column 36): " + [ v.constructor.name ]);
        };
    };
};
var initialEnvironment = Insect_Environment.initialEnvironment;
var identifiers = function (env) {
    return Data_Set.toUnfoldable(Data_Unfoldable.unfoldableArray)(Data_Map.keys(env.values));
};
var functions = function (env) {
    return Data_Semigroup.append(Data_Semigroup.semigroupArray)(Data_Set.toUnfoldable(Data_Unfoldable.unfoldableArray)(Data_Map.keys(env.functions)))([ "sum", "product" ]);
};
var fmtPlain = Insect_Format.fmtPlain;
var fmtJqueryTerminal = Insect_Format.fmtJqueryTerminal;
var fmtConsole = Insect_Format.fmtConsole;
var commands = Insect_Parser.commands;
module.exports = {
    repl: repl,
    initialEnvironment: initialEnvironment,
    supportedUnits: supportedUnits,
    fmtPlain: fmtPlain,
    fmtJqueryTerminal: fmtJqueryTerminal,
    fmtConsole: fmtConsole,
    commands: commands,
    functions: functions,
    identifiers: identifiers
};
