// Generated by purs version 0.14.5
"use strict";
var $foreign = require("./foreign.js");
var Control_Applicative = require("../Control.Applicative/index.js");
var Control_Bind = require("../Control.Bind/index.js");
var Control_Monad_State_Class = require("../Control.Monad.State.Class/index.js");
var DOM_HTML_Indexed_ButtonType = require("../DOM.HTML.Indexed.ButtonType/index.js");
var Data_Array = require("../Data.Array/index.js");
var Data_EuclideanRing = require("../Data.EuclideanRing/index.js");
var Data_Functor = require("../Data.Functor/index.js");
var Data_Maybe = require("../Data.Maybe/index.js");
var Data_Semigroup = require("../Data.Semigroup/index.js");
var Data_Show = require("../Data.Show/index.js");
var Data_Unit = require("../Data.Unit/index.js");
var Effect_Aff = require("../Effect.Aff/index.js");
var Effect_Class = require("../Effect.Class/index.js");
var Halogen_Aff_Util = require("../Halogen.Aff.Util/index.js");
var Halogen_Component = require("../Halogen.Component/index.js");
var Halogen_HTML = require("../Halogen.HTML/index.js");
var Halogen_HTML_Core = require("../Halogen.HTML.Core/index.js");
var Halogen_HTML_Elements = require("../Halogen.HTML.Elements/index.js");
var Halogen_HTML_Events = require("../Halogen.HTML.Events/index.js");
var Halogen_HTML_Properties = require("../Halogen.HTML.Properties/index.js");
var Halogen_Query_HalogenM = require("../Halogen.Query.HalogenM/index.js");
var Halogen_VDom_Driver = require("../Halogen.VDom.Driver/index.js");
var Web_HTML_Common = require("../Web.HTML.Common/index.js");
var Create = (function () {
    function Create(value0) {
        this.value0 = value0;
    };
    Create.create = function (value0) {
        return new Create(value0);
    };
    return Create;
})();
var AppendOneThousand = (function () {
    function AppendOneThousand() {

    };
    AppendOneThousand.value = new AppendOneThousand();
    return AppendOneThousand;
})();
var UpdateEveryTenth = (function () {
    function UpdateEveryTenth() {

    };
    UpdateEveryTenth.value = new UpdateEveryTenth();
    return UpdateEveryTenth;
})();
var Clear = (function () {
    function Clear() {

    };
    Clear.value = new Clear();
    return Clear;
})();
var Swap = (function () {
    function Swap() {

    };
    Swap.value = new Swap();
    return Swap;
})();
var Remove = (function () {
    function Remove(value0) {
        this.value0 = value0;
    };
    Remove.create = function (value0) {
        return new Remove(value0);
    };
    return Remove;
})();
var Select = (function () {
    function Select(value0) {
        this.value0 = value0;
    };
    Select.create = function (value0) {
        return new Select(value0);
    };
    return Select;
})();
var updateEveryTenth = (function () {
    var updateRowLabel = function (ix) {
        return function (row) {
            var $17 = Data_EuclideanRing.mod(Data_EuclideanRing.euclideanRingInt)(ix)(10) === 0;
            if ($17) {
                return {
                    label: row.label + " !!!",
                    id: row.id
                };
            };
            return row;
        };
    };
    return Data_Array.mapWithIndex(updateRowLabel);
})();
var swapRows = function (arr) {
    return function (index1) {
        return function (index2) {
            return Control_Bind.bind(Data_Maybe.bindMaybe)(Data_Array.index(arr)(index1))(function (rowA) {
                return Control_Bind.bind(Data_Maybe.bindMaybe)(Data_Array.index(arr)(index2))(function (rowB) {
                    return Control_Bind.bind(Data_Maybe.bindMaybe)(Data_Array.updateAt(index1)(rowB)(arr))(function (arrA) {
                        return Control_Bind.bind(Data_Maybe.bindMaybe)(Data_Array.updateAt(index2)(rowA)(arrA))(function (arrB) {
                            return Control_Applicative.pure(Data_Maybe.applicativeMaybe)(arrB);
                        });
                    });
                });
            });
        };
    };
};
var nouns = [ "table", "chair", "house", "bbq", "desk", "car", "pony", "cookie", "sandwich", "burger", "pizza", "mouse", "keyboard" ];
var colours = [ "red", "yellow", "blue", "green", "pink", "brown", "purple", "brown", "white", "black", "orange" ];
var class_ = function ($55) {
    return Halogen_HTML_Properties.class_(Web_HTML_Common.ClassName($55));
};
var colMd1 = [ class_("col-md-1") ];
var colMd4 = [ class_("col-md-4") ];
var footer = Halogen_HTML_Elements.span([ class_("preloadicon glyphicon glyphicon-remove"), Halogen_HTML_Properties.attr("aria-hidden")("true") ])([  ]);
var removeIcon = [ Halogen_HTML_Elements.span([ class_("glyphicon glyphicon-remove"), Halogen_HTML_Properties.attr("aria-hidden")("true") ])([  ]) ];
var renderActionButton = function (v) {
    return Halogen_HTML_Elements.div([ class_("col-sm-6 smallpad") ])([ Halogen_HTML_Elements.button([ Halogen_HTML_Properties.type_(Halogen_HTML_Core.isPropButtonType)(DOM_HTML_Indexed_ButtonType.ButtonButton.value), class_("btn btn-primary btn-block"), Halogen_HTML_Properties.id(v.id), Halogen_HTML_Properties.attr("ref")("text"), Halogen_HTML_Events.onClick(function (v1) {
        return v.action;
    }) ])([ Halogen_HTML_Core.text(v.label) ]) ]);
};
var spacer = Halogen_HTML_Elements.td([ class_("col-md-6") ])([  ]);
var renderRow = function (selectedId) {
    return function (row) {
        return Halogen_HTML_Elements.tr((function () {
            var $22 = selectedId === row.id;
            if ($22) {
                return [ class_("danger"), Halogen_HTML_Properties.attr("selected")("true") ];
            };
            return [  ];
        })())([ Halogen_HTML_Elements.td(colMd1)([ Halogen_HTML_Core.text(Data_Show.show(Data_Show.showInt)(row.id)) ]), Halogen_HTML_Elements.td(colMd4)([ Halogen_HTML_Elements.a([ Halogen_HTML_Events.onClick(function (v) {
            return new Select(row.id);
        }) ])([ Halogen_HTML_Core.text(row.label) ]) ]), Halogen_HTML_Elements.td(colMd1)([ Halogen_HTML_Elements.a([ Halogen_HTML_Events.onClick(function (v) {
            return new Remove(row.id);
        }) ])(removeIcon) ]), spacer ]);
    };
};
var buttons = [ {
    id: "run",
    label: "Create 1,000 rows",
    action: new Create(1000)
}, {
    id: "runlots",
    label: "Create 10,000 rows",
    action: new Create(10000)
}, {
    id: "add",
    label: "Append 1,000 rows",
    action: AppendOneThousand.value
}, {
    id: "update",
    label: "Update every 10th row",
    action: UpdateEveryTenth.value
}, {
    id: "clear",
    label: "Clear",
    action: Clear.value
}, {
    id: "swaprows",
    label: "Swap Rows",
    action: Swap.value
} ];
var jumbotron = Halogen_HTML_Elements.div([ class_("jumbotron") ])([ Halogen_HTML_Elements.div([ class_("row") ])([ Halogen_HTML_Elements.div([ class_("col-md-6") ])([ Halogen_HTML_Elements.h1_([ Halogen_HTML_Core.text("Halogen 6.1.3 (non-keyed)") ]) ]), Halogen_HTML_Elements.div([ class_("col-md-6") ])(Data_Functor.map(Data_Functor.functorArray)(Halogen_HTML.lazy(renderActionButton))(buttons)) ]) ]);
var adjectives = [ "pretty", "large", "big", "small", "tall", "short", "long", "handsome", "plain", "quaint", "clean", "elegant", "easy", "angry", "crazy", "helpful", "mushy", "odd", "unsightly", "adorable", "important", "inexpensive", "cheap", "expensive", "fancy" ];
var createRandomNRows = function (n) {
    return function (lastId) {
        return function () {
            return $foreign.createRandomNRowsImpl(adjectives, colours, nouns, n, lastId);
        };
    };
};
var app = (function () {
    var render = function (state) {
        return Halogen_HTML_Elements.div([ class_("container") ])([ jumbotron, Halogen_HTML_Elements.table([ class_("table table-hover table-striped test-data") ])([ Halogen_HTML_Elements.tbody_(Data_Functor.map(Data_Functor.functorArray)(renderRow(state.selectedId))(state.rows)) ]), footer ]);
    };
    var initialState = function (v) {
        return {
            rows: [  ],
            lastId: 0,
            selectedId: 0
        };
    };
    var handleAction = function (v) {
        if (v instanceof Create) {
            return Control_Bind.bind(Halogen_Query_HalogenM.bindHalogenM)(Control_Monad_State_Class.get(Halogen_Query_HalogenM.monadStateHalogenM))(function (state) {
                return Control_Bind.bind(Halogen_Query_HalogenM.bindHalogenM)(Effect_Class.liftEffect(Halogen_Query_HalogenM.monadEffectHalogenM(Effect_Aff.monadEffectAff))(createRandomNRows(v.value0)(state.lastId)))(function (newRows) {
                    return Control_Monad_State_Class.modify_(Halogen_Query_HalogenM.monadStateHalogenM)(function (v1) {
                        var $24 = {};
                        for (var $25 in v1) {
                            if ({}.hasOwnProperty.call(v1, $25)) {
                                $24[$25] = v1[$25];
                            };
                        };
                        $24.rows = newRows;
                        $24.lastId = state.lastId + v.value0 | 0;
                        return $24;
                    });
                });
            });
        };
        if (v instanceof AppendOneThousand) {
            return Control_Bind.bind(Halogen_Query_HalogenM.bindHalogenM)(Control_Monad_State_Class.get(Halogen_Query_HalogenM.monadStateHalogenM))(function (state) {
                return Control_Bind.bind(Halogen_Query_HalogenM.bindHalogenM)(Effect_Class.liftEffect(Halogen_Query_HalogenM.monadEffectHalogenM(Effect_Aff.monadEffectAff))(createRandomNRows(1000)(state.lastId)))(function (newRows) {
                    return Control_Monad_State_Class.modify_(Halogen_Query_HalogenM.monadStateHalogenM)(function (v1) {
                        var $28 = {};
                        for (var $29 in v1) {
                            if ({}.hasOwnProperty.call(v1, $29)) {
                                $28[$29] = v1[$29];
                            };
                        };
                        $28.rows = Data_Semigroup.append(Data_Semigroup.semigroupArray)(state.rows)(newRows);
                        $28.lastId = state.lastId + 1000 | 0;
                        return $28;
                    });
                });
            });
        };
        if (v instanceof UpdateEveryTenth) {
            var updateLabel = function (ix) {
                return function (row) {
                    var $31 = Data_EuclideanRing.mod(Data_EuclideanRing.euclideanRingInt)(ix)(10) === 0;
                    if ($31) {
                        var $32 = {};
                        for (var $33 in row) {
                            if ({}.hasOwnProperty.call(row, $33)) {
                                $32[$33] = row[$33];
                            };
                        };
                        $32.label = row.label + " !!!";
                        return $32;
                    };
                    return row;
                };
            };
            return Control_Monad_State_Class.modify_(Halogen_Query_HalogenM.monadStateHalogenM)(function (state) {
                var $35 = {};
                for (var $36 in state) {
                    if ({}.hasOwnProperty.call(state, $36)) {
                        $35[$36] = state[$36];
                    };
                };
                $35.rows = Data_Array.mapWithIndex(updateLabel)(state.rows);
                return $35;
            });
        };
        if (v instanceof Clear) {
            return Control_Monad_State_Class.modify_(Halogen_Query_HalogenM.monadStateHalogenM)(function (v1) {
                var $38 = {};
                for (var $39 in v1) {
                    if ({}.hasOwnProperty.call(v1, $39)) {
                        $38[$39] = v1[$39];
                    };
                };
                $38.rows = [  ];
                return $38;
            });
        };
        if (v instanceof Swap) {
            return Control_Bind.bind(Halogen_Query_HalogenM.bindHalogenM)(Control_Monad_State_Class.get(Halogen_Query_HalogenM.monadStateHalogenM))(function (state) {
                var v1 = swapRows(state.rows)(1)(998);
                if (v1 instanceof Data_Maybe.Nothing) {
                    return Control_Applicative.pure(Halogen_Query_HalogenM.applicativeHalogenM)(Data_Unit.unit);
                };
                if (v1 instanceof Data_Maybe.Just) {
                    return Control_Monad_State_Class.modify_(Halogen_Query_HalogenM.monadStateHalogenM)(function (v2) {
                        var $42 = {};
                        for (var $43 in v2) {
                            if ({}.hasOwnProperty.call(v2, $43)) {
                                $42[$43] = v2[$43];
                            };
                        };
                        $42.rows = v1.value0;
                        return $42;
                    });
                };
                throw new Error("Failed pattern match at Main (line 91, column 7 - line 93, column 49): " + [ v1.constructor.name ]);
            });
        };
        if (v instanceof Remove) {
            return Control_Monad_State_Class.modify_(Halogen_Query_HalogenM.monadStateHalogenM)(function (state) {
                var $46 = {};
                for (var $47 in state) {
                    if ({}.hasOwnProperty.call(state, $47)) {
                        $46[$47] = state[$47];
                    };
                };
                $46.rows = Data_Array.filter(function (r) {
                    return r.id !== v.value0;
                })(state.rows);
                return $46;
            });
        };
        if (v instanceof Select) {
            return Control_Bind.bind(Halogen_Query_HalogenM.bindHalogenM)(Control_Monad_State_Class.get(Halogen_Query_HalogenM.monadStateHalogenM))(function (state) {
                var $50 = state.selectedId === v.value0;
                if ($50) {
                    return Control_Applicative.pure(Halogen_Query_HalogenM.applicativeHalogenM)(Data_Unit.unit);
                };
                return Control_Monad_State_Class.modify_(Halogen_Query_HalogenM.monadStateHalogenM)(function (v1) {
                    var $51 = {};
                    for (var $52 in v1) {
                        if ({}.hasOwnProperty.call(v1, $52)) {
                            $51[$52] = v1[$52];
                        };
                    };
                    $51.selectedId = v.value0;
                    return $51;
                });
            });
        };
        throw new Error("Failed pattern match at Main (line 67, column 18 - line 104, column 40): " + [ v.constructor.name ]);
    };
    return Halogen_Component.mkComponent({
        initialState: initialState,
        render: render,
        "eval": Halogen_Component.mkEval({
            handleAction: handleAction,
            handleQuery: Halogen_Component.defaultEval.handleQuery,
            receive: Halogen_Component.defaultEval.receive,
            initialize: Halogen_Component.defaultEval.initialize,
            finalize: Halogen_Component.defaultEval.finalize
        })
    });
})();
var main = Halogen_Aff_Util.runHalogenAff(Control_Bind.bind(Effect_Aff.bindAff)(Halogen_Aff_Util.awaitBody)(function (body) {
    return Halogen_VDom_Driver.runUI(app)(Data_Unit.unit)(body);
}));
module.exports = {
    main: main,
    Create: Create,
    AppendOneThousand: AppendOneThousand,
    UpdateEveryTenth: UpdateEveryTenth,
    Clear: Clear,
    Swap: Swap,
    Remove: Remove,
    Select: Select,
    app: app,
    buttons: buttons,
    renderActionButton: renderActionButton,
    renderRow: renderRow,
    removeIcon: removeIcon,
    class_: class_,
    colMd1: colMd1,
    colMd4: colMd4,
    spacer: spacer,
    footer: footer,
    jumbotron: jumbotron,
    updateEveryTenth: updateEveryTenth,
    swapRows: swapRows,
    createRandomNRows: createRandomNRows,
    adjectives: adjectives,
    colours: colours,
    nouns: nouns,
    createRandomNRowsImpl: $foreign.createRandomNRowsImpl
};
