"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.registerCellRenderer = exports.cellRenderer = void 0;
var React = require("react");
var ReactDOMServer = require("react-dom/server");
var common_1 = require("../common");
var css = require("../styles/global.cell");
var CLASS = css.CLASS;
var CELL = CLASS.CELL, GRID = CLASS.GRID;
exports.cellRenderer = function (grid, factory) {
    var CACHE = {};
    function toHtml(args) {
        var el = toElement(args);
        return ReactDOMServer.renderToString(el);
    }
    function toElement(args) {
        var row = args.row, column = args.column, cell = args.cell;
        var child = factory.cell({ row: row, column: column, cell: cell });
        var isHtml = typeof child === 'string' && child.startsWith('<');
        var props = cell ? cell.props || {} : {};
        var style = props.style || {};
        var view = (props.view && props.view.cell) || common_1.constants.DEFAULT.CELL.PROPS.view.cell;
        var className = CELL.BASE;
        var add = function (isRequired, value) {
            return (className = isRequired && value ? className + " " + value : className);
        };
        add(isHtml, CELL.MARKDOWN);
        add(row === 0, GRID.FIRST.ROW);
        add(column === 0, GRID.FIRST.COLUMN);
        add(style.bold, CELL.BOLD);
        add(style.italic, CELL.ITALIC);
        add(style.underline, CELL.UNDERLINE);
        add(common_1.func.isFormula(cell && cell.value), CELL.FORMULA);
        add(Boolean(view.className), view.className);
        add(Boolean(cell && cell.error), CELL.ERROR);
        if (isHtml) {
            return React.createElement("div", { className: className, dangerouslySetInnerHTML: { __html: child } });
        }
        else {
            return React.createElement("div", { className: className }, child);
        }
    }
    function toCellHash(args) {
        var row = args.row, column = args.column, cell = args.cell;
        return cell ? cell.hash || common_1.util.gridCellHash(grid, common_1.coord.cell.toKey(column, row), cell) : '-';
    }
    function toMemoizedHtml(args) {
        var row = args.row, column = args.column, cell = args.cell;
        var hash = toCellHash({ row: row, column: column, cell: cell });
        var key = row + ":" + column + "/" + hash;
        if (CACHE[key]) {
            return CACHE[key];
        }
        var html = toHtml(args);
        CACHE[key] = html;
        return html;
    }
    var fn = function (instance, td, row, column, prop, value, cellProps) {
        if (!grid.isDisposed) {
            td.innerHTML = toMemoizedHtml({ td: td, row: row, column: column, cell: value });
        }
        return td;
    };
    return fn;
};
function registerCellRenderer(Table, grid, factory) {
    var renderers = Table.renderers;
    var fn = renderers.registerRenderer;
    fn(common_1.constants.DEFAULT.CELL.RENDERER, exports.cellRenderer(grid, factory));
}
exports.registerCellRenderer = registerCellRenderer;
