"use strict";

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

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

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

var _styledComponents = _interopRequireDefault(require("styled-components"));

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

var _styles = require("@vincent_za/styles");

var _utils = require("@vincent_za/utils");

var _container = _interopRequireDefault(require("../TooltipWrap/container"));

var _constants = require("./constants");

var _component = _interopRequireDefault(require("./Modal/component"));

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

function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(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); return Constructor; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }

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

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

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 } }); 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 _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 _templateObject3() {
  var data = _taggedTemplateLiteral(["\n  outline: none;\n"]);

  _templateObject3 = function _templateObject3() {
    return data;
  };

  return data;
}

function _templateObject2() {
  var data = _taggedTemplateLiteral(["\n  display: ", ";\n  padding: ", " 0;\n\n  & + ", " {\n    padding-bottom: 0;\n  }\n"]);

  _templateObject2 = function _templateObject2() {
    return data;
  };

  return data;
}

function _templateObject() {
  var data = _taggedTemplateLiteral(["\n  display: flex;\n  margin-top: ", ";\n"]);

  _templateObject = function _templateObject() {
    return data;
  };

  return data;
}

function _taggedTemplateLiteral(strings, raw) { if (!raw) { raw = strings.slice(0); } return Object.freeze(Object.defineProperties(strings, { raw: { value: Object.freeze(raw) } })); }

var ModalContentFooter = _styledComponents.default.footer(_templateObject(), _styles.REM_UNIT_LG);

exports.ModalContentFooter = ModalContentFooter;

var ModalContentTitle = _styledComponents.default.div(_templateObject2(), function (props) {
  return props.isHidden ? 'none' : 'flex';
}, _styles.REM_UNIT_MD, ModalContentFooter);

exports.ModalContentTitle = ModalContentTitle;

var TriggerTemplateWrap = _styledComponents.default.div(_templateObject3());

var ModalWrap =
/*#__PURE__*/
function (_React$Component) {
  _inherits(ModalWrap, _React$Component);

  function ModalWrap(props) {
    var _this;

    _classCallCheck(this, ModalWrap);

    _this = _possibleConstructorReturn(this, _getPrototypeOf(ModalWrap).call(this, props));

    _defineProperty(_assertThisInitialized(_this), "onResize", function () {
      return _this.updateCoordinates();
    });

    _defineProperty(_assertThisInitialized(_this), "onClick", function (event) {
      var _this$props = _this.props,
          hasOverlay = _this$props.hasOverlay,
          onOverlayToggle = _this$props.onOverlayToggle,
          id = _this$props.id;
      onOverlayToggle({
        event: event,
        id: id,
        hasOverlay: hasOverlay,
        showOverlay: true
      });
    });

    _defineProperty(_assertThisInitialized(_this), "updateCoordinates", function () {
      var isRootEl = _this.props.isRootEl;

      var clientRect = _this.modalRef.getBoundingClientRect();

      return _this.setState({
        position: {
          xPos: isRootEl ? clientRect.x + _this.props.offset.x : 0,
          yPos: isRootEl ? clientRect.y + _this.props.offset.y : 0
        }
      });
    });

    _defineProperty(_assertThisInitialized(_this), "addModalRef", function (el) {
      _this.modalRef = el;
    });

    _this.state = {
      position: {
        xPos: 0,
        yPos: 0
      }
    };
    _this.throttledResize = (0, _utils.throttle)(100)(_this.onResize);
    return _this;
  }

  _createClass(ModalWrap, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      this.updateCoordinates();
      window.addEventListener('resize', this.throttledResize);
    }
  }, {
    key: "shouldComponentUpdate",
    value: function shouldComponentUpdate(nextProps, nextState) {
      if (nextState.position !== this.state.position) return true;
      if (nextProps.activeModalIds !== this.props.activeModalIds) return true;
      if (nextProps.activeOverlayModalIds !== this.props.activeOverlayModalIds) return true;
      if (nextProps.children !== this.props.children) return true;
      if (nextProps.customStyles !== this.props.customStyles) return true;
      if (nextProps.id !== this.props.id) return true;
      if (nextProps.offset !== this.props.offset) return true;
      if (nextProps.title !== this.props.title) return true;
      if (nextProps.asyncStatus !== this.props.asyncStatus) return true;
      return false;
    }
  }, {
    key: "componentWillUnmount",
    value: function componentWillUnmount() {
      window.removeEventListener('resize', this.throttledResize);
    }
  }, {
    key: "render",
    value: function render() {
      var _this$props2 = this.props,
          isTouchDevice = _this$props2.isTouchDevice,
          activeModalIds = _this$props2.activeModalIds,
          activeOverlayModalIds = _this$props2.activeOverlayModalIds,
          children = _this$props2.children,
          className = _this$props2.className,
          customStyles = _this$props2.customStyles,
          grid = _this$props2.grid,
          id = _this$props2.id,
          isDraggable = _this$props2.isDraggable,
          isResizable = _this$props2.isResizable,
          isCentered = _this$props2.isCentered,
          isFixed = _this$props2.isFixed,
          hasOverlay = _this$props2.hasOverlay,
          isRightAligned = _this$props2.isRightAligned,
          isNoContentSpacing = _this$props2.isNoContentSpacing,
          isNoContentHorizontalSpacing = _this$props2.isNoContentHorizontalSpacing,
          width = _this$props2.width,
          height = _this$props2.height,
          onOverlayToggle = _this$props2.onOverlayToggle,
          portalSelector = _this$props2.portalSelector,
          title = _this$props2.title,
          tooltipLabel = _this$props2.tooltipLabel,
          tooltipPosition = _this$props2.tooltipPosition,
          triggerTemplate = _this$props2.triggerTemplate;
      var isModalActive = activeOverlayModalIds.includes(id) || activeModalIds.includes(id);
      return _react.default.createElement("div", {
        className: className
      }, !tooltipLabel ? _react.default.createElement(TriggerTemplateWrap, {
        className: (0, _classnames.default)(_defineProperty({}, _constants.MODAL_CLASSNAME_TRIGGER_TEMPLATE_ACTIVE, isModalActive)),
        onClick: this.onClick,
        ref: this.addModalRef
      }, triggerTemplate) : _react.default.createElement(_container.default, {
        isDisabled: isModalActive || !tooltipLabel,
        position: tooltipPosition,
        tooltip: tooltipLabel
      }, _react.default.createElement(TriggerTemplateWrap, {
        className: (0, _classnames.default)(_defineProperty({}, _constants.MODAL_CLASSNAME_TRIGGER_TEMPLATE_ACTIVE, isModalActive)),
        onClick: this.onClick,
        ref: this.addModalRef
      }, triggerTemplate)), _react.default.createElement(_component.default, _extends({
        isTouchDevice: isTouchDevice,
        isModalActive: isModalActive,
        isCentered: isCentered,
        activeModalIds: activeModalIds,
        activeOverlayModalIds: activeOverlayModalIds,
        customStyles: customStyles,
        grid: grid,
        id: id,
        isDraggable: isDraggable,
        isResizable: isResizable,
        isFixed: isFixed,
        hasOverlay: hasOverlay,
        isRightAligned: isRightAligned,
        isNoContentSpacing: isNoContentSpacing,
        isNoContentHorizontalSpacing: isNoContentHorizontalSpacing,
        width: width,
        height: height,
        onOverlayToggle: onOverlayToggle,
        portalSelector: portalSelector,
        title: title
      }, this.state), children));
    }
  }]);

  return ModalWrap;
}(_react.default.Component);

