/* eslint-disable */
import React from 'react';

import './index.less';
var throttleTime = 1;

function getAllChildren(data, expandedKeys, rowKey) {
  var result = [];
  var expandedKeysNew = [];
  if (typeof data === 'object') {
    data.forEach((item) => {
      if (item && item.children && item.children.length) {
        var children = item.children.map((child) => {
          const temp = child;
          temp.vlistParentRowKey = item[rowKey];
          return temp;
        });
        if (expandedKeys.has(item[rowKey])) {
          result = result.concat(children);
          expandedKeysNew = expandedKeysNew.concat(item[rowKey]);
          var nextData = getAllChildren(children, expandedKeys, rowKey);
          result = result.concat(nextData[0]);
          expandedKeysNew = expandedKeysNew.concat(nextData[1]);
        }
      }
    });
  }
  return [result, expandedKeysNew];
}

function _interopDefault(e) {
  return e && 'object' == typeof e && 'default' in e ? e.default : e;
}
function __rest(e, t) {
  var r = {};
  for (var o in e)
    Object.prototype.hasOwnProperty.call(e, o) &&
      t.indexOf(o) < 0 &&
      (r[o] = e[o]);
  if (null != e && 'function' == typeof Object.getOwnPropertySymbols)
    for (var n = 0, o = Object.getOwnPropertySymbols(e); n < o.length; n++)
      t.indexOf(o[n]) < 0 &&
        Object.prototype.propertyIsEnumerable.call(e, o[n]) &&
        (r[o[n]] = e[o[n]]);
  return r;
}
function getRawTag(e) {
  var t = hasOwnProperty.call(e, symToStringTag),
    r = e[symToStringTag];
  try {
    e[symToStringTag] = void 0;
    var o = !0;
  } catch (e) {}
  var n = nativeObjectToString.call(e);
  return o && (t ? (e[symToStringTag] = r) : delete e[symToStringTag]), n;
}
function objectToString(e) {
  return nativeObjectToString$1.call(e);
}
function baseGetTag(e) {
  return null == e
    ? void 0 === e
      ? undefinedTag
      : nullTag
    : symToStringTag$1 && symToStringTag$1 in Object(e)
    ? getRawTag(e)
    : objectToString(e);
}
function isObjectLike(e) {
  return null != e && 'object' == typeof e;
}
function isSymbol(e) {
  return (
    'symbol' == typeof e || (isObjectLike(e) && baseGetTag(e) == symbolTag)
  );
}
function trimmedEndIndex(e) {
  for (var t = e.length; t-- && reWhitespace.test(e.charAt(t)); );
  return t;
}
function baseTrim(e) {
  return e ? e.slice(0, trimmedEndIndex(e) + 1).replace(reTrimStart, '') : e;
}
function isObject(e) {
  var t = typeof e;
  return null != e && ('object' == t || 'function' == t);
}
function toNumber(e) {
  if ('number' == typeof e) return e;
  if (isSymbol(e)) return NAN;
  if (isObject(e)) {
    var t = 'function' == typeof e.valueOf ? e.valueOf() : e;
    e = isObject(t) ? t + '' : t;
  }
  if ('string' != typeof e) return 0 === e ? e : +e;
  e = baseTrim(e);
  var r = reIsBinary.test(e);
  return r || reIsOctal.test(e)
    ? freeParseInt(e.slice(2), r ? 2 : 8)
    : reIsBadHex.test(e)
    ? NAN
    : +e;
}
function debounce(e, t, r) {
  function o(t) {
    var r = v,
      o = f;
    return (v = f = void 0), (T = t), (p = e.apply(o, r));
  }
  function n(e) {
    return (T = e), (b = setTimeout(a, t)), _ ? o(e) : p;
  }
  function i(e) {
    var r = e - T,
      o = t - (e - h);
    return y ? nativeMin(o, g - r) : o;
  }
  function l(e) {
    var r = e - h,
      o = e - T;
    return void 0 === h || r >= t || r < 0 || (y && o >= g);
  }
  function a() {
    var e = now();
    if (l(e)) return c(e);
    b = setTimeout(a, i(e));
  }
  function c(e) {
    return (b = void 0), m && v ? o(e) : ((v = f = void 0), p);
  }
  function s() {
    void 0 !== b && clearTimeout(b), (T = 0), (v = h = f = b = void 0);
  }
  function u() {
    return void 0 === b ? p : c(now());
  }
  function d() {
    var e = now(),
      r = l(e);
    if (((v = arguments), (f = this), (h = e), r)) {
      if (void 0 === b) return n(h);
      if (y) return clearTimeout(b), (b = setTimeout(a, t)), o(h);
    }
    return void 0 === b && (b = setTimeout(a, t)), p;
  }
  var v,
    f,
    g,
    p,
    b,
    h,
    T = 0,
    _ = !1,
    y = !1,
    m = !0;
  if ('function' != typeof e) throw new TypeError(FUNC_ERROR_TEXT);
  return (
    (t = toNumber(t) || 0),
    isObject(r) &&
      ((_ = !!r.leading),
      (g = (y = 'maxWait' in r) ? nativeMax(toNumber(r.maxWait) || 0, t) : g),
      (m = 'trailing' in r ? !!r.trailing : m)),
    (d.cancel = s),
    (d.flush = u),
    d
  );
}
function isNumber(e) {
  return (
    'number' == typeof e || (isObjectLike(e) && baseGetTag(e) == numberTag)
  );
}
function throttle(e, t, r) {
  var o = !0,
    n = !0;
  if ('function' != typeof e) throw new TypeError(FUNC_ERROR_TEXT$1);
  return (
    isObject(r) &&
      ((o = 'leading' in r ? !!r.leading : o),
      (n = 'trailing' in r ? !!r.trailing : n)),
    debounce(e, t, {
      leading: o,
      maxWait: t,
      trailing: n,
    })
  );
}
function styleInject(e, t) {
  void 0 === t && (t = {});
  var r = t.insertAt;
  if (e && 'undefined' != typeof document) {
    var o = document.head || document.getElementsByTagName('head')[0],
      n = document.createElement('style');
    (n.type = 'text/css'),
      'top' === r && o.firstChild
        ? o.insertBefore(n, o.firstChild)
        : o.appendChild(n),
      n.styleSheet
        ? (n.styleSheet.cssText = e)
        : n.appendChild(document.createTextNode(e));
  }
}
function reducer(e, t) {
  var r = t.scrollHeight,
    o = t.curScrollTop,
    n = t.tableScrollY,
    i = t.rowHeight,
    l = t.totalLen,
    a = t.ifScrollTopClear,
    c = e.curScrollTop;
  switch (t.type) {
    case 'changeTrs':
      return __assign(__assign({}, e), {
        curScrollTop: o,
        scrollHeight: r,
        tableScrollY: n,
      });
    case 'initHeight':
      return __assign(__assign({}, e), {
        rowHeight: i,
      });
    case 'changeTotalLen':
      return (
        0 === l && (c = 0),
        __assign(__assign({}, e), {
          totalLen: l,
          curScrollTop: c,
        })
      );
    case 'reset':
      return __assign(__assign({}, e), {
        curScrollTop: a ? 0 : e.curScrollTop,
        scrollHeight: 0,
      });
    default:
      throw new Error();
  }
}
function VCell(e) {
  var t = e.children,
    r = __rest(e, ['children']);
  return React__default.createElement(
    'td',
    __assign({}, r),
    React__default.createElement('div', null, t),
  );
}
function VRow(e, t) {
  var r = React.useContext(ScrollContext),
    o = r.dispatch,
    n = r.rowHeight,
    i = r.totalLen,
    l = r.vid,
    a = e.children,
    c = e.style,
    s = __rest(e, ['children', 'style']),
    u = React.useRef(null);
  return (
    React.useEffect(
      function () {
        !(function (e) {
          var t, r, a;
          if (
            (null === (t = null === e || void 0 === e ? void 0 : e.current) ||
            void 0 === t
              ? void 0
              : t.offsetHeight) &&
            !n &&
            i
          ) {
            var c =
              null !==
                (a =
                  null ===
                    (r = null === e || void 0 === e ? void 0 : e.current) ||
                  void 0 === r
                    ? void 0
                    : r.offsetHeight) && void 0 !== a
                ? a
                : 0;
            vidMap.set(
              l,
              __assign(__assign({}, vidMap.get(l)), {
                rowItemHeight: c,
              }),
            ),
              o({
                type: 'initHeight',
                rowHeight: c,
              });
          }
        })(Object.prototype.hasOwnProperty.call(t, 'current') ? t : u);
      },
      [u, o, n, i, t, l],
    ),
    React__default.createElement(
      'tr',
      __assign({}, s, {
        ref: Object.prototype.hasOwnProperty.call(t, 'current') ? t : u,
        style: __assign(__assign({}, c), {
          height: n || 'auto',
          boxSizing: 'border-box',
        }),
      }),
      a,
    )
  );
}
function VWrapper(e) {
  var t = e.children,
    r = __rest(e, ['children']),
    o = React.useContext(ScrollContext),
    n = o.renderLen,
    i = o.start,
    l = o.dispatch,
    a = o.totalLen,
    c = o.vid,
    s = React.useMemo(
      function () {
        return t[1];
      },
      [t],
    );
  React.useEffect(
    function () {
      var e;
      a !== (null === s || void 0 === s ? void 0 : s.length) &&
        a &&
        (l({
          type: 'changeTotalLen',
          totalLen:
            null !== (e = null === s || void 0 === s ? void 0 : s.length) &&
            void 0 !== e
              ? e
              : 0,
        }),
        vidMap.has(c) &&
          vidMap.set(
            c,
            __assign(__assign({}, vidMap.get(c)), {
              notRefresh: !0,
            }),
          ));
    },
    [a, s, l, c],
  );
  var u = null;
  // return u = Array.isArray(s) && s.length ? [t[0], s.slice(i, i + n).map(function(e) {
  //   return Array.isArray(e) ? e[0] : e
  // })] : t,
  let temp;
  if (Array.isArray(s) && s.length) {
    temp = [
      ...s.slice(i, i + n).map(function (e) {
        return Array.isArray(e) ? e[0] : e;
      }),
    ];
  }
  return (
    (u = Array.isArray(s) && s.length ? [t[0], temp] : t),
    React__default.createElement('tbody', __assign({}, r), u)
  );
}
function VTable(e, t) {
  var r,
    o,
    n,
    i,
    l,
    a,
    c = e.style,
    s = e.children,
    u = __rest(e, ['style', 'children']),
    d = c.width,
    v = __rest(c, ['width']),
    f = null !== t && void 0 !== t ? t : {},
    g = f.vid,
    p = f.scrollY,
    b = f.reachEnd,
    h = f.onScroll,
    T = React.useReducer(reducer, initialState),
    _ = T[0],
    y = T[1],
    m = React.useRef(null),
    S = React.useRef(null),
    w = React.useState(
      null !==
        (i =
          null ===
            (n =
              null ===
                (o = null === (r = s[1]) || void 0 === r ? void 0 : r.props) ||
              void 0 === o
                ? void 0
                : o.data) || void 0 === n
            ? void 0
            : n.length) && void 0 !== i
        ? i
        : 0,
    ),
    R = w[0],
    O = w[1];

  var newR = R;
  if (vidMap && vidMap.get(t.vid) && vidMap.get(t.vid).rowKey) {
    const rowKey = vidMap.get(t.vid).rowKey;
    const getAllChildrenData = getAllChildren(o.data, o.expandedKeys, rowKey);
    var childrenList = getAllChildrenData[0];
    var childrenExpandedKeys = getAllChildrenData[1];
    if (childrenList && childrenList.length) {
      var expandedKeys = o.expandedKeys;
      if (expandedKeys && expandedKeys.size) {
        const expandedChildrenList = childrenList.filter((item) =>
          expandedKeys.has(item.vlistParentRowKey),
        );
        const expandedChildrenKeysList = expandedChildrenList.map(
          (item) => item[rowKey],
        );
        const allExpandedKeysList = childrenExpandedKeys
          .concat(expandedChildrenKeysList)
          .concat(o.data.map((item) => item[rowKey]));
        const allExpandedKeysSet = new Set(allExpandedKeysList);
        newR = allExpandedKeysSet.size;
      }
    }
  }

  if (_.curScrollTop === 0 && vidMap && vidMap.get(t.vid)) {
    const scrollNode = vidMap.get(t.vid).scrollNode;
    if (scrollNode && scrollNode.scrollTop) {
      _.curScrollTop = scrollNode.scrollTop;
    }
  }

  React.useEffect(
    function () {
      _.totalLen && O(_.totalLen);
    },
    [_.totalLen],
  ),
    React.useEffect(
      function () {
        return function () {
          vidMap.delete(g);
        };
      },
      [g],
    ),
    React.useEffect(
      function () {
        var e, t, r, o, n, i;
        isNumber(
          null ===
            (r =
              null ===
                (t = null === (e = s[1]) || void 0 === e ? void 0 : e.props) ||
              void 0 === t
                ? void 0
                : t.data) || void 0 === r
            ? void 0
            : r.length,
        ) &&
          O(
            null ===
              (i =
                null ===
                  (n =
                    null === (o = s[1]) || void 0 === o ? void 0 : o.props) ||
                void 0 === n
                  ? void 0
                  : n.data) || void 0 === i
              ? void 0
              : i.length,
          );
      },
      [s[1].props.data],
    );
  var j = React.useMemo(
      function () {
        var e = 'auto';
        return _.rowHeight && newR && (e = _.rowHeight * newR), e;
      },
      [_.rowHeight, newR, o.expandedKeys],
    ),
    E = 0;
  let maxHeight = '';
  if (
    vidMap &&
    vidMap.get(t.vid) &&
    vidMap.get(t.vid).scrollNode &&
    vidMap.get(t.vid).scrollNode.style.maxHeight
  ) {
    maxHeight = vidMap.get(t.vid).scrollNode.style.maxHeight;
  }
  (E =
    'string' == typeof p
      ? null ===
          (a =
            null === (l = m.current) || void 0 === l ? void 0 : l.parentNode) ||
        void 0 === a
        ? void 0
        : a.offsetHeight
      : p),
    isNumber(j) &&
      (j < E || (E === 0 && newR) || maxHeight === '100%') &&
      (E = j),
    E <= 0 && (E = 0);
  var x = React.useMemo(
      function () {
        var e = 1;
        if (_.rowHeight && newR && E)
          if (E <= 0) e = 0;
          else {
            var t = 1 + ((E / _.rowHeight) | 0) + 2;
            e = t > newR ? newR : t;
          }
        return e;
      },
      [_.rowHeight, newR, E],
    ),
    H = _.rowHeight ? (_.curScrollTop / _.rowHeight) | 0 : 0,
    L = _.rowHeight ? _.curScrollTop % _.rowHeight : 0;
  return (
    _.curScrollTop && _.rowHeight && _.curScrollTop > _.rowHeight
      ? ((H -= 1), (L += _.rowHeight))
      : (H = 0),
    React.useEffect(
      function () {
        var e,
          t,
          r = null === (e = m.current) || void 0 === e ? void 0 : e.parentNode,
          o = null !== (t = vidMap.get(g)) && void 0 !== t ? t : {};
        (null === o || void 0 === o ? void 0 : o.notRefresh) ||
          (b
            ? y({
                type: 'reset',
                ifScrollTopClear: !1,
              })
            : (false && r && (r.scrollTop = 0),
              y({
                type: 'reset',
                ifScrollTopClear: !0,
              }))),
          vidMap.has(g) &&
            vidMap.set(
              g,
              __assign(__assign({}, o), {
                scrollNode: r,
                notRefresh: !1,
              }),
            );
      },
      [newR, b, g],
    ),
    React.useEffect(
      function () {
        var e,
          t = throttle(function (e) {
            var t,
              r,
              o,
              n,
              i,
              l,
              a =
                null !==
                  (r =
                    null ===
                      (t = null === e || void 0 === e ? void 0 : e.target) ||
                    void 0 === t
                      ? void 0
                      : t.scrollTop) && void 0 !== r
                  ? r
                  : 0,
              c =
                null !==
                  (n =
                    null ===
                      (o = null === e || void 0 === e ? void 0 : e.target) ||
                    void 0 === o
                      ? void 0
                      : o.scrollHeight) && void 0 !== n
                  ? n
                  : 0,
              s =
                null !==
                  (l =
                    null ===
                      (i = null === e || void 0 === e ? void 0 : e.target) ||
                    void 0 === i
                      ? void 0
                      : i.clientHeight) && void 0 !== l
                  ? l
                  : 0;
            a === c || (a + s >= c && b && b()),
              h && h(),
              y({
                type: 'changeTrs',
                curScrollTop: a,
                scrollHeight: c,
                tableScrollY: E,
              });
          }, throttleTime),
          r =
            null === (e = null === m || void 0 === m ? void 0 : m.current) ||
            void 0 === e
              ? void 0
              : e.parentNode;
        return (
          r && r.addEventListener('scroll', t),
          function () {
            r.removeEventListener('scroll', t);
          }
        );
      },
      [m, E, h, b],
    ),
    React__default.createElement(
      'div',
      {
        className: 'virtuallist',
        ref: m,
        style: {
          width: '100%',
          position: 'relative',
          height: j,
          boxSizing: 'border-box',
          paddingTop: _.curScrollTop,
        },
      },
      React__default.createElement(
        ScrollContext.Provider,
        {
          value: {
            dispatch: y,
            rowHeight: _.rowHeight,
            start: H,
            offsetStart: L,
            renderLen: x,
            totalLen: newR,
            vid: g,
          },
        },
        React__default.createElement(
          'table',
          __assign({}, u, {
            ref: S,
            style: __assign(__assign({}, v), {
              width: d,
              position: 'relative',
              transform: 'translateY(-' + L + 'px)',
            }),
          }),
          s,
        ),
      ),
    )
  );
}
export function VList(e) {
  var t,
    r,
    o,
    n = null !== (t = e.vid) && void 0 !== t ? t : DEFAULT_VID;
  return (
    vidMap.has(n)
      ? (o =
          null === (r = vidMap.get(n)) || void 0 === r ? void 0 : r.components)
      : ((o = transformTable({
          vid: n,
          scrollY: e.height,
          reachEnd: e.onReachEnd,
          onScroll: e.onScroll,
        })),
        vidMap.set(n, {
          components: o,
          rowKey: e.rowKey,
        })),
    {
      table: o,
      body: {
        wrapper: VWrapper,
        row: VRow,
        cell: VCell,
      },
    }
  );
}
export function scrollTo(e) {
  var t = e.row,
    r = e.y,
    o = e.vid,
    n = void 0 === o ? DEFAULT_VID : o,
    i = vidMap.get(n),
    l = i.scrollNode,
    a = i.rowItemHeight;
  l.scrollTop = t
    ? t - 1 > 0
      ? (t - 1) * (null !== a && void 0 !== a ? a : 0)
      : 0
    : null !== r && void 0 !== r
    ? r
    : 0;
}
Object.defineProperty(exports, '__esModule', {
  value: !0,
});
var React__default = _interopDefault(React),
  __assign = function () {
    return (__assign =
      Object.assign ||
      function (e) {
        for (var t, r = 1, o = arguments.length; r < o; r++) {
          t = arguments[r];
          for (var n in t)
            Object.prototype.hasOwnProperty.call(t, n) && (e[n] = t[n]);
        }
        return e;
      }).apply(this, arguments);
  },
  freeGlobal =
    'object' == typeof global && global && global.Object === Object && global,
  freeSelf = 'object' == typeof self && self && self.Object === Object && self,
  root = freeGlobal || freeSelf || Function('return this')(),
  Symbol = root.Symbol,
  objectProto = Object.prototype,
  hasOwnProperty = objectProto.hasOwnProperty,
  nativeObjectToString = objectProto.toString,
  symToStringTag = Symbol ? Symbol.toStringTag : void 0,
  objectProto$1 = Object.prototype,
  nativeObjectToString$1 = objectProto$1.toString,
  nullTag = '[object Null]',
  undefinedTag = '[object Undefined]',
  symToStringTag$1 = Symbol ? Symbol.toStringTag : void 0,
  symbolTag = '[object Symbol]',
  reWhitespace = /\s/,
  reTrimStart = /^\s+/,
  NAN = NaN,
  reIsBadHex = /^[-+]0x[0-9a-f]+$/i,
  reIsBinary = /^0b[01]+$/i,
  reIsOctal = /^0o[0-7]+$/i,
  freeParseInt = parseInt,
  now = function () {
    return root.Date.now();
  },
  FUNC_ERROR_TEXT = 'Expected a function',
  nativeMax = Math.max,
  nativeMin = Math.min,
  numberTag = '[object Number]',
  FUNC_ERROR_TEXT$1 = 'Expected a function',
  css_248z =
    '.virtuallist .ant-table-tbody>tr>td>div{-webkit-box-sizing:border-box;box-sizing:border-box;white-space:nowrap;vertical-align:middle;overflow:hidden;text-overflow:ellipsis;width:100%}.virtuallist .ant-table-tbody>tr>td.ant-table-row-expand-icon-cell>div{overflow:inherit}.ant-table-bordered .virtuallist>table>.ant-table-tbody>tr>td{border-right:1px solid #f0f0f0}';
styleInject(css_248z);
var initialState = {
    rowHeight: 0,
    curScrollTop: 0,
    scrollHeight: 0,
    tableScrollY: 0,
    totalLen: 0,
  },
  DEFAULT_VID = 'vtable',
  vidMap = new Map(),
  ScrollContext = React.createContext({
    dispatch: void 0,
    renderLen: 1,
    start: 0,
    offsetStart: 0,
    rowHeight: initialState.rowHeight,
    totalLen: 0,
    vid: DEFAULT_VID,
  }),
  transformTable = function (e) {
    return function (t) {
      return VTable(t, e);
    };
  };
