"use strict";
var __assign = (this && this.__assign) || Object.assign || function(t) {
    for (var s, i = 1, n = arguments.length; i < n; i++) {
        s = arguments[i];
        for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
            t[p] = s[p];
    }
    return t;
};
Object.defineProperty(exports, "__esModule", { value: true });
var typescript_fsa_1 = require("typescript-fsa");
var dist_1 = require("typescript-fsa-reducers/dist");
var redux_observable_1 = require("redux-observable");
var operators_1 = require("rxjs/operators");
var typescript_fsa_redux_observable_1 = require("./typescript-fsa-redux-observable");
var _1 = require(".");
var selection_1 = require("../utils/selection");
var actionCreator = typescript_fsa_1.default('react-map-editor/mode');
exports.actions = {
    setPen: actionCreator('USE_PEN'),
    setEraser: actionCreator('USE_ERASER'),
    setNib: actionCreator('SET_NIB')
};
var initialState = {
    cursorMode: 'nope',
    nib: null
};
exports.default = dist_1.reducerWithInitialState(initialState)
    .case(exports.actions.setPen, function (state) { return (__assign({}, state, { cursorMode: state.nib ? 'pen' : 'nope' })); })
    .case(exports.actions.setEraser, function (state) { return (__assign({}, state, { cursorMode: 'eraser' })); })
    .case(exports.actions.setNib, function (state, payload) { return (__assign({}, state, { nib: payload, cursorMode: 'pen' })); });
var nibEpic = function (action$, state$) {
    return action$.pipe(typescript_fsa_redux_observable_1.ofActionWithPayload(_1.palette.actions.setSelection), operators_1.map(function (action) {
        var tileSet = state$.value.palette.tileSet;
        var matrix = selection_1.getMatrix(action.payload);
        var nib = matrix.map(function (row) { return row.map(function (num) { return tileSet[num]; }); });
        return exports.actions.setNib(nib);
    }));
};
exports.epics = redux_observable_1.combineEpics(nibEpic);
//# sourceMappingURL=mode.js.map