function _array_like_to_array(arr, len) {
    if (len == null || len > arr.length) len = arr.length;
    for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
    return arr2;
}
function _array_with_holes(arr) {
    if (Array.isArray(arr)) return arr;
}
function _define_property(obj, key, value) {
    if (key in obj) {
        Object.defineProperty(obj, key, {
            value: value,
            enumerable: true,
            configurable: true,
            writable: true
        });
    } else {
        obj[key] = value;
    }
    return obj;
}
function _iterable_to_array_limit(arr, i) {
    var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"];
    if (_i == null) return;
    var _arr = [];
    var _n = true;
    var _d = false;
    var _s, _e;
    try {
        for(_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true){
            _arr.push(_s.value);
            if (i && _arr.length === i) break;
        }
    } catch (err) {
        _d = true;
        _e = err;
    } finally{
        try {
            if (!_n && _i["return"] != null) _i["return"]();
        } finally{
            if (_d) throw _e;
        }
    }
    return _arr;
}
function _non_iterable_rest() {
    throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}
function _object_spread(target) {
    for(var i = 1; i < arguments.length; i++){
        var source = arguments[i] != null ? arguments[i] : {};
        var ownKeys = Object.keys(source);
        if (typeof Object.getOwnPropertySymbols === "function") {
            ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
                return Object.getOwnPropertyDescriptor(source, sym).enumerable;
            }));
        }
        ownKeys.forEach(function(key) {
            _define_property(target, key, source[key]);
        });
    }
    return target;
}
function _sliced_to_array(arr, i) {
    return _array_with_holes(arr) || _iterable_to_array_limit(arr, i) || _unsupported_iterable_to_array(arr, i) || _non_iterable_rest();
}
function _unsupported_iterable_to_array(o, minLen) {
    if (!o) return;
    if (typeof o === "string") return _array_like_to_array(o, minLen);
    var n = Object.prototype.toString.call(o).slice(8, -1);
    if (n === "Object" && o.constructor) n = o.constructor.name;
    if (n === "Map" || n === "Set") return Array.from(n);
    if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
}
import React, { useState, useRef } from "react";
import { flexRender } from "@tanstack/react-table";
import { useOutsideClick } from "../../../../hooks";
import { Resizer } from "../../Table.styles";
import { SELECT_COLUMN_ID } from "../../utils";
import { IconEditOutline, IconResetOutline, IconDoneCircleOutline } from "../../../_Icon";
import { getIconSize } from "../HeadCell/HeadCell";
import { classes } from "../../Table.tokens";
import { ContentWrapper, Td, IconDoneButton, Input, InnerWrapper, InputWrapper, EditModeWrapper, IconResetButton } from "./EditableCell.styles";
var keys = {
    Enter: 'Enter',
    Tab: 'Tab',
    Escape: 'Escape'
};
export var EditableCell = function EditableCell(param) {
    var size = param.size, view = param.view, cell = param.cell, borderVariant = param.borderVariant, table = param.table, selected = param.selected, additionalProps = param.additionalProps, className = param.className;
    var _table_options_meta;
    var _useState = _sliced_to_array(useState(cell.getValue()), 2), value = _useState[0], setValue = _useState[1];
    var _useState1 = _sliced_to_array(useState(false), 2), editingMode = _useState1[0], setEditingMode = _useState1[1];
    var updateData = (_table_options_meta = table.options.meta) === null || _table_options_meta === void 0 ? void 0 : _table_options_meta.updateData;
    var ref = useRef(null);
    useOutsideClick(function() {
        setEditingMode(false);
    }, [
        ref
    ]);
    var handleEditClick = function handleEditClick(e) {
        e.stopPropagation();
        setEditingMode(true);
        setValue(cell.getValue());
    };
    var handleChange = function handleChange(e) {
        return setValue(e.target.value);
    };
    var handleReset = function handleReset() {
        setValue(cell.getValue());
    };
    var handleSubmit = function handleSubmit(e) {
        e.stopPropagation();
        if (updateData) {
            updateData(cell.row.id, cell.column.id, value);
        }
        setEditingMode(false);
    };
    var handleKeyDown = function handleKeyDown(event) {
        switch(event.code){
            case keys.Enter:
                {
                    if (updateData) {
                        updateData(cell.row.id, cell.column.id, value);
                    }
                    setEditingMode(false);
                    break;
                }
            case keys.Escape:
            case keys.Tab:
                {
                    setEditingMode(false);
                    break;
                }
            default:
                {
                    break;
                }
        }
    };
    return /*#__PURE__*/ React.createElement(Td, _object_spread({
        key: cell.id,
        borderVariant: borderVariant,
        selectionCell: cell.column.id === SELECT_COLUMN_ID,
        onClick: handleEditClick,
        editingMode: editingMode,
        selected: selected,
        view: view,
        ref: ref,
        className: className
    }, additionalProps), /*#__PURE__*/ React.createElement(InnerWrapper, null, editingMode ? /*#__PURE__*/ React.createElement(EditModeWrapper, null, /*#__PURE__*/ React.createElement(InputWrapper, null, /*#__PURE__*/ React.createElement(Input, {
        value: value,
        onChange: handleChange,
        autoFocus: true,
        onKeyDown: handleKeyDown
    }), /*#__PURE__*/ React.createElement(IconResetButton, {
        onClick: handleReset,
        tabIndex: -1
    }, /*#__PURE__*/ React.createElement(IconResetOutline, {
        size: getIconSize(size)
    }))), /*#__PURE__*/ React.createElement(IconDoneButton, {
        onClick: handleSubmit,
        tabIndex: -1
    }, /*#__PURE__*/ React.createElement(IconDoneCircleOutline, {
        size: getIconSize(size)
    }))) : /*#__PURE__*/ React.createElement(ContentWrapper, null, flexRender(cell.column.columnDef.cell, cell.getContext()), /*#__PURE__*/ React.createElement(IconEditOutline, {
        size: "xs",
        className: classes.editIcon,
        color: "var(--text-secondary)"
    }))), cell.column.getIsResizing() && /*#__PURE__*/ React.createElement(Resizer, {
        isResizing: true
    }));
};
