"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var _chunkOPMC32ZYcjs = require('./chunk-OPMC32ZY.cjs');


var _chunk4BTJNCI2cjs = require('./chunk-4BTJNCI2.cjs');


var _chunkJZ3TGO3Dcjs = require('./chunk-JZ3TGO3D.cjs');



var _chunkMFKERIW4cjs = require('./chunk-MFKERIW4.cjs');


var _chunkOA2TKY4Kcjs = require('./chunk-OA2TKY4K.cjs');


var _chunkWF7Z2VITcjs = require('./chunk-WF7Z2VIT.cjs');


var _chunkRSHN4K6Ncjs = require('./chunk-RSHN4K6N.cjs');




var _chunkGH54P25Bcjs = require('./chunk-GH54P25B.cjs');


var _chunkOBHJBBPCcjs = require('./chunk-OBHJBBPC.cjs');


var _chunkKW3JYLRIcjs = require('./chunk-KW3JYLRI.cjs');



var _chunkZ6EUSUADcjs = require('./chunk-Z6EUSUAD.cjs');

// src/components/Table/Table.tsx
var _react = require('@emotion/react');
var _styled = require('@emotion/styled'); var _styled2 = _interopRequireDefault(_styled);





var _reacttable = require('@tanstack/react-table');
var _lodashes = require('lodash-es');
var _react3 = require('react');
var _jsxruntime = require('@emotion/react/jsx-runtime');
var isObjectCell = (value) => {
  return _lodashes.isObject.call(void 0, value) && "text" in value && typeof value.text === "string";
};
var isLinkCell = (value) => {
  return isObjectCell(value) && ("link" in value || "to" in value);
};
var isClipboardCell = (value) => {
  return isObjectCell(value) && "canCopy" in value;
};
var isLinkAndClipboardCell = (value) => {
  return isObjectCell(value) && ("link" in value || "to" in value) && "canCopy" in value;
};
var isMultilineCell = (value) => {
  return isObjectCell(value) && "multiline" in value;
};
function Table({
  columns,
  data,
  loading,
  onClickRow,
  emptyState,
  clipboardCallbacks,
  customComponents,
  rowHoverEffect = "border",
  minHeight = 300
}) {
  const navigate = _chunkGH54P25Bcjs.useNavigate.call(void 0, );
  const [sorting, setSorting] = _react3.useState.call(void 0, []);
  const { canWriteToClipboard, writeTextToClipboard } = _chunkJZ3TGO3Dcjs.useClipboard.call(void 0, clipboardCallbacks);
  const onClickCopy = _react3.useCallback.call(void 0, 
    (value) => (event) => {
      event.stopPropagation();
      void writeTextToClipboard(value);
    },
    [writeTextToClipboard]
  );
  const mappedColumns = _react3.useMemo.call(void 0, 
    () => columns.map((column) => _chunkZ6EUSUADcjs.__spreadProps.call(void 0, _chunkZ6EUSUADcjs.__spreadValues.call(void 0, {}, column), {
      header: (context) => typeof column.header === "string" ? column.header : /* @__PURE__ */ _jsxruntime.jsxs.call(void 0, "div", { children: [
        column.header.name,
        /* @__PURE__ */ _jsxruntime.jsx.call(void 0, 
          _chunkOPMC32ZYcjs.InfoIconTooltip,
          {
            id: `tooltip-${column.header.name}`,
            content: column.header.tooltip || "",
            verticalAlign: -2
          }
        )
      ] }),
      accessorKey: column.accessorKey.toString(),
      cell: (context) => {
        var _a, _b;
        const value = context.getValue();
        let content = null;
        let icon = null;
        if (isObjectCell(value)) {
          const base64icon = value.base64icon ? /* @__PURE__ */ _jsxruntime.jsx.call(void 0, Base64Icon, { src: value.base64icon, alt: "Icon" }) : null;
          icon = value.icon ? /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _chunkWF7Z2VITcjs.Icon, { name: value.icon, width: 14, mr: 4, color: "c4Neutral" }) : null;
          if (isMultilineCell(value)) {
            content = /* @__PURE__ */ _jsxruntime.jsxs.call(void 0, LineClampSpan, { children: [
              base64icon ? base64icon : icon,
              value.text
            ] });
          } else {
            content = /* @__PURE__ */ _jsxruntime.jsxs.call(void 0, _react3.Fragment, { children: [
              base64icon ? base64icon : icon,
              value.text
            ] });
          }
        } else {
          content = value;
        }
        if (isLinkAndClipboardCell(value)) {
          const copyButton = canWriteToClipboard && /* @__PURE__ */ _jsxruntime.jsx.call(void 0, 
            CopyButton,
            {
              onClick: onClickCopy(value.text),
              role: "button",
              tabIndex: 0,
              children: /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _chunkWF7Z2VITcjs.Icon, { name: "Copy", width: 16, mr: 12 })
            }
          );
          return value.link ? /* @__PURE__ */ _jsxruntime.jsxs.call(void 0, LinkClipboardCell, { children: [
            /* @__PURE__ */ _jsxruntime.jsxs.call(void 0, 
              HoverableLinkCell,
              {
                href: value.link,
                target: "_blank",
                rel: "noreferrer",
                onClick: (e) => {
                  e.stopPropagation();
                },
                children: [
                  /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _chunkWF7Z2VITcjs.Icon, { name: "LinkIcon", width: 12 }),
                  /* @__PURE__ */ _jsxruntime.jsx.call(void 0, LinkCellContent, { children: content })
                ]
              }
            ),
            copyButton
          ] }) : value.to ? /* @__PURE__ */ _jsxruntime.jsxs.call(void 0, HoverableCellWrapper, { children: [
            /* @__PURE__ */ _jsxruntime.jsxs.call(void 0, 
              _chunkGH54P25Bcjs.Link,
              {
                to: value.to,
                params: value.toParams,
                onClick: (e) => {
                  e.stopPropagation();
                },
                children: [
                  icon,
                  /* @__PURE__ */ _jsxruntime.jsx.call(void 0, LinkCellContent, { children: value.text })
                ]
              }
            ),
            copyButton
          ] }) : /* @__PURE__ */ _jsxruntime.jsx.call(void 0, "span", { children: content });
        } else if (isLinkCell(value)) {
          return value.link ? /* @__PURE__ */ _jsxruntime.jsxs.call(void 0, 
            HoverableLinkCell,
            {
              href: value.link,
              target: "_blank",
              rel: "noreferrer",
              onClick: (e) => {
                e.stopPropagation();
              },
              children: [
                /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _chunkWF7Z2VITcjs.Icon, { name: "LinkIcon", width: 12 }),
                /* @__PURE__ */ _jsxruntime.jsx.call(void 0, LinkCellContent, { children: content })
              ]
            }
          ) : value.to ? /* @__PURE__ */ _jsxruntime.jsx.call(void 0, HoverableCellWrapper, { children: /* @__PURE__ */ _jsxruntime.jsxs.call(void 0, 
            _chunkGH54P25Bcjs.Link,
            {
              to: value.to,
              params: value.toParams,
              onClick: (e) => {
                e.stopPropagation();
              },
              children: [
                icon,
                /* @__PURE__ */ _jsxruntime.jsx.call(void 0, LinkCellContent, { children: value.text })
              ]
            }
          ) }) : /* @__PURE__ */ _jsxruntime.jsx.call(void 0, "span", { children: content });
        } else if (isClipboardCell(value)) {
          let maxLines = 1;
          let maxChars = void 0;
          if (isMultilineCell(value)) {
            maxLines = 2;
          } else {
            maxChars = value.maxChars || 16;
          }
          const ellipsisPosition = value.ellipsisPosition || "middle";
          return /* @__PURE__ */ _jsxruntime.jsxs.call(void 0, HoverableCellWrapper, { children: [
            icon,
            /* @__PURE__ */ _jsxruntime.jsx.call(void 0, 
              _chunk4BTJNCI2cjs.ClipboardTextField,
              {
                value: value.text,
                elide: maxChars ? {
                  maxChars,
                  ellipsisPosition
                } : void 0,
                stopPropagation: true,
                maxLines,
                icon: true,
                iconSide: "right",
                clipboardCallbacks,
                typography: ((_a = customComponents == null ? void 0 : customComponents.clipboardTextField) == null ? void 0 : _a.typography) ? customComponents.clipboardTextField.typography : void 0,
                iconName: (_b = customComponents == null ? void 0 : customComponents.clipboardTextField) == null ? void 0 : _b.iconName
              }
            )
          ] });
        }
        return /* @__PURE__ */ _jsxruntime.jsx.call(void 0, "span", { children: content });
      }
    })),
    [
      columns,
      canWriteToClipboard,
      onClickCopy,
      clipboardCallbacks,
      customComponents
    ]
  );
  const tableInstance = _reacttable.useReactTable.call(void 0, {
    columns: mappedColumns,
    data,
    state: {
      sorting
    },
    onSortingChange: setSorting,
    getCoreRowModel: _reacttable.getCoreRowModel.call(void 0, ),
    getSortedRowModel: _reacttable.getSortedRowModel.call(void 0, )
    // debugTable: true
  });
  function onClickDataRow(event, row) {
    const onClickRowResult = onClickRow ? onClickRow(row) : null;
    if (onClickRowResult) {
      const newTabKey = event.metaKey || event.ctrlKey;
      if ((onClickRowResult == null ? void 0 : onClickRowResult.link) || (onClickRowResult == null ? void 0 : onClickRowResult.to) && newTabKey) {
        let link = onClickRowResult == null ? void 0 : onClickRowResult.link;
        if (!link) {
          link = _chunkGH54P25Bcjs.replaceParams.call(void 0, onClickRowResult.to, onClickRowResult.params);
        }
        const target = newTabKey ? "_blank" : void 0;
        window.open(link, target);
      } else if (onClickRowResult == null ? void 0 : onClickRowResult.to) {
        navigate(onClickRowResult.to, onClickRowResult.params);
      }
    }
  }
  const thead = /* @__PURE__ */ _jsxruntime.jsx.call(void 0, "thead", {
    // Loop over the header rows
    children: tableInstance.getHeaderGroups().map((headerGroup) => {
      return /* @__PURE__ */ _jsxruntime.jsx.call(void 0, "tr", { children: headerGroup.headers.map((header) => /* @__PURE__ */ _jsxruntime.jsx.call(void 0, "th", { children: header.isPlaceholder ? null : _reacttable.flexRender.call(void 0, 
        header.column.columnDef.header,
        header.getContext()
      ) }, header.id)) }, headerGroup.id);
    })
  });
  const tbody = /* @__PURE__ */ _jsxruntime.jsx.call(void 0, "tbody", {
    // Loop over the table rows
    children: tableInstance.getRowModel().rows.map((row) => {
      return /* @__PURE__ */ _jsxruntime.jsx.call(void 0, 
        "tr",
        {
          onClick: (event) => onClickDataRow(event, row),
          onKeyDown: (event) => {
            if (event.key === "Enter") {
              onClickDataRow(event, row);
            }
          },
          tabIndex: 0,
          children: row.getVisibleCells().map((cell) => /* @__PURE__ */ _jsxruntime.jsx.call(void 0, "td", { children: _reacttable.flexRender.call(void 0, cell.column.columnDef.cell, cell.getContext()) }, cell.id))
        },
        row.id
      );
    })
  });
  const TableComponent = (customComponents == null ? void 0 : customComponents.table) || StyledTable;
  return /* @__PURE__ */ _jsxruntime.jsxs.call(void 0, TableWrapper, { minHeight, children: [
    /* @__PURE__ */ _jsxruntime.jsxs.call(void 0, 
      TableComponent,
      {
        clickable: Boolean(onClickRow),
        rowHoverEffect,
        children: [
          thead,
          tbody
        ]
      }
    ),
    emptyState,
    loading && /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _chunkOA2TKY4Kcjs.Loading, {})
  ] });
}
var TableWrapper = _styled2.default.div`
  position: relative;
  min-height: ${({ minHeight }) => minHeight}px;
  ${_chunkMFKERIW4cjs.overflowAutoWithoutScrollbars}
`;
var hoverCellStyles = _react.css`
  cursor: pointer;
  transition: opacity 0.25s;

  &:hover {
    opacity: 0.6;
  }
`;
var LinkCellContent = _styled2.default.span`
  vertical-align: middle;
`;
var HoverableCellWrapper = _styled2.default.div`
  ${hoverCellStyles}

  ${_chunkGH54P25Bcjs.Link} {
    text-overflow: ellipsis;
    overflow: hidden;
  }
  display: flex !important;
  gap: 4px;
`;
var LinkClipboardCell = _styled2.default.a`
  ${hoverCellStyles}

  color: ${({ theme }) => theme.hcNeutral};
  display: flex !important;
  gap: 4px;
`;
var HoverableLinkCell = _styled2.default.a`
  ${hoverCellStyles}

  color: ${({ theme }) => theme.hcNeutral};
  display: flex !important;
  gap: 4px;
`;
var CopyButton = _styled2.default.span`
  overflow: hidden;
  align-items: center;
  cursor: pointer;
  display: inline-table;
`;
var LineClampSpan = _styled2.default.span`
  ${_chunkMFKERIW4cjs.lineClamp.call(void 0, 2)}
`;
var Base64Icon = _styled2.default.img`
  width: 32px;
  height: 32px;
  object-fit: contain;
  border-radius: 4px;
  margin-right: 12px;
`;
var cellPaddingPx = 15;
var StyledTable = _styled2.default.table`
  position: relative;
  border-spacing: 0;
  width: 100%;

  /* Important to use padding here instead of width so that table container overflow functions properly: */
  ${_chunkKW3JYLRIcjs.bp.lg(`
    padding-left: ${_chunkRSHN4K6Ncjs.standardContentInset.lgPx}px;
    padding-right: ${_chunkRSHN4K6Ncjs.standardContentInset.lgPx}px;
  `)}
  ${_chunkKW3JYLRIcjs.bp.sm(`
    padding-left: ${_chunkRSHN4K6Ncjs.standardContentInset.smPx}px;
    padding-right: ${_chunkRSHN4K6Ncjs.standardContentInset.smPx}px;
  `)}
  ${_chunkKW3JYLRIcjs.bp.minSmMaxLg(`
    padding-left: ${_chunkRSHN4K6Ncjs.standardContentInset.minSmMaxLgPx}px;
    padding-right: ${_chunkRSHN4K6Ncjs.standardContentInset.minSmMaxLgPx}px;
  `)}

  th {
    max-width: 200px;
    overflow: hidden;
    text-overflow: ellipsis;
    text-align: left;
    padding: ${cellPaddingPx}px 0px;
    border-bottom: 1px solid
      ${({ theme }) => _chunkOBHJBBPCcjs.themeOrWithKey.call(void 0, "c1Neutral", "c2Neutral")({ theme })};
    padding: ${cellPaddingPx}px;
    &:first-of-type {
      padding-left: 0px;
    }
    &:last-of-type {
      padding-right: 0px;
    }
    &:last-of-type {
      text-align: right;
    }
  }

  td {
    max-width: 200px;
    text-overflow: ellipsis;
    overflow: hidden;
    padding: 0 ${cellPaddingPx}px;
    white-space: nowrap;
    & > span {
      overflow: hidden;
      text-overflow: ellipsis;
      display: block;
      padding: ${cellPaddingPx}px 0px;
      & > * {
        overflow: hidden;
        text-overflow: ellipsis;
        vertical-align: middle;
      }
    }
    &:first-of-type {
      padding-left: 0px;
    }
    &:last-of-type {
      text-align: right;
      padding-right: 0px;
    }
    & span:has(> ${LineClampSpan}),
    & ${LineClampSpan} {
      white-space: normal;
    }
  }

  tr {
    cursor: ${({ clickable }) => clickable ? "pointer" : "default"};
    position: relative;

    ${({ rowHoverEffect, theme }) => `
        &:hover {
          td {
            position: relative;
            &:before {
              content: "";
              display: block;
              position: absolute;
              pointer-events: none;
              height: 80%;
              top: 10%;
              bottom: 10%;
              ${rowHoverEffect === "background" ? `background: ${theme.c1Neutral};` : `
                    border: 1px solid ${_chunkOBHJBBPCcjs.themeOrWithKey.call(void 0, 
  "c1Neutral",
  "c2Neutral"
)({ theme })};
                    border-left-width: 0;
                    border-right-width: 0;
                  `}
              left: 0;
              right: 0;
              width: 100%;
            }
            &:first-of-type {
              overflow: visible;
              &:before {
                border-top-left-radius: 32px;
                border-bottom-left-radius: 32px;
                transform: translateX(-12px);
                width: calc(100% + 12px);
                ${rowHoverEffect === "border" ? "border-left-width: 1px;" : ""}
              }
            }
            &:last-of-type {
              overflow: visible;
              &:before {
                border-top-right-radius: 32px;
                border-bottom-right-radius: 32px;
                transform: translateX(12px);
                width: calc(100% + 12px);
                left: auto;
                ${rowHoverEffect === "border" ? "border-right-width: 1px;" : ""}
              }
            }
          }
        }
        `}
  }
`;



exports.Table = Table;
