function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
var _excluded = ["children", "className", "render", "isVirtualized"];
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
import React, { useContext, useRef, useEffect } from 'react';
import classNames from 'classnames';
import { VariableSizeList } from 'react-window';
import AutoSizer from 'react-virtualized-auto-sizer';
import mergeRefs from 'react-merge-refs';
import { StyledRows } from './partials/StyledRows';
import { TimelineNoData } from './TimelineNoData';
import { TimelineContext } from '.';
export var TimelineRows = function TimelineRows(_ref) {
  var children = _ref.children,
    className = _ref.className,
    render = _ref.render,
    isVirtualized = _ref.isVirtualized,
    rest = _objectWithoutProperties(_ref, _excluded);
  var listRef = useRef(null);
  var rowHeights = useRef({});
  var count = React.Children.count(children);
  var hasChildren = count > 0;
  var mainClasses = classNames('timeline__main', className);
  var _useContext = useContext(TimelineContext),
    _useContext$state = _useContext.state,
    currentScaleOption = _useContext$state.currentScaleOption,
    days = _useContext$state.days;
  if (!currentScaleOption) {
    return null;
  }

  // @TODO: Move into a custom hook

  var getRowHeight = function getRowHeight(index) {
    var _rowHeights$current$i;
    return (_rowHeights$current$i = rowHeights.current[index]) !== null && _rowHeights$current$i !== void 0 ? _rowHeights$current$i : 64; // @TODO: Add sensible default
  };

  var setRowHeight = function setRowHeight(index, height) {
    var _listRef$current;
    listRef === null || listRef === void 0 ? void 0 : (_listRef$current = listRef.current) === null || _listRef$current === void 0 ? void 0 : _listRef$current.resetAfterIndex(0);
    rowHeights.current = _objectSpread(_objectSpread({}, rowHeights.current), {}, _defineProperty({}, index, height));
  };
  var Row = function Row(_ref2) {
    var style = _ref2.style,
      index = _ref2.index;
    var ref = useRef(null);
    var child = React.Children.toArray(children)[index];
    useEffect(function () {
      if (ref.current) {
        var _ref$current;
        setRowHeight(index, ref === null || ref === void 0 ? void 0 : (_ref$current = ref.current) === null || _ref$current === void 0 ? void 0 : _ref$current.clientHeight);
      }
    }, [index, ref]);
    return /*#__PURE__*/React.cloneElement(child, {
      style: style,
      ref: mergeRefs([ref]) // @TODO: Merge the forwardedRef
    });
  };

  return /*#__PURE__*/React.createElement(StyledRows, _extends({
    $hasDefaultStyles: !render,
    $width: currentScaleOption.widths.day * days.length,
    className: mainClasses,
    role: "rowgroup"
  }, rest), !hasChildren && /*#__PURE__*/React.createElement(TimelineNoData, null), hasChildren && !isVirtualized && children, hasChildren && isVirtualized && /*#__PURE__*/React.createElement(AutoSizer, null, function (_ref3) {
    var height = _ref3.height,
      width = _ref3.width;
    return /*#__PURE__*/React.createElement(VariableSizeList, {
      height: height,
      width: width,
      itemCount: count,
      itemSize: getRowHeight,
      ref: listRef
    }, Row);
  }));
};
TimelineRows.displayName = 'TimelineRows';