"use strict";

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); }

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

var _react = _interopRequireWildcard(require("react"));

var _reactDom = _interopRequireDefault(require("react-dom"));

var _classnames = _interopRequireDefault(require("classnames"));

var _simplebar = _interopRequireDefault(require("../../../scrollbar/simplebar/simplebar"));

var _menuCard = _interopRequireDefault(require("../menu-card"));

var _flipBackHeader = _interopRequireDefault(require("./flip-back-header"));

var _menuCloseBtn = _interopRequireDefault(require("../menu-close-btn"));

var _dragResize = _interopRequireDefault(require("../drag-resize"));

var _precls = _interopRequireDefault(require("./precls"));

require("./index.css");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }

function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }

function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

var _default = /*#__PURE__*/(0, _react.forwardRef)(function (props, ref) {
  var _classNames, _classNames2, _classNames3;

  var children = props.children,
      defaultWidth = props.defaultWidth,
      collapseMenu = props.collapseMenu,
      _props$onChange = props.onChange,
      onChange = _props$onChange === void 0 ? function () {} : _props$onChange;

  var _useState = (0, _react.useState)(false),
      _useState2 = _slicedToArray(_useState, 2),
      isBack = _useState2[0],
      setIsBack = _useState2[1];

  var _useState3 = (0, _react.useState)(false),
      _useState4 = _slicedToArray(_useState3, 2),
      isFloat = _useState4[0],
      setIsFloat = _useState4[1];

  var _useState5 = (0, _react.useState)(false),
      _useState6 = _slicedToArray(_useState5, 2),
      isFloatHide = _useState6[0],
      setIsFloatHide = _useState6[1];

  var _useState7 = (0, _react.useState)(false),
      _useState8 = _slicedToArray(_useState7, 2),
      isHideTopRightIcon = _useState8[0],
      setIsHideTopRightIcon = _useState8[1];

  var _useState9 = (0, _react.useState)(true),
      _useState10 = _slicedToArray(_useState9, 2),
      showMore = _useState10[0],
      setShowMore = _useState10[1];

  var _useState11 = (0, _react.useState)(true),
      _useState12 = _slicedToArray(_useState11, 2),
      showFrontMore = _useState12[0],
      setShowFrontMore = _useState12[1];

  var hideFloatRef = (0, _react.useRef)();
  var flipCardRef = (0, _react.useRef)();
  var flipFrontRef = (0, _react.useRef)();
  var flipBackRef = (0, _react.useRef)();

  var goBackHandler = function goBackHandler() {
    if (!isBack) return;
    setMenuHideIcon(true);
    flipBackRef.current.style.position = "absolute";
    flipFrontRef.current.style.display = "block";
    setTimeout(function () {
      flipFrontRef.current.style.position = "relative";
      flipBackRef.current.style.display = "none";
      setMenuHideIcon(false);
    }, 600);
    setIsBack(false);
    onChange(false);
  };

  var nextHandler = function nextHandler() {
    if (isBack) return;
    setMenuHideIcon(true);
    flipFrontRef.current.style.position = "absolute";
    flipBackRef.current.style.display = "block";
    setTimeout(function () {
      flipBackRef.current.style.position = "relative";
      flipFrontRef.current.style.display = "none";
      setMenuHideIcon(false);
    }, 600);
    setIsBack(true);
    onChange(true);
  };

  var setMenuHideIcon = function setMenuHideIcon(isHide) {
    if (isHide === false && isFloat) return; // 如果是浮动状态则不能显示隐藏按钮

    setIsHideTopRightIcon(isHide);
  };

  (0, _react.useEffect)(function () {
    setMenuHideIcon(isFloat);
  }, [isFloat]);
  (0, _react.useImperativeHandle)(ref, function () {
    return {
      next: nextHandler,
      goback: goBackHandler
    };
  });

  var onMenuHideHandler = function onMenuHideHandler() {
    collapseMenu(!isFloat); // 最外层菜单with=0

    setIsFloat(!isFloat); // 浮动菜单，设置阴影

    setTimeout(function () {
      // 1秒后隐藏菜单到左侧
      setIsFloatHide(!isFloatHide);
    }, !isFloat ? 400 : 0);
  };

  var removeCloseFloat = function removeCloseFloat() {
    clearTimeout(hideFloatRef.current);
  }; // 隐藏浮动的菜单（通用）


  var hideFlipCard = function hideFlipCard(e) {
    if (!isFloat) return; // 非浮动状态下不隐藏

    removeCloseFloat();
    hideFloatRef.current = setTimeout(function () {
      setIsFloatHide(true);
    }, 400);
  };

  var onFixedHideIconMouseEnter = function onFixedHideIconMouseEnter(e) {
    e.stopPropagation();
    removeCloseFloat();
    setIsFloatHide(false);
  };

  var onFixedHideIconMouseLeave = function onFixedHideIconMouseLeave(e) {
    e.stopPropagation();
    hideFlipCard(e);
  };

  var onFixedHideIconClickHandler = function onFixedHideIconClickHandler(e) {
    e.stopPropagation();
    collapseMenu(false);
    setIsFloat(false);
    setIsFloatHide(false);
  };

  var onFlipMouseEnterHandler = function onFlipMouseEnterHandler(e) {
    e.stopPropagation();
    removeCloseFloat();
  };

  var onFlipMouseLeaveHandler = function onFlipMouseLeaveHandler(e) {
    e.stopPropagation();

    if (e.currentTarget.className.indexOf("titaui-flip-card--is-float-hide") != -1) {
      return;
    }

    hideFlipCard(e);
  };

  (0, _react.useEffect)(function () {
    var targetNode = document.querySelector(".".concat(_precls["default"], "__inner-back-content"));
    var config = {
      attributes: true,
      childList: true,
      subtree: true
    };

    var callback = function callback(_mutationsList, _observer) {
      var scrollArea = _simplebar["default"].instances.get(document.querySelector(".".concat(_precls["default"], "__inner-back [data-simplebar]")));

      var el = scrollArea === null || scrollArea === void 0 ? void 0 : scrollArea.getScrollElement();

      if (el && el.scrollHeight > el.offsetHeight) {
        if (el.scrollHeight - el.offsetHeight == el.scrollTop) {
          setShowMore(false);
        } else {
          setShowMore(true);
        }
      } else {
        setShowMore(false);
      }

      el === null || el === void 0 ? void 0 : el.addEventListener("scroll", function () {
        if (el.scrollHeight - el.offsetHeight == el.scrollTop) {
          setShowMore(false);
        } else {
          setShowMore(true);
        }
      });
    };

    var observer = new MutationObserver(callback);
    targetNode && observer.observe(targetNode, config);
    return function () {
      observer.disconnect();
    };
  }, [isBack]);
  (0, _react.useEffect)(function () {
    if (isBack) return;
    var targetNode = document.querySelector(".".concat(_precls["default"], "__inner-front-content"));
    var config = {
      attributes: true,
      childList: true,
      subtree: true
    };

    var callback = function callback(_mutationsList, _observer) {
      var scrollArea = _simplebar["default"].instances.get(document.querySelector(".".concat(_precls["default"], "__inner-front [data-simplebar]")));

      var el = scrollArea === null || scrollArea === void 0 ? void 0 : scrollArea.getScrollElement();

      if (el && el.scrollHeight > el.offsetHeight) {
        if (el.scrollHeight - el.offsetHeight == el.scrollTop) {
          setShowFrontMore(false);
        } else {
          setShowFrontMore(true);
        }
      } else {
        setShowFrontMore(false);
      }

      el === null || el === void 0 ? void 0 : el.addEventListener("scroll", function () {
        if (el.scrollHeight - el.offsetHeight == el.scrollTop) {
          setShowFrontMore(false);
        } else {
          setShowFrontMore(true);
        }
      });
    };

    var observer = new MutationObserver(callback);
    targetNode && observer.observe(targetNode, config);
    return function () {
      observer.disconnect();
    };
  }, [isBack]);

  var onClickShowMore = function onClickShowMore() {
    var _instance;

    var instance = null;

    if (isBack) {
      instance = _simplebar["default"].instances.get(document.querySelector(".".concat(_precls["default"], "__inner-back [data-simplebar]")));
    } else {
      instance = _simplebar["default"].instances.get(document.querySelector(".".concat(_precls["default"], "__inner-front [data-simplebar]")));
    }

    var el = (_instance = instance) === null || _instance === void 0 ? void 0 : _instance.getScrollElement();

    if (el) {
      el.scrollTop += 150;
    }
  };

  return /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, /*#__PURE__*/_react["default"].createElement("div", {
    ref: flipCardRef,
    onMouseEnter: onFlipMouseEnterHandler,
    onMouseLeave: onFlipMouseLeaveHandler,
    className: (0, _classnames["default"])(_precls["default"], (_classNames = {}, _defineProperty(_classNames, "".concat(_precls["default"], "--is-back"), isBack), _defineProperty(_classNames, "".concat(_precls["default"], "--is-float"), isFloat), _defineProperty(_classNames, "".concat(_precls["default"], "--is-float-hide"), isFloatHide), _classNames))
  }, /*#__PURE__*/_react["default"].createElement(_dragResize["default"], {
    minWidth: defaultWidth - 10,
    maxWidth: 600,
    defaultWidth: defaultWidth
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: (0, _classnames["default"])("".concat(_precls["default"], "__inner"))
  }, /*#__PURE__*/_react["default"].createElement("div", {
    ref: flipFrontRef,
    className: (0, _classnames["default"])("".concat(_precls["default"], "__inner-front"))
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: (0, _classnames["default"])("".concat(_precls["default"], "__inner-front-content"), (_classNames2 = {}, _defineProperty(_classNames2, "".concat(_precls["default"], "__inner-front-content--is-float"), isFloat), _defineProperty(_classNames2, "".concat(_precls["default"], "__inner-front-content--is-float-hide"), isFloatHide), _classNames2))
  }, /*#__PURE__*/_react["default"].createElement(_menuCard["default"], {
    style: {
      padding: "28px 12px 28px 12px"
    }
  }, children[0])), showFrontMore && /*#__PURE__*/_react["default"].createElement("div", {
    className: "".concat(_precls["default"], "__has-more"),
    onClick: onClickShowMore
  })), /*#__PURE__*/_react["default"].createElement("div", {
    ref: flipBackRef,
    className: (0, _classnames["default"])("".concat(_precls["default"], "__inner-back"))
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: (0, _classnames["default"])("".concat(_precls["default"], "__inner-back-content"), (_classNames3 = {}, _defineProperty(_classNames3, "".concat(_precls["default"], "__inner-back-content--is-float"), isFloat), _defineProperty(_classNames3, "".concat(_precls["default"], "__inner-back-content--is-float-hide"), isFloatHide), _classNames3))
  }, /*#__PURE__*/_react["default"].createElement(_menuCard["default"], {
    style: {
      padding: "42px 12px 28px 12px"
    }
  }, /*#__PURE__*/_react["default"].createElement(_flipBackHeader["default"], {
    onClick: goBackHandler
  }), children[1])), showMore && /*#__PURE__*/_react["default"].createElement("div", {
    className: "".concat(_precls["default"], "__has-more"),
    onClick: onClickShowMore
  })))), !isHideTopRightIcon && /*#__PURE__*/_react["default"].createElement(_menuCloseBtn["default"], {
    className: (0, _classnames["default"])("".concat(_precls["default"], "__top-right-hide-icon")),
    isAutoShow: true,
    onClick: onMenuHideHandler
  })), isFloat && /*#__PURE__*/_reactDom["default"].createPortal( /*#__PURE__*/_react["default"].createElement("span", {
    className: (0, _classnames["default"])("".concat(_precls["default"], "__fixed-hide-icon"))
  }, /*#__PURE__*/_react["default"].createElement(_menuCloseBtn["default"], {
    isClose: true,
    isAutoShow: false,
    onClick: onFixedHideIconClickHandler,
    onMouseEnter: onFixedHideIconMouseEnter,
    onMouseLeave: onFixedHideIconMouseLeave
  })), document.body));
});

exports["default"] = _default;