var __assign = this && this.__assign || function () {
  __assign = Object.assign || function (t) {
    for (var s, i = 1, n = arguments.length; i < n; i++) {
      s = arguments[i];

      for (var p in s) {
        if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
      }
    }

    return t;
  };

  return __assign.apply(this, arguments);
};

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 _a;

import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
import * as React from 'react';
import DataGrid from 'react-data-grid';
import classNames from 'classnames';
import map from 'lodash/map';
import Icon from "../icon";
import { Dropmenu } from "../dropdown";
import { randomUuid } from "../_utils/stringUtil";
import useMeasure from 'react-use/lib/useMeasure';
import "./style/index.css";
export var FieldType;

(function (FieldType) {
  FieldType["text"] = "text";
  FieldType["number"] = "number";
  FieldType["lnglat"] = "lnglat";
  FieldType["datetime"] = "datetime";
  FieldType["bool"] = "bool";
  FieldType["formula"] = "formula";
  FieldType["geometry"] = "geometry";
  FieldType["image"] = "image";
})(FieldType || (FieldType = {}));

var IconMap = (_a = {}, _a[FieldType.text] = 'text', _a[FieldType.number] = 'num', _a[FieldType.datetime] = 'time', _a[FieldType.lnglat] = 'geo-global', _a[FieldType.image] = 'image', _a);
var prefixCls = 'dmc-datatable';
export var SortTypeEnum;

(function (SortTypeEnum) {
  SortTypeEnum["UNSET"] = "unset";
  SortTypeEnum["ASCEND"] = "ascend";
  SortTypeEnum["DESCEND"] = "descend";
})(SortTypeEnum || (SortTypeEnum = {}));

var EmptyRowsRenderer = function EmptyRowsRenderer() {
  return _jsx("div", __assign({
    className: prefixCls + "-empty-row"
  }, {
    children: "\u6682\u65E0\u6570\u636E"
  }), void 0);
};

var DataTable = function DataTable(_a) {
  var _b;

  var columns = _a.columns,
      rowHeight = _a.rowHeight,
      headerRowHeight = _a.headerRowHeight,
      className = _a.className,
      type = _a.type,
      emptyRowsRenderer = _a.emptyRowsRenderer,
      rows = _a.rows,
      loading = _a.loading,
      enableFilters = _a.enableFilters,
      restProps = __rest(_a, ["columns", "rowHeight", "headerRowHeight", "className", "type", "emptyRowsRenderer", "rows", "loading", "enableFilters"]);

  var _c = useMeasure(),
      ref = _c[0],
      height = _c[1].height;

  var sortTypeArr = [SortTypeEnum.UNSET, SortTypeEnum.ASCEND, SortTypeEnum.DESCEND];

  var _d = React.useState({}),
      sortTypeMap = _d[0],
      setSortTypeMap = _d[1];

  var mixColumns = map(columns, function (column) {
    var _a;

    return __assign(__assign({
      headerRenderer: function headerRenderer(p) {
        var _a = p.column,
            columnKey = _a.key,
            type = _a.type,
            dropmenuProps = _a.dropmenuProps,
            sorter = _a.sorter,
            columnSortType = _a.sortType;
        var isSorter = typeof sorter === 'function';
        var icon = type ? IconMap[type] : 'text';

        var SortDom = function SortDom(_a) {
          var _b, _c;

          var visible = _a.visible,
              sortKey = _a.sortKey;
          var sortType = columnSortType || sortTypeMap[sortKey] || SortTypeEnum.UNSET;
          return _jsx(_Fragment, {
            children: visible ? _jsxs("div", __assign({
              className: prefixCls + "-header-cell-sort",
              onClick: function onClick(e) {
                var _a;

                e.preventDefault();
                e.stopPropagation();
                var currentIndex = sortTypeArr.findIndex(function (item) {
                  return item === sortType;
                });
                var nextIndex = sortTypeArr.length - 1 === currentIndex ? 0 : currentIndex + 1;
                !columnSortType && setSortTypeMap(__assign(__assign({}, sortTypeMap), (_a = {}, _a[sortKey] = sortTypeArr[nextIndex], _a)));
                sorter === null || sorter === void 0 ? void 0 : sorter(sortTypeArr[nextIndex], p);
              }
            }, {
              children: [_jsx(Icon, {
                icon: "arrow-up",
                className: classNames((_b = {}, _b[prefixCls + "-header-cell-sort-active"] = sortType === SortTypeEnum.ASCEND, _b))
              }, void 0), _jsx(Icon, {
                icon: "arrow-down",
                className: classNames((_c = {}, _c[prefixCls + "-header-cell-sort-active"] = sortType === SortTypeEnum.DESCEND, _c))
              }, void 0)]
            }), void 0) : null
          }, void 0);
        };

        var node = _jsxs("div", __assign({
          className: prefixCls + "-header-cell-wrapper"
        }, {
          children: [_jsx(Icon, {
            icon: icon
          }, void 0), p.column.name]
        }), void 0);

        return !dropmenuProps ? _jsxs("div", __assign({
          className: prefixCls + "-header-cell-sorter"
        }, {
          children: [node, _jsx(SortDom, {
            visible: isSorter,
            sortKey: columnKey
          }, void 0)]
        }), void 0) : _jsx(Dropmenu, __assign({}, dropmenuProps, {
          renderDropNode: function renderDropNode() {
            return _jsxs("div", __assign({
              className: prefixCls + "-header-cell-dropmenu"
            }, {
              children: [node, _jsxs("div", __assign({
                className: prefixCls + "-header-cell-operate"
              }, {
                children: [_jsx(SortDom, {
                  visible: isSorter,
                  sortKey: columnKey
                }, void 0), _jsx(Icon, {
                  icon: "arrow-down"
                }, void 0)]
              }), void 0)]
            }), void 0);
          }
        }), void 0);
      }
    }, column), {
      headerCellClass: classNames(column.headerCellClass, prefixCls + "-header-cell", (_a = {}, _a[prefixCls + "-header-cell-" + (column.type || 'text')] = column.type || 'text', _a))
    });
  });
  var uuid = React.useMemo(function () {
    return randomUuid();
  }, []);
  var dataTableCls = classNames(className, prefixCls, prefixCls + "-" + uuid, (_b = {}, _b[prefixCls + "-" + type] = type, _b[prefixCls + "-empty"] = rows.length === 0, _b));
  var headerHeight = (headerRowHeight || 45) + (enableFilters ? 45 : 0);
  var contentHeight = height - headerHeight;
  return _jsxs("div", __assign({
    className: prefixCls + "-wrapper",
    ref: ref
  }, {
    children: [_jsx(DataGrid, __assign({}, restProps, {
      enableFilters: enableFilters,
      columns: mixColumns,
      rows: rows,
      rowHeight: rowHeight || 36,
      headerRowHeight: headerRowHeight || 45,
      className: dataTableCls,
      emptyRowsRenderer: emptyRowsRenderer || EmptyRowsRenderer
    }), void 0), loading && _jsx("div", __assign({
      style: {
        height: contentHeight,
        marginTop: headerHeight
      },
      className: prefixCls + "-loading"
    }, {
      children: _jsx(Icon, {
        icon: "loading"
      }, void 0)
    }), void 0)]
  }), void 0);
};

DataTable.displayName = 'DataTable';
export default DataTable;
var DataTableMemo = /*#__PURE__*/React.memo(DataTable);
DataTableMemo.displayName = 'DataTableMemo';
export { DataTableMemo };

