'use strict';
import { c } from 'react/compiler-runtime';
import { jsx, jsxs } from 'react/jsx-runtime';
import { useState, useContext, useEffect, createElement } from 'react';
import { cn } from '../../../../fd-toolbox/lib/utils.js';
import { Checkbox } from '../../../fd-component-toolbox/checkbox/Checkbox.js';
import { internalTableColumnTypes } from '../table-models.js';
import { getBodyImageCell, initBodyCell, onViewClick, updateSelectedRow, handleDialogResponse } from './BodyCell.funcs.js';
import { isRightAlignColumn } from '../services/table-column-alignment.js';
import { Button } from '../../../fd-component-toolbox/button/Button.js';
import { Icon, iconNames } from '../../../fd-component-toolbox/icon/Icon.js';
import { useAsyncEffect } from '../../../../fd-toolbox/hooks/use-async-effect.js';
import { ActionBodyCell } from './ActionBodyCell.js';
import { Avatar, AvatarImage, AvatarFallback } from '../../../fd-component-toolbox/avatar/Avatar.js';
import { isClickableCell } from '../services/table-private-cell.js';
import { FadeOutText } from '../../../fd-component-toolbox/fade-out-text/FadeOutText.js';
import { imageNotFoundUrl } from '../../../../fd-toolbox/constants/public-files.js';
import { isMarkdown, isRichText } from '../../core/property-types.js';
import { markdownToHtml, parseText } from '../../../fd-component-toolbox/markdown/markdown-parser.js';
import { TableReactContext } from '../table-context.js';
import { BodyCellValue } from './BodyCellValue.js';
import { isString } from '../../../../fd-toolbox/types/ensure-type.js';
import { BodyCellArrayTooltip } from './BodyCellArrayTooltip.js';

