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 _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 _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

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

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); }

/* eslint-disable */
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { composeThemeFromProps } from '@css-modules-theme/react';
import Card from '../Card/Card';
import './iframeCardStyles.scss';
import UrlUtils from './UrlUtils';
import buildThemeProperties from '../Utils';
import style from '../Layout/LayoutStyle.scss';
var IFRAME_NOT_SUPPORTED_STR = 'This browser does not support iframes.';

var IframeCard =
/*#__PURE__*/
function (_Component) {
  _inherits(IframeCard, _Component);

  function IframeCard() {
    var _getPrototypeOf2;

    var _this;

    _classCallCheck(this, IframeCard);

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _possibleConstructorReturn(this, (_getPrototypeOf2 = _getPrototypeOf(IframeCard)).call.apply(_getPrototypeOf2, [this].concat(args)));

    _this.getSrcURL = function () {
      if (_this.props.params || _this.props.externalProps) {
        var params = _this.props.params || _this.props.externalProps;
        return UrlUtils.template(_this.props.url, params);
      }

      return _this.props.url;
    };

    _this.subscribeToIframeEvents = function () {
      window.addEventListener('message', function (e) {
        try {
          var event = JSON.parse(e.data);
          console.log(event);

          _this.handleEvent({
            type: event.type,
            data: event.data
          });
        } catch (err) {
          console.warn('cannot handle event; this might be a system event that does not match the interactive iframe api', e);
        }
      });
    };

    _this.handleEvent = function (_ref) {
      var type = _ref.type,
          data = _ref.data;

      if (_this.props.eventManager) {
        _this.props.eventManager.trigger(type, _this.props.configId, data);
      }
    };

    _this.subscribeToApplicationEvents = function () {
      var em = _this.props.eventManager;
      var eventIds = _this.props.eventIds;
      var params = _this.props.params;
      var url = _this.getSrcURL.url;

      var _loop = function _loop(index) {
        em.subscribe(eventIds[index], function (eventData) {
          var event = {};
          event.metadata = {
            type: eventIds[index]
          };
          event.data = eventData;

          try {
            var stringMessage = JSON.stringify(event);

            _this.frameReference.contentWindow.postMessage(stringMessage, url);
          } catch (error) {
            console.error(error); // eslint-disable-line
          }
        });
      };

      for (var index = 0; index < eventIds.length; index += 1) {
        _loop(index);
      }
    };

    return _this;
  }

  _createClass(IframeCard, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      this.subscribeToIframeEvents();
      this.subscribeToApplicationEvents();
    }
  }, {
    key: "componentWillUnmount",
    value: function componentWillUnmount() {
      window.removeEventListener('message', this.handleFrameTasks); // TODO need to unsubscribe to events here
    }
  }, {
    key: "render",
    value: function render() {
      var _this2 = this;

      var src = this.getSrcURL();
      var _this$props = this.props,
          theme = _this$props.theme,
          themeProps = _this$props.themeProps;
      var themingProperties = buildThemeProperties(theme, themeProps);
      var themeStyles = composeThemeFromProps(style, themingProperties, {
        compose: 'Merge'
      });
      return React.createElement(Card, this.props, React.createElement("iframe", {
        className: themeStyles.iframeCard,
        title: src,
        src: src,
        ref: function ref(iframe) {
          _this2.frameReference = iframe;
        }
      }, IFRAME_NOT_SUPPORTED_STR));
    }
  }]);

  return IframeCard;
}(Component);

export { IframeCard as default };
IframeCard.propTypes = {
  configId: PropTypes.string.isRequired,
  title: PropTypes.string,
  eventManager: PropTypes.instanceOf(Object),
  actions: PropTypes.arrayOf(PropTypes.shape({
    id: PropTypes.string,
    displayName: PropTypes.string
  })),
  eventIds: PropTypes.arrayOf(PropTypes.string),
  url: PropTypes.string.isRequired,
  params: PropTypes.instanceOf(Object),
  externalProps: PropTypes.instanceOf(Object),
  themeProps: PropTypes.shape({
    compose: PropTypes.string,
    prefix: PropTypes.string
  }),
  theme: PropTypes.instanceOf(Object)
};
IframeCard.defaultProps = {
  title: undefined,
  actions: undefined,
  eventManager: undefined,
  eventIds: [],
  themeProps: {
    compose: 'merge',
    prefix: 'configurableInteractiveLayout-'
  }
};