"use strict";

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

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

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

var _supportsCss = _interopRequireDefault(require("../_util/supportsCss"));

var _ajax = _interopRequireDefault(require("../_util/ajax"));

var _exenv = require("../_util/exenv");

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

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)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj["default"] = obj; return newObj; } }

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 _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? 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 _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }

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

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

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

/**
 * App 下载广告
 */
var Download =
/*#__PURE__*/
function (_Component) {
  _inherits(Download, _Component);

  function Download(props) {
    var _this;

    _classCallCheck(this, Download);

    _this = _possibleConstructorReturn(this, _getPrototypeOf(Download).call(this, props));

    _defineProperty(_assertThisInitialized(_this), "handleScroll", function () {
      var scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;

      if (scrollTop > _this.stickyRoot.offsetTop) {
        var isSticky = _this.state.isSticky;
        if (isSticky) return;

        _this.setState({
          isSticky: true
        });
      } else {
        _this.setState({
          isSticky: false
        });
      }
    });

    _defineProperty(_assertThisInitialized(_this), "saveStickyEle", function (key) {
      return function (el) {
        _this[key] = el;
      };
    });

    var supportSticky = (0, _supportsCss["default"])('position', 'sticky');
    var supportWebkitSticky = (0, _supportsCss["default"])('position', '-webkit-sticky');
    _this.stickyRoot = {};
    _this.stickyContainer = {};
    _this.state = {
      supportSticky: supportSticky,
      supportWebkitSticky: supportWebkitSticky,
      isSticky: false,
      logoUrl: '',
      name: '',
      description: '',
      downloadUrl: ''
    };
    return _this;
  }

  _createClass(Download, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      this.getAppInfo();
      var _this$state = this.state,
          supportSticky = _this$state.supportSticky,
          supportWebkitSticky = _this$state.supportWebkitSticky;

      if (!supportSticky && !supportWebkitSticky) {
        window.addEventListener('scroll', this.handleScroll);
      }
    }
  }, {
    key: "componentWillUnmount",
    value: function componentWillUnmount() {
      window.removeEventListener('scroll', this.handleScroll);
    }
  }, {
    key: "getAppInfo",
    value: function getAppInfo() {
      var _this2 = this;

      var namespaceId = this.props.namespaceId; // 特殊区分 iOS

      var osType = _exenv.IS_IOS ? 1 : 2;
      (0, _ajax["default"])({
        api: '/appUrl/getAppInfo',
        data: {
          namespaceId: namespaceId,
          osType: osType
        },
        success: function success() {
          var appInfo = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
          var downloadUrl = appInfo.downloadUrl;

          if (!_exenv.IS_IOS) {
            downloadUrl = appInfo.linkUrl;
          }

          _this2.setState(_objectSpread({}, appInfo, {
            downloadUrl: downloadUrl
          }));
        },
        error: function error() {
          var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
              errorDescription = _ref.errorDescription;

          console.error(errorDescription);
        }
      });
    }
  }, {
    key: "computeStickyStyle",
    value: function computeStickyStyle() {
      var _this$state2 = this.state,
          supportSticky = _this$state2.supportSticky,
          supportWebkitSticky = _this$state2.supportWebkitSticky,
          isSticky = _this$state2.isSticky;
      var rootStyle = {};
      var containerStyle = {};

      if (supportSticky || supportWebkitSticky) {
        var position = 'relative';

        if (supportSticky) {
          position = 'sticky';
        } else if (supportWebkitSticky) {
          position = '-webkit-sticky';
        }

        rootStyle = {
          position: position,
          top: 0,
          zIndex: 9
        };
      } else {
        rootStyle = {
          paddingTop: this.stickyContainer.offsetHeight
        };
        containerStyle = {
          position: isSticky ? 'fixed' : 'absolute',
          top: 0,
          left: 0,
          right: 0,
          zIndex: 9
        };
      }

      return {
        rootStyle: rootStyle,
        containerStyle: containerStyle
      };
    }
  }, {
    key: "render",
    value: function render() {
      var prefixCls = 'eh-download';
      var _this$props = this.props,
          btnTxt = _this$props.btnTxt,
          themeColor = _this$props.themeColor;
      var _this$state3 = this.state,
          logoUrl = _this$state3.logoUrl,
          name = _this$state3.name,
          description = _this$state3.description,
          downloadUrl = _this$state3.downloadUrl;
      if (!downloadUrl) return null;

      var _this$computeStickySt = this.computeStickyStyle(),
          rootStyle = _this$computeStickySt.rootStyle,
          containerStyle = _this$computeStickySt.containerStyle;

      return _react["default"].createElement("div", {
        className: prefixCls,
        ref: this.saveStickyEle('stickyRoot'),
        style: rootStyle
      }, _react["default"].createElement("div", {
        className: "".concat(prefixCls, "-container"),
        ref: this.saveStickyEle('stickyContainer'),
        style: containerStyle
      }, _react["default"].createElement("img", {
        src: logoUrl,
        className: "".concat(prefixCls, "-logo"),
        alt: "logo"
      }), _react["default"].createElement("div", {
        className: "".concat(prefixCls, "-description")
      }, _react["default"].createElement("span", {
        className: "name"
      }, name), _react["default"].createElement("span", {
        className: "description"
      }, description)), _react["default"].createElement("a", {
        href: downloadUrl,
        className: "".concat(prefixCls, "-btn"),
        style: {
          backgroundColor: themeColor || '#10A1F1'
        }
      }, btnTxt || '立即打开')));
    }
  }]);

  return Download;
}(_react.Component);

_defineProperty(Download, "propTypes", {
  namespaceId: _propTypes["default"].number.isRequired,
  // 当前域空间主题色
  themeColor: _propTypes["default"].string,
  // App 下载广告按钮文字，默认为”立即打开“
  btnTxt: _propTypes["default"].string
});

_defineProperty(Download, "defaultProps", {
  btnTxt: '立即打开',
  themeColor: '#10A1F1'
});

var _default = Download;
exports["default"] = _default;
module.exports = exports.default;