"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 rxjs_1 = require("rxjs");
var operators_1 = require("rxjs/operators");
var next_1 = require("@hackforplay/next");
var lodash_1 = require("lodash");
var typescript_fsa_redux_observable_1 = require("./typescript-fsa-redux-observable");
var _1 = require(".");
var actionCreator = typescript_fsa_1.default('react-map-editor/asset');
exports.actions = {
    loadAsset: actionCreator.async('LOAD_ASSET')
};
var initialState = {
    loading: false,
    images: []
};
exports.default = dist_1.reducerWithInitialState(initialState)
    .case(exports.actions.loadAsset.started, function (state) { return (__assign({}, state, { loading: true })); })
    .case(exports.actions.loadAsset.done, function (state, action) { return (__assign({}, state, { loading: false, images: action.result })); });
exports.initMapEpic = function (action$) {
    return action$.pipe(typescript_fsa_redux_observable_1.ofAction(_1.canvas.actions.initMap), operators_1.map(function (action) { return exports.actions.loadAsset.started(action.payload.squares); }));
};
exports.loadAssetEpic = function (action$, state$) {
    return action$.pipe(typescript_fsa_redux_observable_1.ofAction(exports.actions.loadAsset.started), operators_1.mergeMap(function (action) {
        var input = {
            map: {
                tables: [[[-1]]],
                squares: action.payload
            },
            assets: {
                images: state$.value.asset.images
            },
            screen: {
                width: 0,
                height: 0
            }
        };
        return rxjs_1.from(next_1.loadImages(input)).pipe(operators_1.map(function (scene) {
            return exports.actions.loadAsset.done({
                result: scene.assets.images,
                params: action.payload
            });
        }));
    }));
};
exports.drawNewSquareEpic = function (action$, state$) {
    return action$.pipe(typescript_fsa_redux_observable_1.ofAction(_1.canvas.actions.draw), operators_1.filter(function (action) { return action.payload.mode === 'pen'; }), operators_1.map(function (action) {
        return lodash_1.flatten(action.payload.nib).filter(function (square) {
            return (square.index > -1 &&
                state$.value.asset.images.every(function (img) { return img.index !== square.index; }));
        });
    }), operators_1.filter(function (array) { return array.length > 0; }), operators_1.map(function (array) { return exports.actions.loadAsset.started(array); }));
};
exports.epics = redux_observable_1.combineEpics(exports.initMapEpic, exports.loadAssetEpic, exports.drawNewSquareEpic);
//# sourceMappingURL=asset.js.map