"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Cell = void 0;
var common_1 = require("../../common");
var Cell = (function () {
    function Cell(args) {
        this._ = {
            table: undefined,
            td: undefined,
        };
        this._.table = args.table;
        this.ns = args.ns;
        this.row = args.row;
        this.column = args.column;
    }
    Cell.create = function (args) {
        return new Cell(args);
    };
    Cell.createFromKey = function (args) {
        var table = args.table, ns = args.ns, cellKey = args.cellKey;
        var _a = common_1.coord.cell.fromKey(cellKey), row = _a.row, column = _a.column;
        return new Cell({ table: table, ns: ns, row: row, column: column });
    };
    Cell.toKey = function (args) {
        return common_1.coord.cell.toKey(args.column, args.row);
    };
    Cell.fromKey = function (cellKey) {
        return common_1.coord.cell.fromKey(cellKey);
    };
    Cell.toPosition = function (ref) {
        return typeof ref === 'string' ? Cell.fromKey(ref) : ref;
    };
    Cell.toRangePositions = function (args) {
        var range = common_1.coord.range.fromKey(args.range);
        var start = range.left;
        var end = range.right;
        if (range.type === 'COLUMN') {
            start.row = 0;
            end.row = args.totalRows - 1;
        }
        if (range.type === 'ROW') {
            start.column = 0;
            end.column = args.totalColumns - 1;
        }
        return { start: start, end: end };
    };
    Cell.changeEvent = function (args) {
        var cell = args.cell, from = args.from, to = args.to;
        var value = { from: from, to: to };
        var isChanged;
        var payload = {
            cell: cell,
            value: value,
            get isChanged() {
                return isChanged === undefined ? (isChanged = !common_1.R.equals(value.from, value.to)) : isChanged;
            },
            isCancelled: false,
            isModified: false,
            cancel: function () {
                payload.isCancelled = true;
            },
            modify: function (change) {
                value.to = change;
                payload.isModified = true;
            },
        };
        return payload;
    };
    Object.defineProperty(Cell.prototype, "isDisposed", {
        get: function () {
            return this._.table.isDestroyed;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(Cell.prototype, "key", {
        get: function () {
            var row = this.row;
            var column = this.column;
            return Cell.toKey({ column: column, row: row });
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(Cell.prototype, "td", {
        get: function () {
            this._.td = this._.td || this._.table.getCell(this.row, this.column);
            return this._.td;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(Cell.prototype, "size", {
        get: function () {
            var width = this.width;
            var height = this.height;
            return { width: width, height: height };
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(Cell.prototype, "width", {
        get: function () {
            return this.td.offsetWidth;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(Cell.prototype, "height", {
        get: function () {
            return this.td.offsetHeight;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(Cell.prototype, "data", {
        get: function () {
            var data = this._.table.getDataAtCell(this.row, this.column) || {};
            if (typeof data === 'object') {
                var value = data.value;
                var props = data.props || {};
                var error = data.error;
                return { value: value, props: props, error: error };
            }
            else {
                return {};
            }
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(Cell.prototype, "siblings", {
        get: function () {
            var table = this._.table;
            var cell = this;
            var ns = cell.ns, row = cell.row, column = cell.column;
            return {
                get left() {
                    var column = cell.column - 1;
                    return column < 0 ? undefined : Cell.create({ table: table, ns: ns, row: row, column: column });
                },
                get right() {
                    var column = cell.column + 1;
                    return column > table.countCols() - 1 ? undefined : Cell.create({ table: table, ns: ns, row: row, column: column });
                },
                get top() {
                    var row = cell.row - 1;
                    return row < 0 ? undefined : Cell.create({ table: table, ns: ns, row: row, column: column });
                },
                get bottom() {
                    var row = cell.row + 1;
                    return row < 0 ? undefined : Cell.create({ table: table, ns: ns, row: row, column: column });
                },
            };
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(Cell.prototype, "rowspan", {
        get: function () {
            return common_1.defaultValue(Cell.props(this.data.props).merge.rowspan, 1);
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(Cell.prototype, "colspan", {
        get: function () {
            return common_1.defaultValue(Cell.props(this.data.props).merge.colspan, 1);
        },
        enumerable: false,
        configurable: true
    });
    Cell.prototype.toString = function () {
        common_1.Schema.uri.create.cell(this.ns, this.key);
    };
    Cell.isEmpty = common_1.util.cell.value.isEmptyCell;
    Cell.isEmptyProps = common_1.util.cell.value.isEmptyCellProps;
    Cell.isEmptyValue = common_1.util.cell.value.isEmptyCellValue;
    Cell.diff = common_1.util.cell.value.cellDiff;
    Cell.props = common_1.util.toGridCellProps;
    return Cell;
}());
exports.Cell = Cell;