ModalWrap.propTypes = {
  isTouchDevice: _propTypes.bool.isRequired,
  isRightAligned: _propTypes.bool,
  isCentered: _propTypes.bool,
  isDraggable: _propTypes.bool,
  isResizable: _propTypes.bool,
  isNoContentSpacing: _propTypes.bool,
  isNoContentHorizontalSpacing: _propTypes.bool,
  isRootEl: _propTypes.bool,
  isFixed: _propTypes.bool,
  hasOverlay: _propTypes.bool,
  width: _propTypes.number,
  height: _propTypes.number,
  title: _propTypes.string.isRequired,
  activeOverlayModalIds: (0, _propTypes.arrayOf)(_propTypes.string).isRequired,
  // offset: shape(PROP_OFFSET),
  activeModalIds: (0, _propTypes.arrayOf)(_propTypes.string),
  triggerTemplate: _propTypes.node.isRequired,
  tooltipLabel: _propTypes.string,
  tooltipPosition: _propTypes.string,
  id: _propTypes.string.isRequired,
  grid: (0, _propTypes.arrayOf)(_propTypes.number),
  onOverlayToggle: _propTypes.func.isRequired,
  className: _propTypes.string,
  portalSelector: _propTypes.string,
  customStyles: _propTypes.string,
  children: _propTypes.node.isRequired,
  asyncStatus: (0, _propTypes.shape)(_utils.PROP_ASYNC_STATUS)
};
ModalWrap.defaultProps = {
  isRightAligned: false,
  isCentered: false,
  isDraggable: false,
  isResizable: false,
  hasOverlay: false,
  isRootEl: false,
  isFixed: false,
  isNoContentSpacing: false,
  isNoContentHorizontalSpacing: false,
  width: undefined,
  height: undefined,
  offset: {
    x: 0,
    y: 0
  },
  activeModalIds: undefined,
  grid: undefined,
  tooltipLabel: '',
  tooltipPosition: 'right',
  portalSelector: undefined,
  customStyles: undefined,
  className: undefined,
  asyncStatus: _utils.SHAPE_ASYNC_STATUS_INITIAL
};
var _default = ModalWrap;
exports.default = _default;