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 _class, _temp2;

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 _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }

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

import React from 'react';
import PropTypes from 'prop-types';
import './style.css';

var Icon = (_temp2 = _class = function (_React$Component) {
  _inherits(Icon, _React$Component);

  function Icon() {
    var _temp, _this, _ret;

    _classCallCheck(this, Icon);

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

    return _ret = (_temp = (_this = _possibleConstructorReturn(this, _React$Component.call.apply(_React$Component, [this].concat(args))), _this), _this.state = {
      content: null,
      loaded: null
    }, _this.loadAssets = function (asset) {
      return Promise.resolve().then(function () {
        return _interopRequireWildcard(require('!svg-sprite-loader!./assets/' + asset + '.svg'));
      });
    }, _temp), _possibleConstructorReturn(_this, _ret);
  }

  Icon.prototype.render = function render() {
    var _this2 = this;

    var _props = this.props,
        svgWidth = _props.svgWidth,
        svgHeight = _props.svgHeight,
        title = _props.title,
        name = _props.name,
        classes = _props.classes,
        ariaHidden = _props.ariaHidden,
        fill = _props.fill,
        rest = _objectWithoutProperties(_props, ['svgWidth', 'svgHeight', 'title', 'name', 'classes', 'ariaHidden', 'fill']);
    // The promise will not be resolved until after render,
    // so re-render once the promise is finished by using state.


    this.loadAssets(name).then(function (_ref) {
      var SVG = _ref.default;

      if (SVG && SVG.content) {
        var attr = {
          width: svgWidth || null,
          height: svgHeight || null,
          className: classes && classes.length > 0 ? classes.filter(function (c) {
            return c;
          }).toString() : null,
          'aria-hidden': ariaHidden || null
        };
        var content = React.createElement(
          'svg',
          _extends({}, attr, rest),
          title && React.createElement(
            'title',
            null,
            title
          ),
          React.createElement('use', { xlinkHref: '#' + name, fill: fill })
        );
        if (_this2.state.loaded !== name) {
          _this2.setState({ loaded: name, content: content });
        }
      }
    });
    return this.state.content;
  };

  return Icon;
}(React.Component), _class.defaultProps = {
  title: null,
  classes: []
}, _temp2);
export { Icon as default };
Icon.propTypes = process.env.NODE_ENV !== "production" ? {
  name: PropTypes.string.isRequired,
  title: PropTypes.string,
  svgWidth: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
  svgHeight: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
  classes: PropTypes.arrayOf(PropTypes.string),
  ariaHidden: PropTypes.bool,
  fill: PropTypes.string
} : {};