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

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 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) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

var PopInner = /*#__PURE__*/function (_Component) {
  _inherits(PopInner, _Component);

  var _super = _createSuper(PopInner);

  function PopInner(props) {
    var _this;

    _classCallCheck(this, PopInner);

    _this = _super.call(this, props);

    _this.caculPosition = function () {
      if (!_this.props.bindNode) {
        throw new error("需要传入bindNode");
      }

      var position = {}; // container距视窗左上角的位置

      var containerOffset = _this.props.container.getBoundingClientRect(); // node距离视窗左上角的位置


      var nodeOffset = _this.props.bindNode.getBoundingClientRect(); // bindNode距container左上角的位置


      var nodeScrollOffset = {
        top: _this.props.container.scrollTop + nodeOffset.top - containerOffset.top,
        left: _this.props.container.scrollLeft + nodeOffset.left - containerOffset.left,
        bottom: _this.props.container.scrollTop + nodeOffset.top - containerOffset.top + nodeOffset.height,
        right: _this.props.container.scrollLeft + nodeOffset.left - containerOffset.left + nodeOffset.width
      }; // pop的宽高

      var popSize = _this.refs.wrapper.getBoundingClientRect();

      var positionType = _this.props.position.split(/\s+/); // 确定pop的位置需要pop的宽高 bindNode相对视窗左上角的位置 bindNode相对container左上角的位置
      // 其中bindNode相对container左上角的位置 = bindNode相对视窗左上角的位置 - container相对视窗左上角的位置 + container的滚动距离
      // 是否需要自动调整pop的位置


      var autoX = _this.props.autoAdjust == "x" || _this.props.autoAdjust == "x y";
      var autoY = _this.props.autoAdjust == "y" || _this.props.autoAdjust == "x y"; // 确定显示在bindNode的上方还是下方

      if (positionType[0] === "top") {
        // 优先出现在bindNode顶部 顶部空间不够则出现在底部
        if (!autoY || nodeOffset.top - (containerOffset.top < 0 ? 0 : containerOffset.top) >= popSize.height) {
          position.top = "".concat(nodeScrollOffset.top - popSize.height, "px");
        } else {
          position.top = "".concat(nodeScrollOffset.bottom, "px");
        }
      } else {
        // 优先出现在bindNode底部 底部空间不够则出现在顶部
        if (!autoY || containerOffset.bottom >= nodeOffset.bottom + popSize.height) {
          position.top = "".concat(nodeScrollOffset.bottom, "px");
        } else {
          position.top = "".concat(nodeScrollOffset.top - popSize.height, "px");
        }
      } // 确定横向的位置


      if (positionType[1] == "right") {
        // container中bindNode右侧的空间大于pop的宽度则将左边框对其bindNode的左边框
        if (!autoX || containerOffset.right - nodeOffset.right >= popSize.width) {
          position.left = "".concat(nodeScrollOffset.left, "px");
        } else {
          // 否则贴在右侧
          position.left = "".concat(containerOffset.right - containerOffset.left - popSize.width, "px");
        }
      } else if (positionType[1] == "left") {
        if (!autoX || nodeOffset.right - (containerOffset.left < 0 ? 0 : containerOffset.left) >= popSize.width) {
          position.left = "".concat(nodeScrollOffset.right - popSize.width, "px");
        } else {
          position.left = "".concat(_this.props.container.scrollLeft, "px");
        }
      } else {
        // 优先剧中
        if (!autoX || containerOffset.right - nodeOffset.right >= (popSize.width - nodeOffset.width) / 2) {
          position.left = "".concat(nodeScrollOffset.left - (popSize.width - nodeOffset.width) / 2, "px");
        } else {
          position.left = "".concat(containerOffset.right - containerOffset.left - popSize.width, "px");
        }

        if (autoX && nodeOffset.right - (containerOffset.left < 0 ? 0 : containerOffset.left) < (popSize.width + nodeOffset.width) / 2) {
          position.left = "".concat(_this.props.container.scrollLeft, "px");
        }
      }

      return position;
    };

    _this.state = {
      visibility: "hidden",
      position: {}
    };
    return _this;
  }

  _createClass(PopInner, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      this.setState({
        visibility: "visible",
        position: this.caculPosition()
      });
    }
  }, {
    key: "render",
    value: function render() {
      return /*#__PURE__*/_react["default"].createElement("div", {
        ref: "wrapper",
        style: _objectSpread({
          zIndex: 9999,
          visibility: this.state.visibility,
          position: "absolute"
        }, this.state.position)
      }, this.props.children);
    }
  }]);

  return PopInner;
}(_react.Component);

exports["default"] = PopInner;