'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.TourStep = undefined;

var _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; };

var _createClass = 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); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _assign = require('core-js/library/fn/object/assign');

var _assign2 = _interopRequireDefault(_assign);

var _elementHelper = require('kitten/helpers/dom/element-helper');

var _tourPopover = require('kitten/components/tours/tour-popover');

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

function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }

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

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

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

var TourStep = exports.TourStep = function (_React$Component) {
  _inherits(TourStep, _React$Component);

  function TourStep(props) {
    _classCallCheck(this, TourStep);

    var _this = _possibleConstructorReturn(this, (TourStep.__proto__ || Object.getPrototypeOf(TourStep)).call(this, props));

    _this.state = {
      targetHighlightStyles: null,
      currentPositionTarget: _this.props.targetElement,
      popoverComputedStyles: null
    };

    _this.handleResize = _this.handleResize.bind(_this);
    _this.handleTargetHighlightPosition = _this.handleTargetHighlightPosition.bind(_this);
    _this.handlePopoverPosition = _this.handlePopoverPosition.bind(_this);
    return _this;
  }

  // Component lifecyle.

  _createClass(TourStep, [{
    key: 'componentDidMount',
    value: function componentDidMount() {
      this.positionTargetHighlight();
      window.addEventListener('resize', this.handleResize);
    }
  }, {
    key: 'componentDidUpdate',
    value: function componentDidUpdate() {
      if (!this.isTargetHighlightPositionUpdated()) {
        this.positionTargetHighlight();
      }
    }
  }, {
    key: 'componentWillUnmount',
    value: function componentWillUnmount() {
      window.removeEventListener('resize', this.handleResize);
    }

    // Component listener callbacks.

  }, {
    key: 'handleResize',
    value: function handleResize() {
      this.positionTargetHighlight();
    }
  }, {
    key: 'handleTargetHighlightPosition',
    value: function handleTargetHighlightPosition() {
      this.props.onTargetHighlightPosition(this.props);
    }
  }, {
    key: 'handlePopoverPosition',
    value: function handlePopoverPosition(popover) {
      this.positionPopover(popover);
    }

    // Component methods.

  }, {
    key: 'getTargetHighlightPositionStyles',
    value: function getTargetHighlightPositionStyles() {
      var target = document.querySelector(this.props.targetElement);
      var targetStyles = target.getBoundingClientRect();
      var targetHeight = targetStyles.height;
      var targetWidth = targetStyles.width;
      var targetTop = targetStyles.top;
      var targetLeft = targetStyles.left;

      var highlight = this.refs.targetHighlight;
      var highlightStyles = highlight.getBoundingClientRect();
      var highlightHeight = highlightStyles.height;
      var highlightWidth = highlightStyles.width;
      var highlightTop = targetTop + targetHeight / 2 - highlightHeight / 2;
      var highlightLeft = targetLeft + targetWidth / 2 - highlightWidth / 2;

      return {
        top: highlightTop + 'px',
        left: highlightLeft + 'px'
      };
    }
  }, {
    key: 'positionTargetHighlight',
    value: function positionTargetHighlight() {
      if (_elementHelper.domElementHelper.canUseDom()) {
        this.handleTargetHighlightPosition();

        this.setState({
          targetHighlightStyles: this.getTargetHighlightPositionStyles(),
          currentPositionTarget: this.props.targetElement
        });
      }
    }
  }, {
    key: 'positionPopover',
    value: function positionPopover(popover) {
      this.setState({
        popoverComputedStyles: this.getPopoverPositionStyles(popover, this.props.popoverPosition)
      });
    }
  }, {
    key: 'getPopoverPositionStyles',
    value: function getPopoverPositionStyles(popover, position) {
      if (!position) return;

      var target = document.querySelector(this.props.targetElement);

      if (position == 'right') {
        var left = _elementHelper.domElementHelper.getComputedLeft(target) - _elementHelper.domElementHelper.getComputedWidth(popover) + _elementHelper.domElementHelper.getComputedWidth(target);

        return { left: left };
      }

      return { left: _elementHelper.domElementHelper.getComputedLeft(target) };
    }
  }, {
    key: 'isTargetHighlightPositionUpdated',
    value: function isTargetHighlightPositionUpdated() {
      return this.state.currentPositionTarget == this.props.targetElement;
    }
  }, {
    key: 'renderTargetHighlight',
    value: function renderTargetHighlight() {
      return _react2.default.createElement(
        'div',
        {
          ref: 'targetHighlight',
          className: 'k-Tour__step__targetHighlight',
          style: this.state.targetHighlightStyles
        },
        _react2.default.createElement('span', { className: 'k-Tour__step__targetHighlightContent' })
      );
    }
  }, {
    key: 'render',
    value: function render() {
      var _props = this.props,
          targetElement = _props.targetElement,
          popoverProps = _objectWithoutProperties(_props, ['targetElement']);

      return _react2.default.createElement(
        'div',
        { className: 'k-Tour__step' },
        this.renderTargetHighlight(),
        _react2.default.createElement(_tourPopover.TourPopover, _extends({
          ref: 'popover',
          onPopoverPosition: this.handlePopoverPosition,
          style: this.state.popoverComputedStyles
        }, popoverProps))
      );
    }
  }]);

  return TourStep;
}(_react2.default.Component);

TourStep.defaultProps = {
  position: null // 'left' | 'right'


  // DEPRECATED: do not use default export.
};exports.default = TourStep;