"use strict";
var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Table = exports.TableBase = void 0;
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = require("react");
const core_1 = require("../core");
const utils_1 = require("../utils");
const context_1 = require("./context");
const tableSortIcon_1 = __importDefault(require("./tableSortIcon"));
const tbody_1 = __importDefault(require("./tbody"));
const td_1 = __importDefault(require("./td"));
const tfoot_1 = __importDefault(require("./tfoot"));
const th_1 = __importDefault(require("./th"));
const thead_1 = __importDefault(require("./thead"));
const tr_1 = __importDefault(require("./tr"));
const useTable_1 = __importDefault(require("./useTable"));
/**
 * Displays a Table with support for columns, rows and sort.
 */
exports.TableBase = core_1.styled.tableBox `
  border-collapse: separate;
  border-spacing: 0px;
  display: table;
  position: relative;
  width: 100%;

  ${p => p.stickyColumn &&
    (0, core_1.css) `
      thead tr:last-of-type th:first-of-type,
      td:first-of-type {
        left: 0;
        position: sticky;
        z-index: 2;

        ::after {
          bottom: -1px;
          box-shadow: inset 10px 0 8px -8px #00000026;
          content: '';
          pointer-events: none;
          position: absolute;
          right: 0;
          top: 0;
          transform: translate(100%);
          transition: box-shadow 0.3s;
          width: 10px;
        }
      }

      thead tr:last-of-type th:first-of-type:not([data-active='true']):not([data-disabled='true']),
      td:first-of-type {
        background-color: white;
      }
    `}
`;
/** Displays a Table with support for columns, rows and sort. */
exports.Table = (0, core_1.vui)((props, ref) => {
    const { children, className, columns, defaultSort, onSortChange, rows, size, sort: sortProp, stickyColumn, stickyHeader, variant } = props, rest = __rest(props, ["children", "className", "columns", "defaultSort", "onSortChange", "rows", "size", "sort", "stickyColumn", "stickyHeader", "variant"]);
    const styles = (0, core_1.useStyleConfig)('Table', props);
    const tableProps = (0, useTable_1.default)({ defaultSort, onSortChange, sort: sortProp });
    const { isControlled, sort } = tableProps;
    // Rows are sorted internally only in uncontrolled mode, that is when 'sort' prop is not provided.
    const sortedRows = (0, react_1.useMemo)(() => (!isControlled ? rows === null || rows === void 0 ? void 0 : rows.slice().sort((0, utils_1.compareBy)(sort.key, sort.order)) : rows), [rows, sort, isControlled]);
    const context = Object.assign(Object.assign({}, tableProps), { columns, rows: sortedRows, size, stickyColumn, stickyHeader, variant });
    return ((0, jsx_runtime_1.jsx)(context_1.TableProvider, { value: context, children: (0, jsx_runtime_1.jsx)(exports.TableBase, Object.assign({ className: (0, utils_1.cs)('vui-table', className), ref: ref, stickyColumn: stickyColumn }, styles.container, rest, { children: children !== null && children !== void 0 ? children : ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)(thead_1.default, {}), (0, jsx_runtime_1.jsx)(tbody_1.default, {})] })) })) }));
});
exports.Table.SortIcon = tableSortIcon_1.default;
exports.Table.Tbody = tbody_1.default;
exports.Table.Td = td_1.default;
exports.Table.Tfoot = tfoot_1.default;
exports.Table.Th = th_1.default;
exports.Table.Thead = thead_1.default;
exports.Table.Tr = tr_1.default;
exports.Table.displayName = 'Table';
exports.default = exports.Table;
//# sourceMappingURL=table.js.map