"use strict";
var __values = (this && this.__values) || function(o) {
    var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
    if (m) return m.call(o);
    if (o && typeof o.length === "number") return {
        next: function () {
            if (o && i >= o.length) o = void 0;
            return { value: o && o[i++], done: !o };
        }
    };
    throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.resizeSceneMap = void 0;
var immer_1 = require("immer");
/**
 * マップの大きさを変更する
 * @param editing state
 * @param rows 新しい行の数
 * @param cols 新しい列の数
 */
function resizeSceneMap(editing, rows, cols) {
    var patches = [];
    var sceneMap = immer_1.default(editing.sceneMap, function (draft) {
        var e_1, _a, e_2, _b;
        try {
            for (var _c = __values(draft.tables), _d = _c.next(); !_d.done; _d = _c.next()) {
                var table = _d.value;
                var diffRows = rows - table.length;
                if (diffRows > 0) {
                    // 足りない行を追加
                    for (var y = 0; y < diffRows; y++) {
                        table.push(Array.from({ length: cols }).map(function () { return -1; }));
                    }
                }
                if (diffRows < 0) {
                    // 多い行を削る
                    table.length = rows;
                }
                try {
                    for (var table_1 = (e_2 = void 0, __values(table)), table_1_1 = table_1.next(); !table_1_1.done; table_1_1 = table_1.next()) {
                        var row = table_1_1.value;
                        var diffCols = cols - row.length;
                        if (diffCols > 0) {
                            // 足りない列を追加
                            for (var x = 0; x < diffCols; x++) {
                                row.push(-1);
                            }
                        }
                        if (diffCols < 0) {
                            // 多い列を削る
                            row.length = cols;
                        }
                    }
                }
                catch (e_2_1) { e_2 = { error: e_2_1 }; }
                finally {
                    try {
                        if (table_1_1 && !table_1_1.done && (_b = table_1.return)) _b.call(table_1);
                    }
                    finally { if (e_2) throw e_2.error; }
                }
            }
        }
        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; }
        }
    }, function (_, invertPatches) {
        patches = invertPatches;
    });
    if (patches.length === 0) {
        return editing;
    }
    return {
        sceneMap: sceneMap,
        undoPatches: editing.undoPatches.concat({
            patches: patches
        })
    };
}
exports.resizeSceneMap = resizeSceneMap;
//# sourceMappingURL=resizeSceneMap.js.map