'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var core = require('@linaria/core');
var common = require('./common.js');
var TableWidget = require('./TableWidget.js');
var antd = require('antd');
var cs = require('classnames');
var React = require('react');
var TableSkeleton = require('./TableSkeleton.js');

function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }

var cs__default = /*#__PURE__*/_interopDefault(cs);
var React__default = /*#__PURE__*/_interopDefault(React);

var __defProp = Object.defineProperty;
var __defProps = Object.defineProperties;
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, {
  enumerable: true,
  configurable: true,
  writable: true,
  value
}) : obj[key] = value;
var __spreadValues = (a, b) => {
  for (var prop in b || (b = {})) if (__hasOwnProp.call(b, prop)) __defNormalProp(a, prop, b[prop]);
  if (__getOwnPropSymbols) for (var prop of __getOwnPropSymbols(b)) {
    if (__propIsEnum.call(b, prop)) __defNormalProp(a, prop, b[prop]);
  }
  return a;
};
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
const TableContainerStyle = "E_tc97u5y";
const emptyTableStyle = "E_efzf37v";
const tableStyleCover = "E_t1fisho6";
const Table = props => {
  const {
    loading = false,
    error,
    dataSource: _dataSource,
    columns,
    onSorterChange,
    onRowClick,
    rowClassName,
    scroll,
    bordered,
    components,
    rowSelection,
    empty,
    tableLayout = "fixed",
    // FIXME Should be removed at 0.30.x
    initLoading = false,
    rowKey,
    wrapper,
    pagination,
    onRow,
    skeletonProps,
    expandable
  } = props;
  const orderRef = React.useRef(null);
  const hasScrollBard = common.useTableBodyHasScrollBar(wrapper, _dataSource);
  const _columns = React.useMemo(() => columns.map(column => {
    const _column = __spreadValues({}, column);
    if (_column.sorter && typeof _column.title !== "function") {
      _column.title = /* @__PURE__ */React__default.default.createElement(TableWidget.ColumnTitle, {
        title: column.title,
        sortOrder: column.sortOrder
      });
    }
    if (_column.align === "right") {
      _column.className = _column.className ? `${_column.className} align-right` : "align-right";
    }
    return _column;
  }), [columns]);
  const dataSource = React.useMemo(() => {
    if (loading) {
      return _dataSource;
    }
    if (!!error) {
      return [];
    }
    return _dataSource != null ? _dataSource : [];
  }, [_dataSource, error, loading]);
  const emptyText = React.useMemo(() => {
    if (loading) {
      return "";
    }
    if (!!error) {
      return error;
    }
    return empty;
  }, [empty, error, loading]);
  return /* @__PURE__ */React__default.default.createElement("div", {
    className: core.cx(TableContainerStyle, "table-container", !hasScrollBard && "no-scroll-bar")
  }, /* @__PURE__ */React__default.default.createElement(antd.Table, {
    className: cs__default.default(tableStyleCover, !(dataSource == null ? void 0 : dataSource.length) && emptyTableStyle, rowSelection && "has-selection"),
    bordered,
    loading: {
      spinning: loading || initLoading,
      indicator: /* @__PURE__ */React__default.default.createElement(TableSkeleton.TableSkeleton, __spreadValues({}, skeletonProps))
    },
    locale: {
      emptyText
    },
    dataSource,
    pagination: pagination || false,
    columns: _columns,
    components,
    rowKey: rowKey || "id",
    tableLayout: (dataSource == null ? void 0 : dataSource.length) ? tableLayout : "auto",
    size: "small",
    onChange: (_, filters, sorter) => {
      if (!(sorter instanceof Array)) {
        orderRef.current = sorter.order || (orderRef.current === "ascend" ? "descend" : "ascend");
        onSorterChange == null ? void 0 : onSorterChange(orderRef.current, sorter.columnKey);
      }
    },
    onRow: (record, index) => __spreadValues({
      onClick: evt => onRowClick == null ? void 0 : onRowClick(record, index, evt)
    }, onRow == null ? void 0 : onRow(record, index)),
    rowClassName,
    scroll,
    rowSelection: rowSelection && __spreadProps(__spreadValues({}, rowSelection), {
      columnWidth: 32
    }),
    showSorterTooltip: false,
    expandable
  }));
};
var Table$1 = Table;

exports.useTableBodyHasScrollBar = common.useTableBodyHasScrollBar;
exports.ColumnTitle = TableWidget.ColumnTitle;
exports.EmptyRowMenu = TableWidget.EmptyRowMenu;
exports.KitTableContext = TableWidget.KitTableContext;
exports.TableLoading = TableWidget.TableLoading;
exports.TableSkeleton = TableSkeleton.TableSkeleton;
exports.default = Table$1;
exports.emptyTableStyle = emptyTableStyle;
exports.tableStyleCover = tableStyleCover;