var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", {
  value,
  configurable: true
});
function BodyCell(t0) {
  const $ = c(39);
  const {
    className,
    width,
    cellContext,
    onRefreshItems,
    itemDeleted,
    actionType,
    hovered
  } = t0;
  const [cellId, setCellId] = useState("");
  const [checkboxId, setCheckboxId] = useState("");
  const [iconId, setIconId] = useState("");
  const [viewCellId, setViewCellId] = useState("");
  const [cellValue, setCellValue] = useState("");
  const [parsedMarkdown, setParsedMarkdown] = useState("");
  let t1;
  if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
    t1 = [];
    $[0] = t1;
  } else {
    t1 = $[0];
  }
  const [parsedRichText, setParsedRichText] = useState(t1);
  const [cellImage, setCellImage] = useState(void 0);
  const [imageSrc, setImageSrc] = useState(imageNotFoundUrl);
  const [cellWidth, setCellWidth] = useState(cellContext?.internalTableColumn.width ?? 0);
  const [isDefaultOrExpandAdvisableCell, setIsDefaultOrExpandAdvisableCell] = useState(false);
  const [isCheckmarkCell, setIsCheckmarkCell] = useState(false);
  const [shouldShowEditButton, setShouldShowEditButton] = useState(false);
  const [shouldShowViewButton, setShouldShowViewButton] = useState(false);
  const [shouldShowDeleteButton, setShouldShowDeleteButton] = useState(false);
  const {
    loading,
    setTableRows
  } = useContext(TableReactContext);
  let t2;
  let t3;
  if ($[1] !== cellContext) {
    t2 = () => {
      if (cellContext) {
        const loadImage = __name(async () => {
          const image = getBodyImageCell(cellContext);
          if (image) {
            const src = await image;
            setImageSrc(src ?? imageNotFoundUrl);
          }
        }, "loadImage");
        loadImage();
      }
    };
    t3 = [cellContext];
    $[1] = cellContext;
    $[2] = t2;
    $[3] = t3;
  } else {
    t2 = $[2];
    t3 = $[3];
  }
  useEffect(t2, t3);
  let t4;
  let t5;
  if ($[4] !== cellContext) {
    t4 = async () => {
      await initBodyCell(setCellValue, setCellImage, setCheckboxId, setViewCellId, setCellId, setIconId, setIsDefaultOrExpandAdvisableCell, setIsCheckmarkCell, setCellWidth, setShouldShowEditButton, setShouldShowViewButton, setShouldShowDeleteButton, cellContext);
    };
    t5 = [cellContext];
    $[4] = cellContext;
    $[5] = t4;
    $[6] = t5;
  } else {
    t4 = $[5];
    t5 = $[6];
  }
  useAsyncEffect(t4, t5);
  let t6;
  if ($[7] !== cellContext || $[8] !== cellValue) {
    t6 = async () => {
      if (cellContext && isString(cellValue)) {
        if (isMarkdown(cellContext.internalTableColumn.propertyType, cellContext.internalTableColumn.propertyTypeFormat)) {
          const html = await markdownToHtml(cellValue);
          setParsedMarkdown(html ?? "");
        }
        if (isRichText(cellContext.internalTableColumn.propertyType, cellContext.internalTableColumn.propertyTypeFormat)) {
          setParsedRichText(parseText(cellValue));
        }
      }
    };
    $[7] = cellContext;
    $[8] = cellValue;
    $[9] = t6;
  } else {
    t6 = $[9];
  }
  let t7;
  if ($[10] !== cellValue) {
    t7 = [cellValue];
    $[10] = cellValue;
    $[11] = t7;
  } else {
    t7 = $[11];
  }
  useAsyncEffect(t6, t7);
  const alignmentClass = isRightAlignColumn(cellContext?.internalTableColumn) ? "justify-end" : "";
  if (!cellContext) {
    const t8 = loading ? "Loading..." : "No data";
    let t9;
    if ($[12] !== t8) {
      t9 = jsx("div", {
        className: "text-muted-foreground p-4 text-xs lg:text-sm",
        children: t8
      });
      $[12] = t8;
      $[13] = t9;
    } else {
      t9 = $[13];
    }
    return t9;
  }
  let t8;
  if ($[14] !== actionType || $[15] !== alignmentClass || $[16] !== cellContext || $[17] !== cellId || $[18] !== cellImage || $[19] !== cellValue || $[20] !== cellWidth || $[21] !== checkboxId || $[22] !== className || $[23] !== hovered || $[24] !== iconId || $[25] !== imageSrc || $[26] !== isCheckmarkCell || $[27] !== isDefaultOrExpandAdvisableCell || $[28] !== itemDeleted || $[29] !== onRefreshItems || $[30] !== parsedMarkdown || $[31] !== parsedRichText || $[32] !== setTableRows || $[33] !== shouldShowDeleteButton || $[34] !== shouldShowEditButton || $[35] !== shouldShowViewButton || $[36] !== viewCellId || $[37] !== width) {
    t8 = jsxs("div", {
      style: {
        minWidth: cellWidth,
        width
      },
      className: cn("flex flex-col justify-around px-1 text-xs lg:text-sm", className),
      children: [isDefaultOrExpandAdvisableCell && jsx("div", {
        className: "text-foreground relative w-full pr-0.5 pl-1.5",
        children: !cellContext.internalTableColumn.cellTemplate ? jsx("div", {
          className: cn("hover:overflow-visible hover:whitespace-nowrap", isClickableCell(cellContext) && "text-fd-accent"),
          children: jsx("div", {
            className: "transition-colors duration-200 ease-linear",
            children: jsxs(FadeOutText, {
              tooltipContent: jsx(BodyCellArrayTooltip, {
                cellContext
              }),
              rightAlign: isRightAlignColumn(cellContext?.internalTableColumn),
              children: [jsx("div", {
                className: cn("flex items-center", alignmentClass),
                children: cellImage && jsxs(Avatar, {
                  className: "mr-2 size-5",
                  children: [jsx(AvatarImage, {
                    src: cellImage,
                    alt: "Cell image"
                  }), jsx(AvatarFallback, {})]
                })
              }), jsx("div", {
                id: cellId,
                className: "flex",
                children: jsx(BodyCellValue, {
                  cellContext,
                  onRefreshItems,
                  cellValue: String(cellValue),
                  imageSrc,
                  onViewClick: () => onViewClick(cellContext),
                  parsedMarkdown,
                  parsedRichText
                })
              })]
            })
          })
        }) : jsx("div", {
          className: "hover:overflow-visible hover:whitespace-nowrap",
          children: jsx("div", {
            className: "transition-colors duration-200 ease-linear",
            children: jsx("div", {
              id: cellId,
              className: "truncate transition-colors duration-200 ease-linear",
              children: createElement(cellContext.internalTableColumn.cellTemplate, {
                resource: cellContext.row.item,
                propertyName: cellContext.internalTableColumn.name
              })
            })
          })
        })
      }), jsxs("div", {
        className: "flex w-full flex-row items-center justify-center gap-2",
        onClick: _temp,
        children: [cellContext.internalTableColumn.type === internalTableColumnTypes.checkBox && cellContext.tableInstance.multiSelect && jsx("div", {
          children: jsx(Checkbox, {
            id: checkboxId,
            onCheckedChange: isChecked => updateSelectedRow(isChecked, cellContext, setTableRows),
            checked: cellContext.row.selected
          })
        }), (cellContext.internalTableColumn.type === internalTableColumnTypes.action || cellContext.internalTableColumn.type === internalTableColumnTypes.customHeaderAction) && jsx(ActionBodyCell, {
          cellContext,
          actionType,
          shouldShowEditButton,
          shouldShowViewButton,
          shouldShowDeleteButton,
          viewCellId,
          hovered: hovered ?? false,
          onRefreshItems,
          onViewClick: () => onViewClick(cellContext),
          onDialogResponse: isYes => handleDialogResponse(isYes, itemDeleted, cellContext)
        })]
      }), isCheckmarkCell && jsxs("div", {
        id: cellId,
        className: "flex w-full justify-end",
        children: [!!cellValue && jsx(Button, {
          id: iconId,
          className: "size-5",
          size: "icon",
          variant: "icon",
          children: jsx(Icon, {
            name: iconNames.check
          })
        }), "\xA0"]
      })]
    });
    $[14] = actionType;
    $[15] = alignmentClass;
    $[16] = cellContext;
    $[17] = cellId;
    $[18] = cellImage;
    $[19] = cellValue;
    $[20] = cellWidth;
    $[21] = checkboxId;
    $[22] = className;
    $[23] = hovered;
    $[24] = iconId;
    $[25] = imageSrc;
    $[26] = isCheckmarkCell;
    $[27] = isDefaultOrExpandAdvisableCell;
    $[28] = itemDeleted;
    $[29] = onRefreshItems;
    $[30] = parsedMarkdown;
    $[31] = parsedRichText;
    $[32] = setTableRows;
    $[33] = shouldShowDeleteButton;
    $[34] = shouldShowEditButton;
    $[35] = shouldShowViewButton;
    $[36] = viewCellId;
    $[37] = width;
    $[38] = t8;
  } else {
    t8 = $[38];
  }
  return t8;
}
function _temp(event) {
  return event.stopPropagation();
}
__name(BodyCell, "BodyCell");

export { BodyCell };
