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 __spreadArray = this && this.__spreadArray || function (to, from) {
  for (var i = 0, il = from.length, j = to.length; i < il; i++, j++) {
    to[j] = from[i];
  }

  return to;
};

import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import * as React from 'react';
import classNames from 'classnames';
import size from 'lodash/size';
import map from 'lodash/map';
import forEach from 'lodash/forEach';
import get from 'lodash/get';
import isNumber from 'lodash/isNumber';
import "./style/simple-cross-table.css";
import { useEventEmitter, useMemoizedFn, useCreation, useHover } from 'ahooks';
import ScrollX from "./scroll-x";
var prefixCls = 'dmc-simple-cross-table';
var CrossTableCell = /*#__PURE__*/React.memo( /*#__PURE__*/React.forwardRef(function (props, ref) {
  var _c;

  var col = props.col,
      yIndex = props.yIndex,
      xIndex = props.xIndex,
      _e = props.value,
      value = _e === void 0 ? '' : _e,
      renderCell = props.renderCell,
      title = props.title,
      hideSameRender = props.hideSameRender,
      defaultColWidth = props.defaultColWidth,
      defaultColHeight = props.defaultColHeight,
      defaultTitleHeight = props.defaultTitleHeight,
      leftColumnWidth = props.leftColumnWidth,
      nailedLeft = props.nailedLeft,
      currentDataIndex = props.currentDataIndex,
      getRowHeight = props.getRowHeight,
      restProps = __rest(props, ["col", "yIndex", "xIndex", "value", "renderCell", "title", "hideSameRender", "defaultColWidth", "defaultColHeight", "defaultTitleHeight", "leftColumnWidth", "nailedLeft", "currentDataIndex", "getRowHeight"]);

  var firstCell = xIndex === 0;
  var inSortIndex = nailedLeft.indexOf(currentDataIndex);
  var inSort = inSortIndex > -1;
  var left = leftColumnWidth + defaultColWidth * inSortIndex;
  var needBoxShadow = React.useMemo(function () {
    if (inSortIndex === nailedLeft.length - 1 && nailedLeft.length > 0) {
      return true;
    } else {
      if (firstCell) {
        if (nailedLeft.length > 0) {
          return false;
        } else {
          return true;
        }
      } else {
        return false;
      }
    }
  }, [firstCell, inSortIndex, nailedLeft.length]);
  return _jsxs("div", __assign({}, restProps, {
    ref: ref,
    className: classNames(prefixCls + "-col", (_c = {}, _c[prefixCls + "-sticky"] = firstCell || inSort, _c[prefixCls + "-z-index-1"] = firstCell || inSort, _c[prefixCls + "-box-shadow"] = needBoxShadow, _c)),
    style: {
      left: firstCell ? 0 : inSort ? left : '',
      width: xIndex === 0 ? leftColumnWidth : defaultColWidth,
      height: yIndex === 0 ? defaultTitleHeight : getRowHeight ? getRowHeight(col) : defaultColHeight
    }
  }, {
    children: [title !== value && _jsx(_Fragment, {
      children: renderCell ? renderCell(props) : value
    }, void 0), title === value && _jsx(_Fragment, {
      children: hideSameRender ? hideSameRender() : _jsx("div", {
        children: "hideSameRender-\u81EA\u5B9A\u4E49\u533A\u95F4"
      }, void 0)
    }, void 0)]
  }), void 0);
}));
var CrossTableRow = /*#__PURE__*/React.memo(function (props) {
  var _c;

  var yIndex = props.yIndex,
      x = props.x,
      _e = props.data,
      data = _e === void 0 ? [] : _e,
      col = props.col,
      dataIndexSort = props.dataIndexSort,
      sameColumns = props.sameColumns,
      restProps = __rest(props, ["yIndex", "x", "data", "col", "dataIndexSort", "sameColumns"]);

  var ref = React.useRef(null);
  var firstRow = yIndex === 0;
  var isHovering = useHover(ref);
  var isSame = sameColumns.includes(col);
  return _jsx("div", __assign({}, restProps, {
    ref: ref,
    className: classNames(prefixCls + "-row", prefixCls + "-row-" + col, (_c = {}, _c[prefixCls + "-sticky"] = firstRow, _c[prefixCls + "-z-index-20"] = firstRow, _c[prefixCls + "-hover-row"] = isHovering, _c[prefixCls + "-same"] = !firstRow && isSame, _c[prefixCls + "-diff"] = !firstRow && !isSame, _c)),
    style: {
      top: firstRow ? 0 : ''
    }
  }, {
    children: map(new Array(x), function (_b, xIndex) {
      var value = '';

      if (xIndex === 0) {
        value = col;
      } else {
        value = get(data, xIndex - 1 + "." + col);
      }

      return _jsx(CrossTableCell, __assign({
        value: value,
        xIndex: xIndex,
        currentDataIndex: dataIndexSort[xIndex - 1]
      }, props), xIndex);
    })
  }), void 0);
});
var SimpleCrossTable = /*#__PURE__*/React.forwardRef(function (props, ref) {
  var _c;

  var className = props.className,
      _e = props.data,
      data = _e === void 0 ? [] : _e,
      _f = props.width,
      width = _f === void 0 ? 800 : _f,
      _g = props.height,
      height = _g === void 0 ? 300 : _g,
      _h = props.hideSame,
      hideSame = _h === void 0 ? false : _h,
      renderCell = props.renderCell,
      leftColumn = props.leftColumn,
      title = props.title,
      hideSameRender = props.hideSameRender,
      _j = props.defaultColWidth,
      defaultColWidth = _j === void 0 ? 180 : _j,
      _k = props.defaultColHeight,
      defaultColHeight = _k === void 0 ? 56 : _k,
      _l = props.defaultTitleHeight,
      defaultTitleHeight = _l === void 0 ? 80 : _l,
      _m = props.scrollWidth,
      scrollWidth = _m === void 0 ? 400 : _m,
      _o = props.leftColumnWidth,
      leftColumnWidth = _o === void 0 ? defaultColWidth : _o,
      _p = props.nailedLeft,
      nailedLeft = _p === void 0 ? [] : _p,
      getRowHeight = props.getRowHeight,
      restProps = __rest(props, ["className", "data", "width", "height", "hideSame", "renderCell", "leftColumn", "title", "hideSameRender", "defaultColWidth", "defaultColHeight", "defaultTitleHeight", "scrollWidth", "leftColumnWidth", "nailedLeft", "getRowHeight"]); // same Columns


  var _q = React.useState([]),
      sameColumns = _q[0],
      setSameColumns = _q[1]; // 滚动classname


  var _r = React.useState(false),
      ping = _r[0],
      setPing = _r[1];

  var _s = React.useState(data),
      _data = _s[0],
      setData = _s[1];

  var _t = React.useState([]),
      dataIndexSort = _t[0],
      setDataIndexSort = _t[1];

  var x = (size(_data) || 0) + 1;
  var y = size(_data[0]) || 0;
  var needScrollX = useCreation(function () {
    return defaultColWidth * (x - 1) + leftColumnWidth > width;
  }, [defaultColWidth, x, width, leftColumnWidth]);
  var cls = classNames(prefixCls, className, (_c = {}, _c[prefixCls + "-ping"] = ping, _c));
  var event$ = useEventEmitter();

  var _ref = React.useRef(null);
  /**
   * 左侧column
   * 钉左刷新
   * 更新相同项
   */


  React.useEffect(function () {
    // 左侧column
    var _d = [];

    for (var i = 0; i < data.length; i++) {
      var d = data[i];
      var _dd = {};

      for (var j = 0; j < leftColumn.length; j++) {
        var col = leftColumn[j];

        if (col in d) {
          _dd[col] = d[col];
        }
      }

      _d.push(_dd);
    } // 更新相同项


    var _ = {};

    for (var i = 0; i < _d.length; i++) {
      var d = _d[i];

      for (var j = 0; j < leftColumn.length; j++) {
        var col = leftColumn[j];
        var value = d[col];
        _[col] = _[col] || [];

        if (!_[col].includes(value)) {
          _[col].push(value);
        }
      }
    }

    var filterCol = [];
    forEach(_, function (v, k) {
      if (size(v) < 2) {
        filterCol.push(k);
      }
    });
    setSameColumns(filterCol);
    var arr = nailedLeft;
    var arr1 = [];
    var arr2 = [];
    var _dataIndex1 = [];
    var _dataIndex2 = [];

    for (var i = 0; i < _d.length; i++) {
      var index = arr.indexOf(i);

      if (index > -1) {
        // TODO 按照 顺序
        arr1[index] = _d[i];
        _dataIndex1[index] = i; // arr1.push(data[i]);
        // _dataIndex1.push(i);
      } else {
        arr2.push(_d[i]);

        _dataIndex2.push(i);
      }
    }

    setData(__spreadArray(__spreadArray([], arr1), arr2));
    setDataIndexSort(__spreadArray(__spreadArray([], _dataIndex1), _dataIndex2));
  }, [data, leftColumn, nailedLeft]);

  var _onScroll = useMemoizedFn(function (e) {
    var _c;

    var scrollLeft = ((_c = e.target) === null || _c === void 0 ? void 0 : _c.scrollLeft) || 0;

    if (scrollLeft > 0) {
      setPing(true);
    } else if (scrollLeft === 0) {
      setPing(false);
    }

    event$ === null || event$ === void 0 ? void 0 : event$.emit(scrollLeft);
  }); // hideSame 更新


  React.useEffect(function () {
    var doms = document.querySelectorAll("." + prefixCls + "-same");

    if (doms) {
      for (var i = 0; i < doms.length; i++) {
        var dom = doms[i];
        if (dom) dom.style.display = hideSame ? 'none' : 'flex';
      }
    }
  }, [hideSame]);
  var setScrollLeft = useMemoizedFn(function (scrollLeft) {
    var _c;

    var dom = (_c = _ref.current) === null || _c === void 0 ? void 0 : _c.querySelector("." + prefixCls);
    if (dom) dom.scrollLeft = scrollLeft;
  });
  var setScrollTop = useMemoizedFn(function (scrollTop) {
    var _c;

    var dom = (_c = _ref.current) === null || _c === void 0 ? void 0 : _c.querySelector("." + prefixCls);
    if (dom) dom.scrollTop = scrollTop;
  });
  event$ === null || event$ === void 0 ? void 0 : event$.useSubscription(function (value) {
    if (isNumber(value)) {
      setScrollLeft(value);
    }
  });
  var getSameRows = useMemoizedFn(function () {
    var _c;

    var doms = (_c = _ref.current) === null || _c === void 0 ? void 0 : _c.querySelectorAll("." + prefixCls + "-same");
    return doms;
  });
  var getDiffRows = useMemoizedFn(function () {
    var _c;

    var doms = (_c = _ref.current) === null || _c === void 0 ? void 0 : _c.querySelectorAll("." + prefixCls + "-diff");
    return doms;
  });
  React.useImperativeHandle(ref, function () {
    return {
      dom: _ref.current,
      setScrollLeft: setScrollLeft,
      setScrollTop: setScrollTop,
      getSameRows: getSameRows,
      getDiffRows: getDiffRows
    };
  });
  return _jsxs("div", __assign({}, restProps, {
    style: {
      display: 'inline-block'
    },
    ref: _ref
  }, {
    children: [_jsx("div", __assign({
      className: cls,
      style: {
        width: width,
        height: height - (needScrollX ? 28 : 0)
      },
      onScroll: _onScroll
    }, {
      children: map(new Array(y), function (_a, yIndex) {
        var col = leftColumn[yIndex];
        return _jsx(CrossTableRow, {
          yIndex: yIndex,
          x: x,
          data: _data,
          col: col,
          renderCell: renderCell,
          title: title,
          hideSameRender: hideSameRender,
          defaultColWidth: defaultColWidth,
          defaultColHeight: defaultColHeight,
          defaultTitleHeight: defaultTitleHeight,
          leftColumnWidth: leftColumnWidth,
          dataIndexSort: dataIndexSort,
          nailedLeft: nailedLeft,
          sameColumns: sameColumns,
          getRowHeight: getRowHeight
        }, yIndex);
      })
    }), void 0), needScrollX && _jsx("div", __assign({
      style: {
        display: 'flex',
        flexDirection: 'row-reverse'
      }
    }, {
      children: _jsx(ScrollX, {
        "event$": event$,
        width: width,
        allWidth: defaultColWidth * (x - 1) + leftColumnWidth,
        scrollWidth: scrollWidth,
        stepWidth: width - leftColumnWidth - nailedLeft.length * defaultColWidth
      }, void 0)
    }), void 0)]
  }), void 0);
});
export default SimpleCrossTable;

