'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.triggerEnhancer = 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 _elementHelper = require('kitten/helpers/dom/element-helper');

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 triggerEnhancer = exports.triggerEnhancer = function triggerEnhancer(WrappedComponent, wrappedComponentProps) {
  var TriggerWrapper = function (_React$Component) {
    _inherits(TriggerWrapper, _React$Component);

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

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

      _this.state = {
        play: false
      };

      _this.handleStop = _this.handleStop.bind(_this);
      return _this;
    }

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

        this.dispatchEvent(this.props.stopEventName);
      }
    }, {
      key: 'dispatchEvent',
      value: function dispatchEvent(eventLabel) {
        if (!_elementHelper.domElementHelper.canUseDom()) return;

        var event = document.createEvent('Event');

        event.initEvent(eventLabel, true, true);
        window.dispatchEvent(event);
      }
    }, {
      key: 'hasPlayed',
      value: function hasPlayed() {
        // TODO: better implementation of localStorage as state store for React
        // component.
        var componentState = JSON.parse(localStorage.getItem(this.props.storeName));

        return componentState && componentState.hasPlayed;
      }
    }, {
      key: 'shouldStart',
      value: function shouldStart() {
        if (!_elementHelper.domElementHelper.canUseDom()) {
          return false;
        }
        if (!this.props.verifyStorageOnStart) {
          return true;
        }

        return this.props.autorun && !this.hasPlayed();
      }
    }, {
      key: 'start',
      value: function start() {
        var _this2 = this;

        setTimeout(function () {
          return _this2.setState({ play: true });
        }, 10);

        setTimeout(function () {
          var componentState = JSON.stringify({ hasPlayed: true });
          var storeName = _this2.props.storeName;

          localStorage.setItem(storeName, componentState);
        }, 1000);
      }
    }, {
      key: 'stop',
      value: function stop() {
        this.setState({ play: false });
      }
    }, {
      key: 'handlerProps',
      value: function handlerProps() {
        var handlerProps = {};

        if (this.props.stopHandlerName) {
          handlerProps[this.props.stopHandlerName] = this.handleStop;
        }

        return handlerProps;
      }
    }, {
      key: 'render',
      value: function render() {
        if (!this.state.play) return null;

        return _react2.default.createElement(WrappedComponent, _extends({}, this.handlerProps(), wrappedComponentProps));
      }
    }]);

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

  TriggerWrapper.defaultProps = {
    autorun: true,
    stopEventName: 'k:auto-trigger:stop',
    stopHandlerName: null,
    storeName: 'kitten.AutoTrigger',
    verifyStorageOnStart: true
  };

  return TriggerWrapper;
};