"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var redux_observable_1 = require("redux-observable");
var operators_1 = require("rxjs/operators");
var typescript_fsa_1 = require("typescript-fsa");
var _1 = require(".");
var selection_1 = require("../utils/selection");
var reducerWithImmer_1 = require("./reducerWithImmer");
var typescript_fsa_redux_observable_1 = require("./typescript-fsa-redux-observable");
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 = reducerWithImmer_1.reducerWithImmer(initialState)
    .case(exports.actions.setPen, function (draft) {
    draft.cursorMode = draft.nib ? 'pen' : 'nope';
})
    .case(exports.actions.setEraser, function (draft) {
    draft.cursorMode = 'eraser';
})
    .case(exports.actions.setNib, function (draft, payload) {
    draft.nib = payload;
    draft.cursorMode = 'pen';
})
    .toReducer();
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