"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 immer_1 = require("immer");
var redux_observable_1 = require("redux-observable");
var typescript_fsa_1 = require("typescript-fsa");
var dist_1 = require("typescript-fsa-reducers/dist");
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.draw = immer_1.default(function (_a, cursor) {
    var e_1, _b, e_2, _c, e_3, _d;
    var tables = _a.tables, squares = _a.squares;
    if (!cursor.nib || !Array.isArray(tables))
        return;
    if (cursor.mode === 'pen') {
        try {
            for (var _e = __values(cursor.nib.entries()), _f = _e.next(); !_f.done; _f = _e.next()) {
                var _g = __read(_f.value, 2), y = _g[0], row = _g[1];
                var _loop_1 = function (x, tile) {
                    // drawing
                    var layer = autoLayer(tile.placement);
                    var X = cursor.x + x;
                    var Y = cursor.y + y;
                    var table = tables && tables[layer];
                    var tableRow = table && table[Y];
                    if (0 <= X && X < tableRow.length) {
                        tableRow[X] = tile.index;
                    }
                    // add tile info
                    if (squares.every(function (s) { return s.index !== tile.index; })) {
                        squares.push({
                            index: tile.index,
                            placement: tile.placement,
                            tile: {
                                size: [32, 32],
                                image: {
                                    type: 'url',
                                    src: tile.src
                                },
                                author: tile.author
                            }
                        });
                    }
                };
                try {
                    for (var _h = (e_2 = void 0, __values(row.entries())), _j = _h.next(); !_j.done; _j = _h.next()) {
                        var _k = __read(_j.value, 2), x = _k[0], tile = _k[1];
                        _loop_1(x, tile);
                    }
                }
                catch (e_2_1) { e_2 = { error: e_2_1 }; }
                finally {
                    try {
                        if (_j && !_j.done && (_c = _h.return)) _c.call(_h);
                    }
                    finally { if (e_2) throw e_2.error; }
                }
            }
        }
        catch (e_1_1) { e_1 = { error: e_1_1 }; }
        finally {
            try {
                if (_f && !_f.done && (_b = _e.return)) _b.call(_e);
            }
            finally { if (e_1) throw e_1.error; }
        }
    }
    if (cursor.mode === 'eraser') {
        try {
            for (var _l = __values(tables.entries()), _m = _l.next(); !_m.done; _m = _l.next()) {
                var _o = __read(_m.value, 2), layer = _o[0], table = _o[1];
                var tableRow = table && table[cursor.y];
                var index = tableRow && tableRow[cursor.x];
                if (index > -1) {
                    // 空白じゃないマスを見つけた.
                    // しかし, オートレイヤー状態では一番下のレイヤーは消せない
                    if (layer !== tables.length - 1) {
                        tableRow[cursor.x] = -888; // ４桁にしたい
                    }
                }
            }
        }
        catch (e_3_1) { e_3 = { error: e_3_1 }; }
        finally {
            try {
                if (_m && !_m.done && (_d = _l.return)) _d.call(_l);
            }
            finally { if (e_3) throw e_3.error; }
        }
    }
});
exports.default = dist_1.reducerWithInitialState(initialState)
    .case(exports.actions.initMap, function (state, payload) { return payload; })
    .case(exports.actions.set, function (state, payload) { return payload; })
    .case(exports.actions.draw, exports.draw);
exports.epics = redux_observable_1.combineEpics();
/**
 * マップの初期値
 */
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 'Nope':
        case 'Ground':
            return 2;
        case 'Wall':
        case 'Road':
        case 'Rug':
        case 'Barrier':
            return 1;
        case 'Float':
        case 'Sky':
            return 0;
    }
}
//# sourceMappingURL=canvas.js.map