"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TableEditableCell = void 0;
const react_1 = __importStar(require("react"));
const Text_1 = require("../Text");
const Icon_1 = require("../Icon");
const icons_1 = require("../../assets/images/icons");
exports.TableEditableCell = ({ cell, data, updateData, setSkipTableReset, }) => {
    const [currentValue, setCurrentValue] = react_1.useState(cell.value);
    const [isEditting, setIsEditting] = react_1.useState(false);
    const isOnClickEditor = cell.column.editor &&
        (cell.column.editor.name === 'DateInput' ||
            cell.column.editor.name === 'Select');
    const editCell = () => {
        if (cell.column.editor)
            setIsEditting(true);
    };
    const saveNewData = (value) => {
        setSkipTableReset(true);
        updateData(data.map((row, index) => {
            if (index === cell.row.index) {
                return Object.assign(Object.assign({}, data[index]), { [cell.column.id]: value !== undefined ? value : currentValue });
            }
            return row;
        }));
        setIsEditting(false);
        setTimeout(() => {
            setSkipTableReset(false);
        }, 300);
    };
    const handleChange = (e) => {
        const newValue = e && e.target ? e.target.value : e;
        setCurrentValue(newValue);
        if (isOnClickEditor)
            saveNewData(newValue);
    };
    const handleBlur = () => {
        if (!isOnClickEditor)
            saveNewData();
    };
    react_1.useEffect(() => {
        if (cell.value !== currentValue) {
            setCurrentValue(cell.value);
        }
    }, [cell.value]);
    return isEditting ? (react_1.default.createElement(cell.column.editor, Object.assign({ value: currentValue, onChange: handleChange, onBlur: handleBlur, autoFocus: true }, cell.column.editorProps))) : (react_1.default.createElement(react_1.default.Fragment, null,
        react_1.default.createElement(Text_1.Text, Object.assign({ variant: "b2", color: "gray.1", onClick: editCell }, (cell.column.editor ? { className: 'canEdit' } : {})), cell.column.formatter
            ? cell.column.formatter(cell.value, cell.row.index)
            : cell.render('Cell')),
        cell.column.editor && (react_1.default.createElement(Icon_1.Icon, { Asset: icons_1.SvgPencilOutline, width: 16, height: 16, color: "gray.3", mt: "-2px", ml: "s", className: "editIcon" })),
        cell.column.footer && react_1.default.createElement("div", null, cell.column.footer(cell.row.index))));
};
//# sourceMappingURL=TableEditableCell.js.map