'use strict';

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

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 _classnames = require('classnames');

var _classnames2 = _interopRequireDefault(_classnames);

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

var _tourStep = require('kitten/components/tours/tour-step');

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

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 Tour = exports.Tour = function (_Component) {
  _inherits(Tour, _Component);

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

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

    _this.state = {
      play: false,
      visible: false,
      currentIndex: 0,
      currentStep: _this.props.steps[0]
    };

    _this.handleClickOnNext = _this.handleClickOnNext.bind(_this);
    _this.handleClickOnPrev = _this.handleClickOnPrev.bind(_this);
    _this.handleClickOnClose = _this.handleClickOnClose.bind(_this);
    _this.handleTargetHighlightPosition = _this.handleTargetHighlightPosition.bind(_this);
    return _this;
  }

  // Component lifecycle.

  _createClass(Tour, [{
    key: 'componentDidMount',
    value: function componentDidMount() {
      if (this.shouldStart()) {
        this.start();
      }
    }

    // Component listener callbacks.

  }, {
    key: 'handleTargetHighlightPosition',
    value: function handleTargetHighlightPosition(step) {
      this.setState({ visible: true });
    }
  }, {
    key: 'handleClickOnNext',
    value: function handleClickOnNext() {
      var newIndex = this.state.currentIndex + 1;

      this.toggleStep(newIndex);

      this.dispatchEvent('k:tour:next');
    }
  }, {
    key: 'handleClickOnPrev',
    value: function handleClickOnPrev() {
      var newIndex = this.state.currentIndex - 1;

      this.toggleStep(newIndex);

      this.dispatchEvent('k:tour:prev');
    }
  }, {
    key: 'handleClickOnClose',
    value: function handleClickOnClose() {
      this.stop();

      this.dispatchEvent('k:tour:close');
    }
  }, {
    key: 'dispatchEvent',
    value: function dispatchEvent(eventLabel) {
      var event = document.createEvent('Event');
      event.initEvent(eventLabel, true, true);
      window.dispatchEvent(event);
    }

    // Component methods.

  }, {
    key: 'start',
    value: function start() {
      this.setState({
        play: true
      });

      var tourState = JSON.stringify({ hasPlayed: true });

      if (this.props.useStore) {
        localStorage.setItem(this.props.storeName, tourState);
      }
    }
  }, {
    key: 'stop',
    value: function stop() {
      this.setState({ play: false });
    }
  }, {
    key: 'toggleStep',
    value: function toggleStep(newIndex) {
      this.setState({
        currentIndex: newIndex,
        currentStep: this.props.steps[newIndex]
      });
    }
  }, {
    key: 'getProgress',
    value: function getProgress() {
      var currentStep = this.state.currentIndex + 1;
      var totalSteps = this.props.steps.length;

      return currentStep + ' ' + this.props.pagingLabel + ' ' + totalSteps;
    }
  }, {
    key: 'isNextButtonActive',
    value: function isNextButtonActive() {
      var currentStep = this.state.currentIndex + 1;

      return currentStep != this.props.steps.length;
    }
  }, {
    key: 'isPrevButtonActive',
    value: function isPrevButtonActive() {
      return this.state.currentIndex != 0;
    }
  }, {
    key: 'shouldStart',
    value: function shouldStart() {
      if (!_elementHelper.domElementHelper.canUseDom()) {
        return false;
      }

      // TODO: better implementation of localStorage as state store for React
      // component.
      var tourState = JSON.parse(localStorage.getItem(this.props.storeName));

      return !(tourState && tourState.hasPlayed);
    }
  }, {
    key: 'getButtonsList',
    value: function getButtonsList() {
      return {
        next: {
          active: this.isNextButtonActive(),
          label: this.props.nextButtonLabel,
          title: this.props.nextButtonTitle
        },
        prev: {
          active: this.isPrevButtonActive(),
          label: this.props.prevButtonLabel,
          title: this.props.prevButtonTitle
        },
        close: {
          active: true,
          label: this.props.closeButtonLabel
        }
      };
    }
  }, {
    key: 'renderTourStep',
    value: function renderTourStep() {
      if (!this.state.play) return;

      return _react2.default.createElement(_tourStep.TourStep, {
        onNextClick: this.handleClickOnNext,
        onPrevClick: this.handleClickOnPrev,
        onCloseClick: this.handleClickOnClose,
        name: this.state.currentStep.name,
        title: this.state.currentStep.title,
        content: this.state.currentStep.content,
        targetElement: this.state.currentStep.targetElement,
        popoverPosition: this.state.currentStep.popoverPosition,
        illustration: this.state.currentStep.illustration,
        buttons: this.getButtonsList(),
        onTargetHighlightPosition: this.handleTargetHighlightPosition,
        progress: this.getProgress()
      });
    }
  }, {
    key: 'render',
    value: function render() {
      var tourClassName = (0, _classnames2.default)('k-Tour', { 'is-hidden': !this.state.visible }, this.props.className);

      return _react2.default.createElement(
        'div',
        { className: tourClassName },
        this.renderTourStep()
      );
    }
  }]);

  return Tour;
}(_react.Component);

Tour.defaultProps = {
  nextButtonLabel: 'Next',
  nextButtonTitle: 'Next',
  prevButtonLabel: 'Prev',
  prevButtonTitle: 'Previous',
  closeButtonLabel: 'Close',
  pagingLabel: 'on',
  storeName: 'kitten.Tour',
  useStore: true

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