"use strict";

exports.__esModule = true;
exports["default"] = void 0;
var _react = _interopRequireWildcard(require("react"));
var _usePrevious = _interopRequireDefault(require("../../hooks/usePrevious"));
var _flex = _interopRequireDefault(require("../templates/flex"));
var _useIntersection2 = _interopRequireDefault(require("../../hooks/useIntersection"));
var _setRef = _interopRequireDefault(require("../../mixins/set-ref"));
var _excluded = ["height", "width", "fallback", "root", "rootMargin", "threshold", "onVisibility", "children", "ref"];
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { "default": e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n["default"] = e, t && t.set(e, n), n; }
function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; }
var getContent = function getContent(value) {
  return typeof value === "function" ? value() : value;
};
var Intersection = function Intersection(_ref) {
  var _ref$height = _ref.height,
    height = _ref$height === void 0 ? "100%" : _ref$height,
    _ref$width = _ref.width,
    width = _ref$width === void 0 ? "100%" : _ref$width,
    _ref$fallback = _ref.fallback,
    fallback = _ref$fallback === void 0 ? null : _ref$fallback,
    root = _ref.root,
    _ref$rootMargin = _ref.rootMargin,
    rootMargin = _ref$rootMargin === void 0 ? "0px" : _ref$rootMargin,
    _ref$threshold = _ref.threshold,
    threshold = _ref$threshold === void 0 ? 0 : _ref$threshold,
    onVisibility = _ref.onVisibility,
    children = _ref.children,
    parentRef = _ref.ref,
    rest = _objectWithoutPropertiesLoose(_ref, _excluded);
  var _useIntersection = (0, _useIntersection2["default"])({
      root: root,
      rootMargin: rootMargin,
      threshold: threshold,
      onVisibility: onVisibility
    }),
    setRef = _useIntersection[0],
    ref = _useIntersection[1],
    visible = _useIntersection[2];
  var prevVisible = (0, _usePrevious["default"])(visible);
  var lastHeightRef = (0, _react.useRef)(height);
  if (visible !== prevVisible && !visible && ref.current) {
    lastHeightRef.current = ref.current.clientHeight + "px";
  }
  return /*#__PURE__*/_react["default"].createElement(_flex["default"], _extends({
    ref: function ref(element) {
      setRef(element);
      (0, _setRef["default"])(parentRef, element);
    },
    width: width,
    height: visible ? height : {
      min: lastHeightRef.current
    }
  }, rest), getContent(visible ? children : fallback));
};
var _default = exports["default"] = Intersection;