"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 = _interopRequireDefault(require("react"));

var _propTypes = _interopRequireDefault(require("prop-types"));

var _Layout = require("../Layout");

var _Tooltip = _interopRequireDefault(require("../Tooltip"));

require("../common/basic.module.css");

require("@zohodesk/variables/lib/colorVariables.module.css");

require("@zohodesk/variables/lib/dotVariables.module.css");

require("@zohodesk/variables/lib/sizeVariables.module.css");

require("@zohodesk/variables/lib/fontsizeVariables.module.css");

require("@zohodesk/variables/lib/fontFamilyVariables.module.css");

require("@zohodesk/variables/lib/transitionVariables.module.css");

require("@zohodesk/variables/lib/no_transitionVariables.module.css");

var _AppContainerModule = _interopRequireDefault(require("./AppContainer.module.css"));

var _Config = require("../Provider/Config");

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

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

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 AppContainer = /*#__PURE__*/function (_React$Component) {
  _inherits(AppContainer, _React$Component);

  var _super = _createSuper(AppContainer);

  function AppContainer(props) {
    var _this;

    _classCallCheck(this, AppContainer);

    _this = _super.call(this, props);
    _this.setTooltipRef = _this.setTooltipRef.bind(_assertThisInitialized(_this));
    _this.handleOver = _this.handleOver.bind(_assertThisInitialized(_this));
    _this.mouseOverDispatch = _this.mouseOverDispatch.bind(_assertThisInitialized(_this));
    _this.removeTimeout = _this.removeTimeout.bind(_assertThisInitialized(_this));
    _this.getContainerRef = _this.getContainerRef.bind(_assertThisInitialized(_this));
    _this.timer = null;
    _this.tooltipDebounce = (0, _Config.getLibraryConfig)('tooltipDebounce');
    return _this;
  }

  _createClass(AppContainer, [{
    key: "mouseOverDispatch",
    value: function mouseOverDispatch(e) {
      if (this.tooltipRef) {
        this.tooltipRef.handleOver(e);
      }
    }
  }, {
    key: "removeTimeout",
    value: function removeTimeout() {
      if (this.timer) {
        this.timer = clearTimeout(this.timer);
      }
    }
  }, {
    key: "handleOver",
    value: function handleOver(e) {
      var _this2 = this;

      if (this.timer) {
        this.timer = clearTimeout(this.timer);
      }

      this.timer = setTimeout(function () {
        return _this2.mouseOverDispatch(e);
      }, this.tooltipDebounce);
    }
  }, {
    key: "getContainerRef",
    value: function getContainerRef(ref) {
      var eleRef = this.props.eleRef;
      this.containerElement = ref;
      eleRef && eleRef(ref);
    }
  }, {
    key: "setTooltipRef",
    value: function setTooltipRef(ref) {
      this.tooltipRef = ref;
    }
  }, {
    key: "componentDidMount",
    value: function componentDidMount() {
      if (this.containerElement) {
        this.containerElement.addEventListener('mouseover', this.handleOver, false);
        this.containerElement.addEventListener('mouseout', this.removeTimeout, false);
      }
    }
  }, {
    key: "componentWillUnmount",
    value: function componentWillUnmount() {
      if (this.containerElement) {
        this.containerElement.removeEventListener('mouseover', this.handleOver, false);
        this.containerElement.addEventListener('mouseout', this.removeTimeout, false);
      }
    }
  }, {
    key: "render",
    value: function render() {
      var _this$props = this.props,
          className = _this$props.className,
          children = _this$props.children,
          tagName = _this$props.tagName,
          dataId = _this$props.dataId,
          tooltipClass = _this$props.tooltipClass,
          tooltipParentClass = _this$props.tooltipParentClass,
          customProps = _this$props.customProps;
      var _customProps$Containe = customProps.ContainerProps,
          ContainerProps = _customProps$Containe === void 0 ? {} : _customProps$Containe,
          _customProps$TooltipP = customProps.TooltipProps,
          TooltipProps = _customProps$TooltipP === void 0 ? {} : _customProps$TooltipP,
          _customProps$ExtraPro = customProps.ExtraProps,
          ExtraProps = _customProps$ExtraPro === void 0 ? {} : _customProps$ExtraPro;
      return /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, /*#__PURE__*/_react["default"].createElement(_Layout.Container, _extends({
        className: "".concat(_AppContainerModule["default"].container, " ").concat(className) //onMouseOver={this.handleOver}
        ,
        dataId: dataId,
        tagName: tagName,
        eleRef: this.getContainerRef
      }, ContainerProps, ExtraProps), /*#__PURE__*/_react["default"].createElement(_Layout.Box, {
        flexible: true
      }, children)), /*#__PURE__*/_react["default"].createElement("div", _extends({}, ExtraProps, {
        className: "".concat(_AppContainerModule["default"].container, " ").concat(_AppContainerModule["default"].tooltip, " ").concat(tooltipParentClass),
        "data-id": "".concat(dataId, "_tooltip")
      }), /*#__PURE__*/_react["default"].createElement(_Tooltip["default"], _extends({
        ref: this.setTooltipRef,
        customClass: tooltipClass
      }, TooltipProps))));
    }
  }]);

  return AppContainer;
}(_react["default"].Component);

exports["default"] = AppContainer;
AppContainer.propTypes = {
  children: _propTypes["default"].oneOfType([_propTypes["default"].element, _propTypes["default"].arrayOf(_propTypes["default"].element)]),
  dataId: _propTypes["default"].string,
  eleRef: _propTypes["default"].func,
  tagName: _propTypes["default"].string,
  className: _propTypes["default"].string,
  tooltipClass: _propTypes["default"].string,
  tooltipParentClass: _propTypes["default"].string,
  customProps: _propTypes["default"].shape({
    ContainerProps: _propTypes["default"].object,
    TooltipProps: _propTypes["default"].object,
    ExtraProps: _propTypes["default"].object
  })
};
AppContainer.defaultProps = {
  dataId: 'AppContainer',
  tagName: 'div',
  className: '',
  tooltipClass: '',
  tooltipParentClass: '',
  customProps: {}
};

if (false) {
  AppContainer.docs = {
    componentGroup: 'Template',
    folderName: 'Style Guide'
  };
}