import { __assign, __extends } from "tslib";
import { jsx as _jsx } from "react/jsx-runtime";
import { Component, isValidElement } from 'react';
import classnames from 'classnames';
import { GridColumnContext } from './ColumnContext';
import isNil from '../utils/isNil';
import getFromPath from '../utils/getFromPath';
import { hasOwnProperty } from '../utils/hasOwn';
var Cell = (function (_super) {
    __extends(Cell, _super);
    function Cell() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.getText = function (props) {
            var _a, _b;
            return (_a = props.data) === null || _a === void 0 ? void 0 : _a["" + ((_b = props.column) === null || _b === void 0 ? void 0 : _b.name)];
        };
        _this.onClick = function (e) {
            var _a = _this.props, data = _a.data, onCellClick = _a.column.onCellClick;
            if (typeof onCellClick === 'function') {
                onCellClick(data, e);
            }
        };
        return _this;
    }
    Cell.prototype.isInvalidRenderCellText = function (text) {
        return (text &&
            !isValidElement(text) &&
            Object.prototype.toString.call(text) === '[object Object]');
    };
    Cell.prototype.shouldComponentUpdate = function (nextProps) {
        if (nextProps.column && hasOwnProperty(nextProps.column, 'bodyRender')) {
            return true;
        }
        return this.getText(this.props) !== this.getText(nextProps);
    };
    Cell.prototype.render = function () {
        var _a;
        var _b = this.props, prefix = _b.prefix, column = _b.column, data = _b.data, pos = _b.pos;
        var _c = this.context, isValueEmptyInCtx = _c.isValueEmpty, defaultTextInCtx = _c.defaultText;
        var name = column.name, width = column.width, bodyRender = column.bodyRender, textAlign = column.textAlign, nowrap = column.nowrap, noWrap = column.noWrap, className = column.className, _d = column.defaultText, defaultText = _d === void 0 ? defaultTextInCtx : _d, _e = column.isValueEmpty, isValueEmpty = _e === void 0 ? isValueEmptyInCtx !== null && isValueEmptyInCtx !== void 0 ? isValueEmptyInCtx : isNil : _e;
        var text = getFromPath(data, name);
        if (isValueEmpty(text) && !isNil(defaultText)) {
            text = defaultText;
        }
        var tdProps;
        var colSpan;
        var rowSpan;
        if (typeof bodyRender === 'function') {
            text = bodyRender(data, pos, name);
            if (this.isInvalidRenderCellText(text)) {
                tdProps = text.props || {};
                colSpan = tdProps.colSpan;
                rowSpan = tdProps.rowSpan;
                text = text.children;
            }
        }
        if (this.isInvalidRenderCellText(text)) {
            text = null;
        }
        if (rowSpan === 0 || colSpan === 0) {
            return null;
        }
        return (_jsx("td", __assign({ className: classnames(prefix + "-grid-td", className, (_a = {},
                _a[prefix + "-grid-text-align-" + textAlign] = textAlign,
                _a[prefix + "-grid-nowrap"] = noWrap !== null && noWrap !== void 0 ? noWrap : nowrap,
                _a[prefix + "-grid-td-multiple-row"] = tdProps && tdProps.rowSpan > 1,
                _a[prefix + "-grid-td-selection"] = ['selection-column', 'selection-column-single'].indexOf(column.key) !== -1,
                _a[prefix + "-grid-td-expand"] = column.key === 'expand-column',
                _a[prefix + "-grid-td-break-word"] = typeof width !== undefined,
                _a)) }, tdProps, { onClick: this.onClick, "data-zv": 'v10.0.11' }, { children: text }), void 0));
    };
    Cell.contextType = GridColumnContext;
    return Cell;
}(Component));
export default Cell;
