"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.ScrollMask = void 0;
var _react = _interopRequireWildcard(require("react"));
var _classnames = _interopRequireDefault(require("classnames"));
var _web = require("@react-spring/web");
var _ahooks = require("ahooks");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
const classPrefix = `adm-scroll-mask`;
const ScrollMask = props => {
  const maskRef = (0, _react.useRef)(null);
  const [{
    leftMaskOpacity,
    rightMaskOpacity
  }, api] = (0, _web.useSpring)(() => ({
    leftMaskOpacity: 0,
    rightMaskOpacity: 0,
    config: {
      clamp: true
    }
  }));
  const {
    run: updateMask
  } = (0, _ahooks.useThrottleFn)((immediate = false) => {
    const mask = maskRef.current;
    if (!mask) return;
    const scrollEl = props.scrollTrackRef.current;
    if (!scrollEl) return;
    const scrollLeft = scrollEl.scrollLeft;
    const showLeftMask = scrollLeft > 0;
    const showRightMask = scrollLeft + scrollEl.offsetWidth < scrollEl.scrollWidth;
    api.start({
      leftMaskOpacity: showLeftMask ? 1 : 0,
      rightMaskOpacity: showRightMask ? 1 : 0,
      immediate
    });
  }, {
    wait: 100,
    trailing: true,
    leading: true
  });
  (0, _react.useEffect)(() => {
    updateMask(true);
  }, []);
  (0, _react.useEffect)(() => {
    const scrollEl = props.scrollTrackRef.current;
    if (!scrollEl) return;
    scrollEl.addEventListener('scroll', updateMask);
    return () => scrollEl.removeEventListener('scroll', updateMask);
  }, []);
  return _react.default.createElement(_react.default.Fragment, null, _react.default.createElement(_web.animated.div, {
    ref: maskRef,
    className: (0, _classnames.default)(classPrefix, `${classPrefix}-left`),
    style: {
      opacity: leftMaskOpacity
    }
  }), _react.default.createElement(_web.animated.div, {
    className: (0, _classnames.default)(classPrefix, `${classPrefix}-right`),
    style: {
      opacity: rightMaskOpacity
    }
  }));
};
exports.ScrollMask = ScrollMask;