function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
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(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
import React, { forwardRef, useRef, useImperativeHandle, useEffect, useMemo } from 'react';
import classNames from 'classnames';
import "./PisellScrollView.less";
import { useScrollState, useScrollEvents, useIntersection } from "./hooks";
import ActionsContainer from "./components/Actions";

/** 默认滚动按钮配置（模块级常量，避免解构默认值每次 render 新建引用）。 */
var DEFAULT_SCROLL_ACTIONS_CONFIG = {
  show: true,
  type: 'rArrows'
};

/** 默认吸附滚动配置（模块级常量）。 */
var DEFAULT_SNAP_SCROLL = {
  enabled: true,
  type: 'mandatory'
};
var snapScrollPaddings = {
  rArrows: {
    start: 0,
    end: 117 + 10
  },
  lrArrows: {
    start: 47 + 10,
    end: 47 + 10
  },
  tbArrows: {
    start: 47 + 10,
    end: 47 + 10
  }
};
var PisellScrollView = /*#__PURE__*/forwardRef(function (props, ref) {
  var children = props.children,
    className = props.className,
    style = props.style,
    _props$overflow = props.overflow,
    overflow = _props$overflow === void 0 ? 'auto' : _props$overflow,
    _props$scrollbarStyle = props.scrollbarStyle,
    scrollbarStyle = _props$scrollbarStyle === void 0 ? 'default' : _props$scrollbarStyle,
    scrollbarClassName = props.scrollbarClassName,
    width = props.width,
    height = props.height,
    maxWidth = props.maxWidth,
    maxHeight = props.maxHeight,
    minWidth = props.minWidth,
    minHeight = props.minHeight,
    onScroll = props.onScroll,
    onScrollStart = props.onScrollStart,
    onScrollEnd = props.onScrollEnd,
    onReachTop = props.onReachTop,
    onReachBottom = props.onReachBottom,
    onReachLeft = props.onReachLeft,
    onReachRight = props.onReachRight,
    reachThreshold = props.reachThreshold,
    scrollEndDelay = props.scrollEndDelay,
    intersection = props.intersection,
    _props$scrollActionsC = props.scrollActionsConfig,
    scrollActionsConfig = _props$scrollActionsC === void 0 ? DEFAULT_SCROLL_ACTIONS_CONFIG : _props$scrollActionsC,
    _props$snapScroll = props.snapScroll,
    snapScroll = _props$snapScroll === void 0 ? DEFAULT_SNAP_SCROLL : _props$snapScroll,
    __designMode = props.__designMode;
  var containerRef = useRef(null);
  // 1. 状态管理 Hooks
  var _useScrollState = useScrollState({
      containerRef: containerRef,
      overflow: overflow
    }),
    scrollState = _useScrollState.scrollState,
    updateScrollState = _useScrollState.updateScrollState;

  // 2. 事件处理 Hooks
  var _useScrollEvents = useScrollEvents({
      containerRef: containerRef,
      onScroll: onScroll,
      onScrollStart: onScrollStart,
      onScrollEnd: onScrollEnd,
      onReachTop: onReachTop,
      onReachBottom: onReachBottom,
      onReachLeft: onReachLeft,
      onReachRight: onReachRight,
      reachThreshold: reachThreshold,
      scrollEndDelay: scrollEndDelay,
      updateScrollState: updateScrollState,
      scrollState: scrollState
    }),
    handleScroll = _useScrollEvents.handleScroll;
  useEffect(function () {
    document.body.id = 'body';
  }, []);

  // 3. 视图监听 Hooks
  useIntersection({
    containerRef: containerRef,
    config: intersection
  });
  var scrollTo = function scrollTo(position) {
    var container = containerRef.current;
    if (!container) return;
    var scrollTop = position.scrollTop,
      scrollLeft = position.scrollLeft,
      _position$behavior = position.behavior,
      behavior = _position$behavior === void 0 ? 'auto' : _position$behavior;
    var options = {
      behavior: behavior
    };
    if (scrollTop !== undefined) options.top = scrollTop;
    if (scrollLeft !== undefined) options.left = scrollLeft;
    container.scrollTo(options);
  };
  var scrollToElement = function scrollToElement(config) {
    var container = containerRef.current;
    if (!container) return;
    var targetId = config.targetId,
      _config$align = config.align,
      align = _config$align === void 0 ? 'start' : _config$align,
      _config$behavior = config.behavior,
      behavior = _config$behavior === void 0 ? 'auto' : _config$behavior,
      _config$offset = config.offset,
      offset = _config$offset === void 0 ? 0 : _config$offset;
    var target = container.querySelector("#".concat(targetId));
    if (!target) return;

    // 简单实现：使用 scrollIntoView 或者计算 offsetTop
    // scrollIntoView 不支持 offset，所以手动计算更灵活
    var containerRect = container.getBoundingClientRect();
    var targetRect = target.getBoundingClientRect();
    var top = container.scrollTop;
    var left = container.scrollLeft;

    // 垂直方向计算
    if (align === 'start') {
      top += targetRect.top - containerRect.top;
    } else if (align === 'center') {
      top += targetRect.top - containerRect.top - (container.clientHeight / 2 - targetRect.height / 2);
    } else if (align === 'end') {
      top += targetRect.bottom - containerRect.bottom;
    }

    // 加上偏移
    top += offset;
    container.scrollTo({
      top: top,
      left: left,
      behavior: behavior
    });
  };
  var scrollToPercentage = function scrollToPercentage(percentage) {
    var container = containerRef.current;
    if (!container) return;
    var percentX = percentage.percentX,
      percentY = percentage.percentY,
      _percentage$behavior = percentage.behavior,
      behavior = _percentage$behavior === void 0 ? 'auto' : _percentage$behavior;
    var maxScrollTop = container.scrollHeight - container.clientHeight;
    var maxScrollLeft = container.scrollWidth - container.clientWidth;
    var options = {
      behavior: behavior
    };
    if (percentY !== undefined) {
      options.top = percentY / 100 * maxScrollTop;
    }
    if (percentX !== undefined) {
      options.left = percentX / 100 * maxScrollLeft;
    }
    container.scrollTo(options);
  };
  var scrollToTop = function scrollToTop() {
    var _containerRef$current;
    var behavior = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'auto';
    (_containerRef$current = containerRef.current) === null || _containerRef$current === void 0 || _containerRef$current.scrollTo({
      top: 0,
      behavior: behavior
    });
  };
  var scrollToBottom = function scrollToBottom() {
    var behavior = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'auto';
    var container = containerRef.current;
    if (container) {
      container.scrollTo({
        top: container.scrollHeight,
        behavior: behavior
      });
    }
  };
  var scrollToLeft = function scrollToLeft() {
    var _containerRef$current2;
    var behavior = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'auto';
    (_containerRef$current2 = containerRef.current) === null || _containerRef$current2 === void 0 || _containerRef$current2.scrollTo({
      left: 0,
      behavior: behavior
    });
  };
  var scrollToRight = function scrollToRight() {
    var behavior = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'auto';
    var container = containerRef.current;
    if (container) {
      container.scrollTo({
        left: container.scrollWidth,
        behavior: behavior
      });
    }
  };
  var getScrollData = function getScrollData() {
    return scrollState;
  };
  var getScrollContainer = function getScrollContainer() {
    return containerRef.current;
  };

  // 4. Ref 方法实现
  useImperativeHandle(ref, function () {
    if (__designMode) {
      return containerRef.current;
    }
    return {
      scrollTo: scrollTo,
      scrollToElement: scrollToElement,
      scrollToPercentage: scrollToPercentage,
      scrollToTop: scrollToTop,
      scrollToBottom: scrollToBottom,
      scrollToLeft: scrollToLeft,
      scrollToRight: scrollToRight,
      getScrollData: getScrollData,
      getScrollContainer: getScrollContainer,
      // Snap Scroll 占位 (需要更复杂的计算逻辑，此处为基础版)
      scrollToSnapPoint: function scrollToSnapPoint(index) {
        var behavior = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'auto';
      } // 如果使用了 CSS snap，这里可能需要计算 snapPoints 或者直接控制 scrollTop
      // 暂留空或做简单实现
      ,
      getSnapPoints: function getSnapPoints() {
        return [];
      },
      getCurrentSnapIndex: function getCurrentSnapIndex() {
        return 0;
      },
      scrollToNextSnap: function scrollToNextSnap() {},
      scrollToPrevSnap: function scrollToPrevSnap() {}
    };
  });

  // 6. 滚动按钮逻辑
  var handleButtonClick = function handleButtonClick(direction) {
    var container = containerRef.current;
    if (!container) return;
    if ((scrollActionsConfig === null || scrollActionsConfig === void 0 ? void 0 : scrollActionsConfig.type) === 'indicator') {
      var indicatorTargets = {
        top: {
          top: 0
        },
        bottom: {
          top: container.scrollHeight
        },
        left: {
          left: 0
        },
        right: {
          left: container.scrollWidth
        }
      };
      container.scrollTo(_objectSpread(_objectSpread({}, indicatorTargets[direction]), {}, {
        behavior: 'smooth'
      }));
      return;
    }
    var distance = typeof (scrollActionsConfig === null || scrollActionsConfig === void 0 ? void 0 : scrollActionsConfig.scrollDistance) === 'number' ? scrollActionsConfig.scrollDistance : 300; // 默认 100px
    var top = container.scrollTop;
    var left = container.scrollLeft;
    if (direction === 'top') top -= distance;
    if (direction === 'bottom') top += distance;
    if (direction === 'left') left -= distance;
    if (direction === 'right') left += distance;
    container.scrollTo({
      top: top,
      left: left,
      behavior: 'smooth'
    });
  };

  // 计算容器样式
  var containerStyle = _objectSpread(_objectSpread({}, style), {}, {
    width: width,
    height: height,
    maxWidth: maxWidth,
    maxHeight: maxHeight,
    minWidth: minWidth,
    minHeight: minHeight
  });

  // 计算视口样式
  var viewportStyle = useMemo(function () {
    var _viewportStyle = {};
    if (overflow === 'x') {
      _viewportStyle.overflowX = 'auto';
    }
    if (overflow === 'y') {
      _viewportStyle.overflowY = 'auto';
    }
    if (overflow === 'both') {
      _viewportStyle.overflowX = 'auto';
      _viewportStyle.overflowY = 'auto';
    }
    if (overflow === 'hidden') {
      _viewportStyle.overflowX = 'hidden';
      _viewportStyle.overflowY = 'hidden';
    }
    if (snapScroll !== null && snapScroll !== void 0 && snapScroll.enabled && scrollActionsConfig !== null && scrollActionsConfig !== void 0 && scrollActionsConfig.show) {
      _viewportStyle.scrollSnapType = "".concat(overflow === 'x' ? 'x' : 'y', " ").concat(snapScroll.type);
      if (overflow === 'x') {
        var _snapScrollPaddings, _snapScrollPaddings2;
        _viewportStyle.scrollPaddingLeft = ((_snapScrollPaddings = snapScrollPaddings[scrollActionsConfig.type]) === null || _snapScrollPaddings === void 0 ? void 0 : _snapScrollPaddings.start) || 0;
        _viewportStyle.scrollPaddingRight = ((_snapScrollPaddings2 = snapScrollPaddings[scrollActionsConfig.type]) === null || _snapScrollPaddings2 === void 0 ? void 0 : _snapScrollPaddings2.end) || 0;
      }
      if (overflow === 'y') {
        var _snapScrollPaddings3, _snapScrollPaddings4;
        _viewportStyle.scrollPaddingTop = ((_snapScrollPaddings3 = snapScrollPaddings[scrollActionsConfig.type]) === null || _snapScrollPaddings3 === void 0 ? void 0 : _snapScrollPaddings3.start) || 0;
        _viewportStyle.scrollPaddingBottom = ((_snapScrollPaddings4 = snapScrollPaddings[scrollActionsConfig.type]) === null || _snapScrollPaddings4 === void 0 ? void 0 : _snapScrollPaddings4.end) || 0;
      }
    }
    return _viewportStyle;
  }, [overflow, scrollActionsConfig === null || scrollActionsConfig === void 0 ? void 0 : scrollActionsConfig.show, scrollActionsConfig === null || scrollActionsConfig === void 0 ? void 0 : scrollActionsConfig.type, snapScroll === null || snapScroll === void 0 ? void 0 : snapScroll.enabled, snapScroll === null || snapScroll === void 0 ? void 0 : snapScroll.type]);
  var viewportClasses = classNames('pisell-scroll-view-viewport', _defineProperty(_defineProperty({
    'pisell-scroll-view-viewport--hide-scrollbar': scrollbarStyle === 'none' || !scrollState.isOverflows
  }, "pisell-scroll-view-viewport--".concat(scrollbarStyle), scrollbarStyle !== 'default' && scrollbarStyle !== 'custom'), scrollbarClassName || '', scrollbarStyle === 'custom'));
  return /*#__PURE__*/React.createElement("div", {
    className: classNames('pisell-scroll-view', className),
    style: _objectSpread({}, containerStyle)
  }, /*#__PURE__*/React.createElement("div", {
    className: classNames(viewportClasses),
    style: _objectSpread({}, viewportStyle),
    ref: containerRef,
    "data-scrollbar-style": scrollbarStyle,
    onScroll: handleScroll
  }, children), /*#__PURE__*/React.createElement(ActionsContainer, {
    containerRef: containerRef,
    scrollState: scrollState,
    overflow: overflow,
    scrollActionsConfig: scrollActionsConfig,
    onClick: handleButtonClick
  }));
});
PisellScrollView.displayName = 'PisellScrollView';
export default PisellScrollView;