'use strict';
'use client';
import { c } from 'react/compiler-runtime';
import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
import { useState, useEffect, useContext, createElement } from 'react';
import { tableSortDirections } from '../../../../fd-toolbox/enums/enums.js';
import { updateTableHeaderCellHoverState, handleInit, getHeaderClasses, checkIfAllSelected, shouldShowActionSection, isColumnCheckbox, runSelectAllCheckboxes, onCustomAction, shouldShowHeaderAction, shouldShowHeaderText, onSortDirectionChange, getSortIcon } from './TableHeaderCell.funcs.js';
import { tableCustomActionTypes } from '../table-models.js';
import { cn } from '../../../../fd-toolbox/lib/utils.js';
import { isRightAlignColumn } from '../services/table-column-alignment.js';
import { Icon, iconSizes, iconNames } from '../../../fd-component-toolbox/icon/Icon.js';
import { FadeOutText } from '../../../fd-component-toolbox/fade-out-text/FadeOutText.js';
import { TableHeaderActionCell } from './TableHeaderActionCell.js';
import { Checkbox } from '../../../fd-component-toolbox/checkbox/Checkbox.js';
import { TableReactContext } from '../table-context.js';

var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", {
  value,
  configurable: true
});
function TableHeaderCell(t0) {
  const $ = c(11);
  const {
    cellContext,
    tableSorted,
    onRefreshItems,
    onActiveHandlerIndexChange,
    display,
    tableHovered: tableHoveredProp,
    hidden,
    columnSortable
  } = t0;
  const [headerText, setHeaderText] = useState("");
  const [headerId, setHeaderId] = useState("");
  const [fullHeaderText, setFullHeaderText] = useState("");
  const [tableHovered, setTableHovered] = useState(false);
  const [isEditSheetOpen, setIsEditSheetOpen] = useState(false);
  let t1;
  let t2;
  if ($[0] !== isEditSheetOpen || $[1] !== tableHoveredProp) {
    t1 = () => {
      updateTableHeaderCellHoverState(isEditSheetOpen, tableHoveredProp, setTableHovered);
    };
    t2 = [isEditSheetOpen, tableHoveredProp];
    $[0] = isEditSheetOpen;
    $[1] = tableHoveredProp;
    $[2] = t1;
    $[3] = t2;
  } else {
    t1 = $[2];
    t2 = $[3];
  }
  useEffect(t1, t2);
  let t3;
  let t4;
  if ($[4] !== cellContext) {
    t3 = () => {
      handleInit(cellContext, setHeaderText, setFullHeaderText, setHeaderId);
    };
    t4 = [cellContext];
    $[4] = cellContext;
    $[5] = t3;
    $[6] = t4;
  } else {
    t3 = $[5];
    t4 = $[6];
  }
  useEffect(t3, t4);
  const {
    alignmentClass,
    activeSortPaddingClass,
    alignmentArrowClass
  } = getHeaderClasses(cellContext?.internalTableColumn);
  const {
    tableRows: t5,
    setTableRows
  } = useContext(TableReactContext);
  let t6;
  if ($[7] !== t5) {
    t6 = t5 === undefined ? [] : t5;
    $[7] = t5;
    $[8] = t6;
  } else {
    t6 = $[8];
  }
  const tableRows = t6;
  let t7;
  if ($[9] !== tableRows) {
    t7 = checkIfAllSelected(tableRows);
    $[9] = tableRows;
    $[10] = t7;
  } else {
    t7 = $[10];
  }
  const isAllSelected = t7;
  const customAction = cellContext.tableInstance?.customActions?.find(_temp);
  const showActionSection = shouldShowActionSection(cellContext.internalTableColumn);
  return jsx("div", {
    onMouseMove: onActiveHandlerIndexChange,
    className: "group/header-cell size-full",
    children: isColumnCheckbox(cellContext) ? jsx("div", {
      className: cn("relative flex size-full cursor-pointer items-center justify-center p-0 text-xs lg:text-sm"),
      children: jsx(Checkbox, {
        checked: Boolean(isAllSelected),
        onCheckedChange: () => runSelectAllCheckboxes(setTableRows)
      })
    }) : jsxs("div", {
      className: cn("relative flex size-full cursor-pointer items-center justify-center p-2 text-xs lg:text-sm"),
      children: [showActionSection && jsx("div", {
        className: cn("flex size-4 items-center justify-center", shouldShowHeaderAction(cellContext, tableHovered, display) ? "animate-fade-in" : "animate-fade-out"),
        children: cellContext.tableInstance.uiResourceMeta && customAction?.getTemplate ? jsx(Fragment, {
          children: createElement(customAction.getTemplate, {
            onCustomAction: __name(() => onCustomAction({
              shouldTableRefresh: customAction.shouldTableRefreshAfter
            }, onRefreshItems), "onCustomAction"),
            propertyName: cellContext.internalTableColumn.name
          })
        }) : jsx(TableHeaderActionCell, {
          cellContext,
          onRefreshItems,
          setIsEditSheetOpen
        })
      }), shouldShowHeaderText(cellContext) && jsxs("div", {
        className: cn("text-subtle-500 flex w-full flex-row items-center justify-between gap-1 text-xs lg:text-sm", alignmentClass, columnSortable ? "cursor-pointer" : "cursor-default"),
        children: [jsx(FadeOutText, {
          className: activeSortPaddingClass,
          rightAlign: isRightAlignColumn(cellContext.internalTableColumn),
          id: headerId,
          onTooltipClick: () => onSortDirectionChange(tableSorted, cellContext, columnSortable),
          tooltipContent: fullHeaderText,
          children: headerText
        }), jsx("div", {
          className: cn("bg-background flex size-4 min-w-4 items-center justify-center rounded-full", alignmentArrowClass, (hidden ?? !columnSortable) && "animate-fade-out", cellContext.internalTableColumn.isActiveSort && "animate-fade-in", !cellContext.internalTableColumn.isActiveSort && "animate-fade-out group-hover/header-cell:animate-fade-in"),
          onClick: () => onSortDirectionChange(tableSorted, cellContext, columnSortable),
          children: columnSortable && jsx(Icon, {
            name: getSortIcon(cellContext.internalTableColumn.sortDirection ?? tableSortDirections.desc) === "chevron-up" ? iconNames.chevronUp : iconNames.chevronDown,
            size: iconSizes.extraSmall,
            className: "size-4.5"
          })
        })]
      })]
    })
  });
}
function _temp(action) {
  return action.actionType === tableCustomActionTypes.template;
}
__name(TableHeaderCell, "TableHeaderCell");

export { TableHeaderCell };
