"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;
};
var __read = (this && this.__read) || function (o, n) {
    var m = typeof Symbol === "function" && o[Symbol.iterator];
    if (!m) return o;
    var i = m.call(o), r, ar = [], e;
    try {
        while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
    }
    catch (error) { e = { error: error }; }
    finally {
        try {
            if (r && !r.done && (m = i["return"])) m.call(i);
        }
        finally { if (e) throw e.error; }
    }
    return ar;
};
var __spread = (this && this.__spread) || function () {
    for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i]));
    return ar;
};
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 lodash_1 = require("lodash");
var typescript_fsa_redux_observable_1 = require("./typescript-fsa-redux-observable");
var actionCreator = typescript_fsa_1.default('react-map-editor/palette');
exports.actions = {
    mousedown: actionCreator('MOUSE_DOWN'),
    startSelection: actionCreator('START_SELECTION'),
    updateSelection: actionCreator('UPDATE_SELECTION'),
    confirmSelection: actionCreator('CONFIRM_SELECTION'),
    setSelection: actionCreator('SET_SELECTION'),
    addTileset: actionCreator('ADD_TILESET'),
    setTilesetMap: actionCreator('SET_TILESET_MAP')
};
var initialState = {
    tileSet: [],
    tileSetMap: {},
    selection: null
};
exports.default = dist_1.reducerWithInitialState(initialState)
    .case(exports.actions.setTilesetMap, function (state, payload) { return (__assign({}, state, { tileSetMap: payload, tileSet: lodash_1.values(payload) })); })
    .case(exports.actions.setSelection, function (state, payload) { return (__assign({}, state, { selection: payload })); });
var addTilesetEpic = function (action$, state$) {
    return action$.pipe(typescript_fsa_redux_observable_1.ofAction(exports.actions.addTileset), operators_1.map(function (action) { return action.payload.map(function (square) {
        var _a;
        return (_a = {}, _a[square.index] = square, _a);
    }); }), operators_1.map(function (array) {
        return Object.assign.apply(Object, __spread([__assign({}, state$.value.palette.tileSetMap)], array));
    }), operators_1.map(function (tilesetMap) { return exports.actions.setTilesetMap(tilesetMap); }));
};
var startSelectionEpic = function (action$, state$) {
    return action$.pipe(typescript_fsa_redux_observable_1.ofAction(exports.actions.startSelection), operators_1.map(function (action) {
        return exports.actions.setSelection({
            moving: true,
            start: action.payload,
            end: action.payload
        });
    }));
};
var updateSelectionEpic = function (action$, state$) {
    return action$.pipe(typescript_fsa_redux_observable_1.ofAction(exports.actions.updateSelection), operators_1.filter(function () { return state$.value.palette.selection !== null; }), operators_1.map(function (action) {
        var selection = state$.value.palette.selection;
        if (!selection)
            throw 'nope';
        return {
            moving: selection.moving,
            start: selection.start,
            end: action.payload
        };
    }), operators_1.filter(function (payload) { return payload.moving; }), operators_1.map(function (payload) { return exports.actions.setSelection(payload); }));
};
var confirmSelectionEpic = function (action$, state$) {
    return action$.pipe(typescript_fsa_redux_observable_1.ofAction(exports.actions.confirmSelection), operators_1.filter(function () { return state$.value.palette.selection !== null; }), operators_1.map(function (action) {
        var selection = state$.value.palette.selection;
        if (!selection)
            throw 'nope';
        return {
            moving: false,
            start: selection.start,
            end: selection.end
        };
    }), operators_1.map(function (payload) { return exports.actions.setSelection(payload); }));
};
exports.epics = redux_observable_1.combineEpics(addTilesetEpic, startSelectionEpic, updateSelectionEpic, confirmSelectionEpic);
//# sourceMappingURL=palette.js.map