"use strict";
var __values = (this && this.__values) || function (o) {
    var m = typeof Symbol === "function" && o[Symbol.iterator], i = 0;
    if (m) return m.call(o);
    return {
        next: function () {
            if (o && i >= o.length) o = void 0;
            return { value: o && o[i++], done: !o };
        }
    };
};
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;
};
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 _1 = require(".");
var cursor_1 = require("../utils/cursor");
var actionCreator = typescript_fsa_1.default('react-map-editor/canvas');
exports.actions = {
    initMap: actionCreator('INIT_MAP'),
    draw: actionCreator('DRAW'),
    set: actionCreator('SET')
};
var initialState = init();
exports.default = dist_1.reducerWithInitialState(initialState)
    .case(exports.actions.initMap, function (state, payload) { return payload; })
    .case(exports.actions.set, function (state, payload) { return payload; });
var dragEpic = function (action$, state$) {
    return action$.pipe(typescript_fsa_redux_observable_1.ofAction(_1.input.actions.drag), operators_1.map(function (action) {
        var e = action.payload.event;
        var mode = state$.value.mode;
        return new cursor_1.default(((e.clientX - e.currentTarget.offsetLeft) / 32) >> 0, // TODO: unit=32px に依存しない位置参照(@hackforplay/next)に
        ((e.clientY - e.currentTarget.offsetTop) / 32) >> 0, state$.value.mode.cursorMode, mode.nib, action.payload.id);
    }), operators_1.filter(function (cursor) { return cursor.mode !== 'nope'; }), operators_1.distinctUntilChanged(function (x, y) { return x.isEqual(y); }), operators_1.map(function (pen) { return exports.actions.draw(pen); }));
};
var penEpic = function (action$, state$) {
    return action$.pipe(typescript_fsa_redux_observable_1.ofAction(exports.actions.draw), operators_1.filter(function (action) { return action.payload.mode === 'pen'; }), operators_1.map(function (action) {
        var e_1, _a;
        var _b = state$.value.canvas, tables = _b.tables, squares = _b.squares;
        var nib = action.payload.nib;
        if (!nib)
            throw new Error('Nib is null');
        var addFlag = false;
        var _loop_1 = function (item) {
            if (squares.every(function (s) { return s.index !== item.index; })) {
                addFlag = true;
                squares = squares.concat(item);
            }
        };
        try {
            for (var _c = __values(lodash_1.flattenDepth(nib, 2)), _d = _c.next(); !_d.done; _d = _c.next()) {
                var item = _d.value;
                _loop_1(item);
            }
        }
        catch (e_1_1) { e_1 = { error: e_1_1 }; }
        finally {
            try {
                if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
            }
            finally { if (e_1) throw e_1.error; }
        }
        return {
            tables: draw(tables, action.payload),
            squares: squares
        };
    }), operators_1.map(function (map) { return exports.actions.set(map); }));
};
var eraserEpic = function (action$, state$) {
    return action$.pipe(typescript_fsa_redux_observable_1.ofAction(exports.actions.draw), operators_1.filter(function (action) { return action.payload.mode === 'eraser'; }), operators_1.map(function (action) {
        var _a = state$.value.canvas, tables = _a.tables, squares = _a.squares;
        return {
            tables: draw(tables, action.payload),
            squares: squares
        };
    }), operators_1.map(function (map) { return exports.actions.set(map); }));
};
exports.epics = redux_observable_1.combineEpics(dragEpic, penEpic, eraserEpic);
/**
 * ３次元配列を置き換える.
 * @param origin 元の３次元配列
 * @param cursor 置き換える Cursor
 */
function draw(origin, cursor) {
    var e_2, _a;
    switch (cursor.mode) {
        case 'pen':
            if (!cursor.nib)
                return origin;
            var result_1 = lodash_1.cloneDeep(origin);
            cursor.nib.forEach(function (row, y) {
                return row.forEach(function (square, x) {
                    var layer = autoLayer(square.placement);
                    var X = cursor.x + x;
                    var Y = cursor.y + y;
                    if (layer < result_1.length &&
                        Y < result_1[layer].length &&
                        X < result_1[layer][Y].length) {
                        result_1[layer][Y][X] = square.index;
                    }
                });
            });
            return result_1;
        case 'eraser':
            try {
                for (var _b = __values(origin.entries()), _c = _b.next(); !_c.done; _c = _b.next()) {
                    var _d = __read(_c.value, 2), layer = _d[0], table = _d[1];
                    if (table[cursor.y][cursor.x] > -1) {
                        // 空白じゃないマスを見つけた.
                        // しかし, オートレイヤー状態では一番下のレイヤーは消せない
                        if (layer !== origin.length - 1) {
                            var result_2 = lodash_1.cloneDeep(origin);
                            result_2[layer][cursor.y][cursor.x] = -888; // ４桁にしたい
                            return result_2;
                        }
                    }
                }
            }
            catch (e_2_1) { e_2 = { error: e_2_1 }; }
            finally {
                try {
                    if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
                }
                finally { if (e_2) throw e_2.error; }
            }
            return origin; // 見つからなかった
        case 'nope':
            return origin;
    }
}
exports.draw = draw;
/**
 * マップの初期値
 */
function init() {
    var row = function () { return Array.from({ length: 10 }).map(function () { return -1; }); };
    var table = function () { return Array.from({ length: 10 }).map(function () { return row(); }); };
    return {
        tables: [table(), table(), table()],
        squares: []
    };
}
exports.init = init;
function autoLayer(placement) {
    switch (placement.type) {
        case 'Ground':
            return 2;
        case 'Wall':
        case 'Road':
        case 'Rug':
        case 'Barrier':
            return 1;
        case 'Float':
        case 'Sky':
            return 0;
    }
}
//# sourceMappingURL=canvas.js.map