"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SHARED_DATA_KEY = void 0;
const tslib_1 = require("tslib");
const react_1 = tslib_1.__importDefault(require("react"));
const material_table_1 = require("material-table");
const cloneDeep_1 = tslib_1.__importDefault(require("lodash/cloneDeep"));
const set_1 = tslib_1.__importDefault(require("lodash/set"));
exports.SHARED_DATA_KEY = 'sharedRowData';
function mapColumns(columns) {
    return columns.map((i) => {
        return {
            ...i,
            editComponent: (props) => {
                /* eslint-disable react/prop-types */
                const onChange = (newValue) => {
                    // 设置共享数据
                    if (!columns[exports.SHARED_DATA_KEY]) {
                        columns[exports.SHARED_DATA_KEY] = cloneDeep_1.default(props.rowData);
                    }
                    set_1.default(columns[exports.SHARED_DATA_KEY], props.columnDef.field, newValue);
                    props.onChange(newValue);
                };
                const onRowDataChange = (newRowData) => {
                    columns[exports.SHARED_DATA_KEY] = newRowData;
                    props.onRowDataChange(newRowData);
                };
                /* eslint-enable react/prop-types */
                return i.editComponent ? (i.editComponent({ ...props, onChange, onRowDataChange })) : (react_1.default.createElement(material_table_1.MTableEditField, Object.assign({}, props, { onChange: onChange, onRowDataChange: onRowDataChange })));
            },
            editable: typeof i.editable === 'function'
                ? (columnDef, rowData) => {
                    return i.editable(columnDef, columns[exports.SHARED_DATA_KEY] || rowData);
                }
                : i.editable,
        };
    });
}
exports.default = mapColumns;
