"use strict";
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 });
function getBounds(selection) {
    var start = selection.start, end = selection.end;
    return {
        top: Math.min(start.row, end.row),
        right: Math.max(start.col, end.col),
        bottom: Math.max(start.row, end.row),
        left: Math.min(start.col, end.col)
    };
}
exports.getBounds = getBounds;
/**
 * 選択範囲から num のマトリクスを生成する
 * @param selection 選択範囲
 * @returns 画像の表示順を表す数値の二次元配列
 * e.g.
 * [
 *    [0, 1],
 *    [8, 9],
 *    [16, 17]
 * ]
 */
function getMatrix(selection) {
    var start = selection.start, end = selection.end;
    if (start.row === end.row) {
        // 行をまたがない => 単純に連番
        return [range(start.num, end.num)];
    }
    else if (start.row > end.row) {
        // 逆
        return getMatrix({
            page: selection.page,
            start: selection.end,
            end: selection.start
        });
    }
    else {
        // 複数行にまたがる => column を動的に求める
        var tableColumn_1 = getTableColumn(end);
        var height = end.row - start.row + 1;
        var width_1 = Math.abs(start.col - end.col);
        var left_1 = start.col < end.col ? start.col : end.col;
        return Array.from({ length: height }).map(function (_, y) {
            var begin = left_1 + (start.row + y) * tableColumn_1;
            return range(begin, begin + width_1);
        });
    }
}
exports.getMatrix = getMatrix;
function getTableColumn(pos) {
    if (pos.row === 0) {
        throw new Error('getTableColumn can not calcurate when pos.row is zero.');
    }
    return (pos.num - pos.col) / pos.row;
}
function range(a, b) {
    var _a = __read(a < b ? [a, b] : [b, a], 2), min = _a[0], max = _a[1];
    var array = [];
    for (var index = min; index <= max; index++) {
        array.push(index);
    }
    return array;
}
//# sourceMappingURL=selection.js.map