"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (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.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TableCellWithDnd = void 0;
var react_1 = __importStar(require("react"));
var classnames_1 = __importDefault(require("classnames"));
var types_1 = require("../../types");
var helpers_1 = require("../../helpers");
var styles_1 = require("./styles");
var core_1 = require("@dnd-kit/core");
var ColumnDragIndicator_1 = require("./ColumnDragIndicator");
var TableCellWithDnd = function (_a) {
    var _b;
    var columnValue = _a.columnValue, rowData = _a.rowData, index = _a.index, shouldRenderDragIndicator = _a.shouldRenderDragIndicator, setHoveredColumn = _a.setHoveredColumn, resetHoveredColumn = _a.resetHoveredColumn, rowIsDragging = _a.rowIsDragging;
    var styles = (0, styles_1.useStyles)();
    var _c = (0, react_1.useState)(false), enableDropForColumn = _c[0], setEnableDropForColumn = _c[1];
    var _d = (0, react_1.useState)(false), isColumnDragging = _d[0], setIsColumnDragging = _d[1];
    (0, core_1.useDndMonitor)({
        onDragStart: function (event) {
            if (event.active.data.current.type === types_1.DndItemTypes.Column) {
                setEnableDropForColumn(true);
                setIsColumnDragging(event.active.id === columnValue.id);
            }
        },
        onDragEnd: function () {
            setEnableDropForColumn(false);
            setIsColumnDragging(false);
        }
    });
    var setDroppableNodeRef = (0, core_1.useDroppable)({
        id: columnValue.id
    }).setNodeRef;
    var columnRef = (0, react_1.useRef)(null);
    var setColumnRef = (0, react_1.useCallback)(function (elem) {
        setDroppableNodeRef(elem);
        columnRef.current = elem;
    }, [setDroppableNodeRef]);
    var getColumnWidth = function () { var _a; return (_a = columnRef === null || columnRef === void 0 ? void 0 : columnRef.current) === null || _a === void 0 ? void 0 : _a.offsetWidth; };
    var CellRenderer = rowData.cellRenderer || helpers_1.DefaultCellRenderer;
    var onMouseOver = setHoveredColumn ? function () { return setHoveredColumn(columnValue); } : undefined;
    var draggingColumnStyle = (0, react_1.useMemo)(function () { return ({ opacity: isColumnDragging ? 0 : 1 }); }, [isColumnDragging]);
    return (react_1.default.createElement("td", { style: draggingColumnStyle, ref: enableDropForColumn ? setColumnRef : undefined, className: styles.tableData, onMouseOver: onMouseOver, onMouseLeave: resetHoveredColumn },
        react_1.default.createElement("div", { className: (0, classnames_1.default)((_b = {}, _b[styles.hidden] = rowIsDragging, _b)) },
            shouldRenderDragIndicator && (react_1.default.createElement(ColumnDragIndicator_1.ColumnDragIndicator, { columnValue: columnValue, rowData: rowData, index: index, getColumnWidth: getColumnWidth })),
            react_1.default.createElement(CellRenderer, { value: columnValue[rowData.id] }))));
};
exports.TableCellWithDnd = TableCellWithDnd;
