"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const jsx_runtime_1 = require("react/jsx-runtime");
const React = tslib_1.__importStar(require("react"));
const icons_1 = require("@gravity-ui/icons");
const uikit_1 = require("@gravity-ui/uikit");
const __1 = require("../index.js");
const utils_1 = require("../../utils/index.js");
function getMarkerId(index) {
    if (isNaN(index)) {
        return undefined;
    }
    return `marker-${index}`;
}
const b = (0, utils_1.block)('table');
class Table extends React.Component {
    render() {
        const { content, legend, hideLegend, marker = 'disk', className, caption } = this.props;
        if (!content || !content.length || !content[0].length) {
            return null;
        }
        return ((0, jsx_runtime_1.jsxs)("div", { className: b(null, className), role: 'table', "aria-label": caption, children: [this.renderTable(content, marker, legend), legend && !hideLegend && this.renderLegend(legend, marker)] }));
    }
    renderTable(content, marker, legend) {
        const justify = this.getDefaultJustify(content, this.props.justify);
        return ((0, jsx_runtime_1.jsx)("div", { className: b('table'), role: 'rowgroup', children: content.map((row, i) => ((0, jsx_runtime_1.jsx)("div", { className: b('row'), role: 'row', children: row.map((cell, j) => ((0, jsx_runtime_1.jsx)("div", { className: b('cell', { justify: justify[j] }), role: 'cell', children: legend && i && j ? (this.renderMarker(marker, cell)) : ((0, jsx_runtime_1.jsx)(__1.HTML, { children: cell })) }, j))) }, i))) }));
    }
    renderMarker(type, cell) {
        return ((0, jsx_runtime_1.jsx)("div", { "aria-labelledby": getMarkerId(Number(cell)), className: b('marker', { type, index: String(cell) }), children: type === 'tick' ? ((0, jsx_runtime_1.jsx)(uikit_1.Icon, { size: 20, className: b('marker_tick', { check: Number(cell) === 1 }), data: Number(cell) === 1 ? icons_1.Check : icons_1.Minus })) : null }));
    }
    renderLegend(legend, marker) {
        return ((0, jsx_runtime_1.jsx)("div", { className: b('legend'), children: legend.map((item, index) => ((0, jsx_runtime_1.jsxs)("div", { className: b('legend-item'), children: [this.renderMarker(marker, String(index)), (0, jsx_runtime_1.jsx)(__1.YFMWrapper, { className: b('legent-item-text'), content: item, modifiers: { constructor: true }, id: getMarkerId(index) })] }, item))) }));
    }
    getDefaultJustify(content, justify) {
        return justify || new Array(content[0].length).fill('center');
    }
}
exports.default = Table;
//# sourceMappingURL=Table.js.map
