"use strict";

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

var _fishx = require("fishx");

var _propTypes = _interopRequireDefault(require("prop-types"));

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

var _uuid = require("uuid");

var _constants = require("../constants");

var _Modal = _interopRequireDefault(require("../MComponents/mobile/components/common/Message/Modal"));

var _androidUtil = require("../utils/androidUtil");

var _businessUtil = require("../utils/businessUtil");

var _getLocale = require("../utils/getLocale");

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

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

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? Object(arguments[i]) : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }

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

function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { 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 { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], 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 _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; }

/**
 * @class MBaseComponent
 * 所有可以用在 Designer 中的组件的基类，抽象类，请不要直接创建此类的实例。
 * TODO:需要重构，抽象成高阶组件，可以自动包装任意 React 组件进行拖拽和布局。
 *
 * @abstract
 * @author zhang.xiaofei38@iwhalecloud.com
 * 抽象类，所有可拖拽类的基类，请不要创建此类的实例。
 */
var MBaseComponent = /*#__PURE__*/function (_Component) {
  _inherits(MBaseComponent, _Component);

  var _super = _createSuper(MBaseComponent);

  /**
   * @required
   * React 会根据 type 动态创建组件的实例。
   */
  function MBaseComponent(props) {
    var _this;

    _classCallCheck(this, MBaseComponent);

    _this = _super.call(this, props);

    _defineProperty(_assertThisInitialized(_this), "onComponentClick", function (event) {
      // 这里必须阻止事件冒泡，然后再触发自定义的事件
      event.preventDefault();
      event.stopPropagation();
      var evt = new Event('component-click', {
        bubbles: false
      });
      evt.data = _this.props.nodeData;
      window.document.dispatchEvent(evt);
    });

    _defineProperty(_assertThisInitialized(_this), "receiveCoupons", function (params) {
      return (0, _fishx.fetch)("".concat(window.MParams.HUrl, "/coupons/usercoupon"), {
        method: 'POST',
        data: params
      });
    });

    _defineProperty(_assertThisInitialized(_this), "isDesignMode", function () {
      return _this.props.mode === 'designer';
    });

    _defineProperty(_assertThisInitialized(_this), "getCoupon", function (couponId) {
      var locale = (0, _getLocale.getComponentLocale)(_this.props, _this.context, 'CouponList', function () {
        return require('./mobile/components/coupon-list/locale/en_US');
      });

      _this.receiveCoupons({
        couponId: Number(couponId)
      }).then(function (res) {
        _Modal["default"].success({
          content: locale.VouponReceivedSuccess,
          okText: locale.GoShopping
        });
      })["catch"](function (_ref) {
        var code = _ref.code,
            message = _ref.message;

        if (code === _constants.respCodeMap.NOT_LOGGED_IN) {
          return;
        }

        _Modal["default"].success({
          content: message,
          okText: locale.GoShopping
        });
      });
    });

    _defineProperty(_assertThisInitialized(_this), "goH5Page", function (url) {
      if (url.startsWith('/')) {
        window.MParams && window.MParams.history && window.MParams.history.push(url);
      } else {
        location.href = url;
      }
    });

    _defineProperty(_assertThisInitialized(_this), "clickItem", function (url) {
      var _this$props = _this.props,
          terminalType = _this$props.terminalType,
          mode = _this$props.mode; //运营平台预览模式、url为空、非renderer模式 不可点击

      if (window.MParams.noClick || mode !== 'renderer' || !url) {
        return;
      }

      var _parseUrl = (0, _businessUtil.parseUrl)(url),
          h5Link = _parseUrl.h5Link,
          pcLink = _parseUrl.pcLink,
          params = _parseUrl.params;

      var type = params.type,
          data = params.data; //锚点

      if (type === 'ANCHOR') {
        document.getElementById(data.id) && document.getElementById(data.id).scrollIntoView({
          behavior: 'smooth'
        });
        return;
      } //领取优惠券


      if (type === 'GET_COUPON') {
        _this.getCoupon(data.couponId);

        return;
      }

      if (!(0, _androidUtil.browserVersion)().isApp) {
        terminalType === 'pc' ? window.open(pcLink) : _this.goH5Page(h5Link);
        return;
      }

      if ((0, _androidUtil.browserVersion)().android) {
        (0, _androidUtil.sendToAndroid)({
          type: 'jump',
          params: {
            url: h5Link
          }
        });
      } else if ((0, _androidUtil.browserVersion)().ios) {
        if (type === 'CUSTOMIZED') {
          //适配ios 从首页跳转自定义页面
          _this.goH5Page(h5Link);
        }

        (0, _androidUtil.sendToIos)(params);
      }
    });

    var uuid = (0, _uuid.v4)();
    _this.state = _objectSpread({
      id: uuid,
      // required
      key: uuid
    }, props);
    return _this;
  }
  /**
   * @method onComponentClick
   * 在组件实例上点击时的默认 Handler，子类可以覆盖此方法提供自己特有的实现。
   */


  _createClass(MBaseComponent, [{
    key: "render",
    value: function render() {
      return /*#__PURE__*/_react["default"].createElement("div", null, "MBaseComponent");
    }
  }]);

  return MBaseComponent;
}(_react.Component);

exports["default"] = MBaseComponent;

_defineProperty(MBaseComponent, "type", 'MBaseComponent');

_defineProperty(MBaseComponent, "contextTypes", {
  antLocale: _propTypes["default"].object
});