"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }

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

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

var _requestApis = require("./request-apis");

var _bsGlobal = require("../../utils/bs-global");

var _pinkCard = require("./pink-card");

var _card = _interopRequireDefault(require("./pink-card/card"));

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

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

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 asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }

function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }

function ShareCardSelector(_ref) {
  var onShare = _ref.onShare,
      closeModal = _ref.closeModal,
      data = _ref.data;
  var base64ImageRef = (0, _react.useRef)();
  (0, _react.useEffect)(function () {
    if ((0, _bsGlobal.isWx)()) {
      window.WWOpenData && window.WWOpenData.enableCanvasSharing && window.WWOpenData.enableCanvasSharing();
    }

    if (data.user.avatar.src) {
      base64ImageRef.current = (0, _requestApis.getImageBase64ByUrl)(data.user.userId).then(function (res) {
        return (0, _pinkCard.drawPinkCard)(data, "data:image/jpeg;base64,".concat(res.Data.obj));
      });
    } else {
      base64ImageRef.current = (0, _pinkCard.drawPinkCard)(data, "");
    }

    return function () {
      if ((0, _bsGlobal.isWx)()) {
        window.WWOpenData && window.WWOpenData.disableCanvasSharing && window.WWOpenData.disableCanvasSharing();
      }
    };
  }, []);
  return /*#__PURE__*/_react["default"].createElement("div", {
    className: "okr-share-card__selector"
  }, /*#__PURE__*/_react["default"].createElement("a", {
    className: "tu-icon-close",
    onClick: function onClick() {
      return closeModal();
    }
  }), /*#__PURE__*/_react["default"].createElement(_card["default"], {
    data: data
  }), /*#__PURE__*/_react["default"].createElement("div", {
    className: "share-card-button__wrapper"
  }, /*#__PURE__*/_react["default"].createElement("a", {
    className: "share-card-button download",
    onClick: /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
      var base64img, url, a;
      return regeneratorRuntime.wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              _context.next = 2;
              return base64ImageRef.current;

            case 2:
              base64img = _context.sent;
              _context.next = 5;
              return (0, _requestApis.getBase64DownloadUrl)(base64img.split(",")[1]);

            case 5:
              url = _context.sent;
              a = document.createElement("a");
              a.href = url.Data.obj;
              a.download = "okr.png";
              a.click();

            case 10:
            case "end":
              return _context.stop();
          }
        }
      }, _callee);
    }))
  }, (0, _getLocale.getLocale)("OKR_MyO_Popup_Download")), /*#__PURE__*/_react["default"].createElement("a", {
    className: "share-card-button share",
    onClick: /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2() {
      var base64img;
      return regeneratorRuntime.wrap(function _callee2$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              _context2.next = 2;
              return base64ImageRef.current;

            case 2:
              base64img = _context2.sent;
              onShare(base64img);

            case 4:
            case "end":
              return _context2.stop();
          }
        }
      }, _callee2);
    }))
  }, (0, _getLocale.getLocale)("Mod_ShareFeed"))));
}

var _default = ShareCardSelector;
exports["default"] = _default;