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

  return to;
};

var __importDefault = void 0 && (void 0).__importDefault || function (mod) {
  return mod && mod.__esModule ? mod : {
    "default": mod
  };
};

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

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

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

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

var size_1 = __importDefault(require("lodash/size"));

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

var forEach_1 = __importDefault(require("lodash/forEach"));

var get_1 = __importDefault(require("lodash/get"));

var isNumber_1 = __importDefault(require("lodash/isNumber"));

require("./style/simple-cross-table.css");

var ahooks_1 = require("ahooks");

var scroll_x_1 = __importDefault(require("./scroll-x"));

var prefixCls = 'dmc-simple-cross-table';
var CrossTableCell = React.memo(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 jsx_runtime_1.jsxs("div", __assign({}, restProps, {
    ref: ref,
    className: classnames_1["default"](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_runtime_1.jsx(jsx_runtime_1.Fragment, {
      children: renderCell ? renderCell(props) : value
    }, void 0), title === value && jsx_runtime_1.jsx(jsx_runtime_1.Fragment, {
      children: hideSameRender ? hideSameRender() : jsx_runtime_1.jsx("div", {
        children: "hideSameRender-\u81EA\u5B9A\u4E49\u533A\u95F4"
      }, void 0)
    }, void 0)]
  }), void 0);
}));
var CrossTableRow = 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 = ahooks_1.useHover(ref);
  var isSame = sameColumns.includes(col);
  return jsx_runtime_1.jsx("div", __assign({}, restProps, {
    ref: ref,
    className: classnames_1["default"](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_1["default"](new Array(x), function (_b, xIndex) {
      var value = '';

      if (xIndex === 0) {
        value = col;
      } else {
        value = get_1["default"](data, xIndex - 1 + "." + col);
      }

      return jsx_runtime_1.jsx(CrossTableCell, __assign({
        value: value,
        xIndex: xIndex,
        currentDataIndex: dataIndexSort[xIndex - 1]
      }, props), xIndex);
    })
  }), void 0);
});
var SimpleCrossTable = 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_1["default"](_data) || 0) + 1;
  var y = size_1["default"](_data[0]) || 0;
  var needScrollX = ahooks_1.useCreation(function () {
    return defaultColWidth * (x - 1) + leftColumnWidth > width;
  }, [defaultColWidth, x, width, leftColumnWidth]);
  var cls = classnames_1["default"](prefixCls, className, (_c = {}, _c[prefixCls + "-ping"] = ping, _c));
  var event$ = ahooks_1.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_1["default"](_, function (v, k) {
      if (size_1["default"](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 = ahooks_1.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 = ahooks_1.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 = ahooks_1.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_1["default"](value)) {
      setScrollLeft(value);
    }
  });
  var getSameRows = ahooks_1.useMemoizedFn(function () {
    var _c;

    var doms = (_c = _ref.current) === null || _c === void 0 ? void 0 : _c.querySelectorAll("." + prefixCls + "-same");
    return doms;
  });
  var getDiffRows = ahooks_1.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 jsx_runtime_1.jsxs("div", __assign({}, restProps, {
    style: {
      display: 'inline-block'
    },
    ref: _ref
  }, {
    children: [jsx_runtime_1.jsx("div", __assign({
      className: cls,
      style: {
        width: width,
        height: height - (needScrollX ? 28 : 0)
      },
      onScroll: _onScroll
    }, {
      children: map_1["default"](new Array(y), function (_a, yIndex) {
        var col = leftColumn[yIndex];
        return jsx_runtime_1.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_runtime_1.jsx("div", __assign({
      style: {
        display: 'flex',
        flexDirection: 'row-reverse'
      }
    }, {
      children: jsx_runtime_1.jsx(scroll_x_1["default"], {
        "event$": event$,
        width: width,
        allWidth: defaultColWidth * (x - 1) + leftColumnWidth,
        scrollWidth: scrollWidth,
        stepWidth: width - leftColumnWidth - nailedLeft.length * defaultColWidth
      }, void 0)
    }), void 0)]
  }), void 0);
});
exports["default"] = SimpleCrossTable;

