"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _react = _interopRequireDefault(require("react"));

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

function _typeof(obj) { "@babel/helpers - typeof"; 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 ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return 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 _createSuper(Derived) { return function () { var Super = _getPrototypeOf(Derived), result; if (_isNativeReflectConstruct()) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }

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 _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }

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

var GUmiUIFlag = /*#__PURE__*/function (_React$Component) {
  _inherits(GUmiUIFlag, _React$Component);

  var _super = _createSuper(GUmiUIFlag);

  function GUmiUIFlag(props) {
    var _this;

    _classCallCheck(this, GUmiUIFlag);

    _this = _super.call(this, props);

    _this.handleMessage = function (event) {
      try {
        var _JSON$parse = JSON.parse(event.data),
            action = _JSON$parse.action;

        switch (action) {
          case 'umi.ui.enableBlockEditMode':
            _this.setState({
              visible: true
            });

            break;

          case 'umi.ui.disableBlockEditMode':
            _this.setState({
              visible: false
            });

            break;

          case 'umi.ui.enable.GUmiUIFlag':
            _this.setState({
              visible: true
            });

            break;

          default:
            break;
        }
      } catch (_) {}
    };

    _this.toggleHover = function () {
      _this.setState(function (_ref) {
        var hovered = _ref.hovered;
        return {
          hovered: !hovered
        };
      });
    };

    _this.clickHandler = function () {
      var _this$props = _this.props,
          filename = _this$props.filename,
          index = _this$props.index;
      var el = document.getElementById('umi-ui-bubble');

      if (el && el.contentWindow) {
        el.contentWindow.postMessage(JSON.stringify({
          action: 'umi.ui.block.addBlock',
          payload: {
            filename: filename,
            index: index
          }
        }), '*');
      }
    };

    _this.state = {
      hovered: false,
      visible: false
    };
    return _this;
  }

  _createClass(GUmiUIFlag, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      window.addEventListener('message', this.handleMessage, false);
    }
  }, {
    key: "componentWillUnmount",
    value: function componentWillUnmount() {
      window.removeEventListener('message', this.handleMessage, false);
    }
  }, {
    key: "render",
    value: function render() {
      var _this$props2 = this.props,
          inline = _this$props2.inline,
          children = _this$props2.children;
      var _this$state = this.state,
          visible = _this$state.visible,
          hovered = _this$state.hovered;
      if (!visible) return children || null;
      return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement("div", {
        onClick: this.clickHandler,
        style: _objectSpread({}, inline ? {
          display: 'inline-block',
          verticalAlign: 'middle'
        } : {
          margin: '10px 0',
          height: '60px',
          lineHeight: '60px'
        }, {
          background: hovered ? 'rgba(24, 144, 255, 0.25)' : 'rgba(24,144,255,0.15)',
          border: '1px dashed #1890ff',
          textAlign: 'center',
          color: '#329bff',
          fontSize: '14px',
          cursor: 'pointer',
          transition: 'all .3s',
          overflow: 'hidden',
          fontFamily: 'Chinese Quote,-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,PingFang SC,Hiragino Sans GB,Microsoft YaHei,Helvetica Neue,Helvetica,Arial,sans-serif'
        }),
        className: "g_umiuiBlockAddEditMode",
        onMouseEnter: this.toggleHover,
        onMouseLeave: this.toggleHover
      }, /*#__PURE__*/_react.default.createElement("svg", {
        width: "1em",
        height: "1em",
        fill: "currentColor",
        viewBox: "0 0 1024 1024",
        style: {
          display: 'inline-block',
          verticalAlign: 'middle',
          marginRight: '8px'
        }
      }, /*#__PURE__*/_react.default.createElement("path", {
        d: "M482 152h60c5.333 0 8 2.667 8 8v704c0 5.333-2.667 8-8 8h-60c-5.333 0-8-2.667-8-8V160c0-5.333 2.667-8 8-8zM176 474h672c5.333 0 8 2.667 8 8v60c0 5.333-2.667 8-8 8H176c-5.333 0-8-2.667-8-8v-60c0-5.333 2.667-8 8-8z",
        fill: "#329bff",
        fillRule: "nonzero"
      })), /*#__PURE__*/_react.default.createElement("span", {
        style: {
          display: 'inline-block',
          verticalAlign: 'middle'
        }
      }, "Add to here")), children);
    }
  }]);

  return GUmiUIFlag;
}(_react.default.Component);

exports.default = GUmiUIFlag;