'use strict';
import { jsxs, jsx } from 'react/jsx-runtime';
import React, { useState, useContext, useEffect } from 'react';
import { cn } from '../../../../fd-toolbox/lib/utils.js';
import { onInitTableBody, applyResizableTableBodyLayout, handleRowClick, getRowId } from './TableBodyRow.funcs.js';
import { BodyCell } from '../body-cell/BodyCell.js';
import { ResizablePanelGroup, ResizablePanel, ResizableHandle } from '../header-cell/Resizable.js';
import { internalTableColumnTypes } from '../table-models.js';
import { TableReactContext } from '../table-context.js';
import { tableElementSuffixes } from '../../../fd-component-toolbox/constants/table-element-suffixes.js';
import { calculateDefaultSize } from '../services/fd-table-private-column-calculation.js';
import { useEmptyRef } from '../../../../fd-toolbox/hooks/use-empty-ref.js';
import { getColumnSize } from '../services/table-column-size.js';

var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", {
  value,
  configurable: true
});
function TableBodyRow({
  rowContext,
  displayMode,
  onRefreshItems,
  itemDeleted,
  cellClick,
  className,
  tableActionType
}) {
  const [bodyCellClasses, setBodyCellClasses] = useState([]);
  const [bodyCellTestGroup, setBodyCellTestGroup] = useState("");
  const [cellContexts, setCellContexts] = useState([]);
  const [isHovered, setIsHovered] = useState(false);
  const {
    columnWidths
  } = useContext(TableReactContext);
  const resizableRef = useEmptyRef();
  useEffect(() => {
    onInitTableBody(bodyCellTestGroup, setBodyCellTestGroup, setCellContexts, setBodyCellClasses, rowContext, displayMode);
  }, [bodyCellTestGroup, displayMode, rowContext]);
  useEffect(() => {
    if (resizableRef.current) {
      applyResizableTableBodyLayout(columnWidths, resizableRef.current.setLayout);
    }
  }, [columnWidths]);
  return rowContext ? /* @__PURE__ */jsxs("div", {
    role: "row",
    id: getRowId(rowContext.row.index, rowContext, tableElementSuffixes.row),
    onClick: () => handleRowClick(rowContext, cellClick),
    className: cn("border-input hover:bg-muted/50 relative w-full overflow-visible py-1.5 transition duration-200 ease-linear", className),
    onMouseEnter: () => setIsHovered(true),
    onMouseLeave: () => setIsHovered(false),
    onBlur: () => setIsHovered(false),
    children: [/* @__PURE__ */jsx("span", {
      hidden: true,
      "data-id": rowContext.row.item?.id,
      "data-row-index": rowContext.row.index
    }), /* @__PURE__ */jsxs(ResizablePanelGroup, {
      autoSaveId: "persistence",
      direction: "horizontal",
      className: "flex h-full min-h-5 flex-row items-center overflow-visible",
      ref: resizableRef,
      children: [rowContext.tableInstance.internalTableColumns.map((column, i) => /* @__PURE__ */jsxs(React.Fragment, {
        children: [/* @__PURE__ */jsx(ResizablePanel, {
          style: {
            overflow: "visible"
          },
          defaultSize: calculateDefaultSize(columnWidths[i]) || void 0,
          className: cn("flex min-w-0 justify-center", rowContext.tableInstance.internalTableColumns[i].type === internalTableColumnTypes.checkBox && "max-w-8", rowContext.tableInstance.internalTableColumns[i].type === internalTableColumnTypes.action && i < 2 && "max-w-12 min-w-12", rowContext.tableInstance.internalTableColumns[i].type === internalTableColumnTypes.customHeaderAction && "max-w-16 min-w-5"),
          minSize: getColumnSize(rowContext.tableInstance.internalTableColumns[i], rowContext.tableInstance, displayMode, rowContext.tableInstance.internalTableColumns),
          children: /* @__PURE__ */jsx(BodyCell, {
            cellContext: cellContexts[i],
            "data-test-group": bodyCellTestGroup,
            className: bodyCellClasses[i],
            onRefreshItems,
            itemDeleted,
            actionType: tableActionType,
            hovered: isHovered
          })
        }), i === rowContext.tableInstance.internalTableColumns.length - 1 ? void 0 : /* @__PURE__ */jsx(ResizableHandle, {
          disabled: true,
          className: "w-0 border-0"
        })]
      }, column.index)), /* @__PURE__ */jsx(ResizablePanel, {
        order: rowContext.tableInstance.internalTableColumns.length
      })]
    })]
  }) : /* @__PURE__ */jsx(BodyCell, {
    className: bodyCellClasses.join(" ")
  });
}
__name(TableBodyRow, "TableBodyRow");

export { TableBodyRow };
