"use strict";

var __assign = void 0 && (void 0).__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 __createBinding = void 0 && (void 0).__createBinding || (Object.create ? function (o, m, k, k2) {
  if (k2 === undefined) k2 = k;
  Object.defineProperty(o, k2, {
    enumerable: true,
    get: function get() {
      return m[k];
    }
  });
} : function (o, m, k, k2) {
  if (k2 === undefined) k2 = k;
  o[k2] = m[k];
});

var __setModuleDefault = void 0 && (void 0).__setModuleDefault || (Object.create ? function (o, v) {
  Object.defineProperty(o, "default", {
    enumerable: true,
    value: v
  });
} : function (o, v) {
  o["default"] = v;
});

var __importStar = void 0 && (void 0).__importStar || function (mod) {
  if (mod && mod.__esModule) return mod;
  var result = {};
  if (mod != null) for (var k in mod) {
    if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  }

  __setModuleDefault(result, mod);

  return result;
};

var __rest = void 0 && (void 0).__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 = void 0 && (void 0).__importDefault || function (mod) {
  return mod && mod.__esModule ? mod : {
    "default": mod
  };
};

var _a;

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.DataTableMemo = exports.SortTypeEnum = exports.FieldType = void 0;

var jsx_runtime_1 = require("react/jsx-runtime");

var React = __importStar(require("react"));

var react_data_grid_1 = __importDefault(require("react-data-grid"));

var classnames_1 = __importDefault(require("classnames"));

var map_1 = __importDefault(require("lodash/map"));

var icon_1 = __importDefault(require("../icon"));

var dropdown_1 = require("../dropdown");

var stringUtil_1 = require("../_utils/stringUtil");

var useMeasure_1 = __importDefault(require("react-use/lib/useMeasure"));

require("./style/index.css");

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 = exports.FieldType || (exports.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';
var SortTypeEnum;

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

var EmptyRowsRenderer = function EmptyRowsRenderer() {
  return jsx_runtime_1.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_1["default"](),
      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_1["default"](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_runtime_1.jsx(jsx_runtime_1.Fragment, {
            children: visible ? jsx_runtime_1.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_runtime_1.jsx(icon_1["default"], {
                icon: "arrow-up",
                className: classnames_1["default"]((_b = {}, _b[prefixCls + "-header-cell-sort-active"] = sortType === SortTypeEnum.ASCEND, _b))
              }, void 0), jsx_runtime_1.jsx(icon_1["default"], {
                icon: "arrow-down",
                className: classnames_1["default"]((_c = {}, _c[prefixCls + "-header-cell-sort-active"] = sortType === SortTypeEnum.DESCEND, _c))
              }, void 0)]
            }), void 0) : null
          }, void 0);
        };

        var node = jsx_runtime_1.jsxs("div", __assign({
          className: prefixCls + "-header-cell-wrapper"
        }, {
          children: [jsx_runtime_1.jsx(icon_1["default"], {
            icon: icon
          }, void 0), p.column.name]
        }), void 0);
        return !dropmenuProps ? jsx_runtime_1.jsxs("div", __assign({
          className: prefixCls + "-header-cell-sorter"
        }, {
          children: [node, jsx_runtime_1.jsx(SortDom, {
            visible: isSorter,
            sortKey: columnKey
          }, void 0)]
        }), void 0) : jsx_runtime_1.jsx(dropdown_1.Dropmenu, __assign({}, dropmenuProps, {
          renderDropNode: function renderDropNode() {
            return jsx_runtime_1.jsxs("div", __assign({
              className: prefixCls + "-header-cell-dropmenu"
            }, {
              children: [node, jsx_runtime_1.jsxs("div", __assign({
                className: prefixCls + "-header-cell-operate"
              }, {
                children: [jsx_runtime_1.jsx(SortDom, {
                  visible: isSorter,
                  sortKey: columnKey
                }, void 0), jsx_runtime_1.jsx(icon_1["default"], {
                  icon: "arrow-down"
                }, void 0)]
              }), void 0)]
            }), void 0);
          }
        }), void 0);
      }
    }, column), {
      headerCellClass: classnames_1["default"](column.headerCellClass, prefixCls + "-header-cell", (_a = {}, _a[prefixCls + "-header-cell-" + (column.type || 'text')] = column.type || 'text', _a))
    });
  });
  var uuid = React.useMemo(function () {
    return stringUtil_1.randomUuid();
  }, []);
  var dataTableCls = classnames_1["default"](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 jsx_runtime_1.jsxs("div", __assign({
    className: prefixCls + "-wrapper",
    ref: ref
  }, {
    children: [jsx_runtime_1.jsx(react_data_grid_1["default"], __assign({}, restProps, {
      enableFilters: enableFilters,
      columns: mixColumns,
      rows: rows,
      rowHeight: rowHeight || 36,
      headerRowHeight: headerRowHeight || 45,
      className: dataTableCls,
      emptyRowsRenderer: emptyRowsRenderer || EmptyRowsRenderer
    }), void 0), loading && jsx_runtime_1.jsx("div", __assign({
      style: {
        height: contentHeight,
        marginTop: headerHeight
      },
      className: prefixCls + "-loading"
    }, {
      children: jsx_runtime_1.jsx(icon_1["default"], {
        icon: "loading"
      }, void 0)
    }), void 0)]
  }), void 0);
};

DataTable.displayName = 'DataTable';
exports["default"] = DataTable;
var DataTableMemo = React.memo(DataTable);
exports.DataTableMemo = DataTableMemo;
DataTableMemo.displayName = 'DataTableMemo';

