"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 _network = require("../../server/network.js");

var _firework = _interopRequireDefault(require("../../../firework"));

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 _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }

function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }

function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

function OkrCaseCard(props) {
  var onApplyTpl = props.onApplyTpl,
      cardInfo = props.cardInfo,
      index = props.index;
  var parentName = cardInfo.parentName,
      name = cardInfo.name,
      subelements = cardInfo.subelements,
      usedNum = cardInfo.usedNum,
      id = cardInfo.id,
      isLike = cardInfo.isLike,
      like = cardInfo.like;

  var _useState = (0, _react.useState)(like),
      _useState2 = _slicedToArray(_useState, 2),
      likeNum = _useState2[0],
      setLikeNum = _useState2[1];

  var _useState3 = (0, _react.useState)(isLike),
      _useState4 = _slicedToArray(_useState3, 2),
      hasLike = _useState4[0],
      setHasLike = _useState4[1];

  var _useState5 = (0, _react.useState)(usedNum),
      _useState6 = _slicedToArray(_useState5, 2),
      usedNumber = _useState6[0],
      setUsedNumber = _useState6[1];

  var _useState7 = (0, _react.useState)(false),
      _useState8 = _slicedToArray(_useState7, 2),
      isApplyBtnShow = _useState8[0],
      setIsApplyBtnShow = _useState8[1]; // const [isBlue, setIsBlue] = useState(index % 4 > 1 ? true : false)


  var isBlue = index % 4 > 1;
  var likeRef = (0, _react.useRef)();
  (0, _react.useEffect)(function () {
    setLikeNum(like);
    setHasLike(isLike);
    setUsedNumber(usedNum);
  }, []);
  var setFirework = (0, _react.useCallback)(function () {
    var _getBoundingClientRec = likeRef.current.getBoundingClientRect(),
        x = _getBoundingClientRec.x,
        y = _getBoundingClientRec.y;

    (0, _firework["default"])({
      x: x,
      y: y,
      images: [require("../../image/thumb-up.svg"), require("../../image/666.svg"), require("../../image/bixin.svg"), require("../../image/fighting.svg"), require("../../image/nice.svg"), require("../../image/welldone.svg")]
    });
  }, []);

  var handleLikeClick = function handleLikeClick(isLike, id) {
    setHasLike(!hasLike);

    if (isLike) {
      (0, _network.postLike)(0, id);
      setLikeNum(likeNum - 1);
    } else {
      setLikeNum(likeNum + 1);
      (0, _network.postLike)(1, id);
      setFirework();
    }
  };

  var handleApplyBtnClick = function handleApplyBtnClick(id) {
    setUsedNumber(usedNumber + 1);
    (0, _network.postApply)(id);
    onApplyTpl(cardInfo); // @ts-ignore

    window.titaTracker && window.titaTracker("action").record({
      action: (0, _getLocale.getLocale)("Per_Ass_Pop_button_Apply"),
      actionGroup: (0, _getLocale.getLocale)("OKR_OKRex_Casebase"),
      productName: (0, _getLocale.getLocale)("OKR_OKRex_Casebase")
    });
  };

  return /*#__PURE__*/_react["default"].createElement("div", {
    className: "okr-cases-library__cardList__card",
    onMouseEnter: function onMouseEnter() {
      return setIsApplyBtnShow(true);
    },
    onMouseLeave: function onMouseLeave() {
      return setIsApplyBtnShow(false);
    }
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: "okr-cases-library__cardList__card__title\n            ".concat(isBlue ? "okr-cases-library__cardList__card__title-blue" : "okr-cases-library__cardList__card__title-red")
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: "okr-cases-library__cardList__card__title__label"
  }, "\u300C", parentName, "\u300D"), /*#__PURE__*/_react["default"].createElement("div", {
    className: "okr-cases-library__cardList__card__title__name"
  }, "O", " ", name), /*#__PURE__*/_react["default"].createElement("div", {
    className: "okr-cases-library__cardList__card__title__btn \n                ".concat(isApplyBtnShow && "okr-cases-library__cardList__card__title__btn-show", "\n                ").concat(isBlue ? "okr-cases-library__cardList__card__title__btn-blue" : "okr-cases-library__cardList__card__title__btn-red"),
    onClick: function onClick() {
      return handleApplyBtnClick(id);
    }
  }, (0, _getLocale.getLocale)("Per_Ass_Pop_button_Apply"))), /*#__PURE__*/_react["default"].createElement("div", {
    className: "okr-cases-library__cardList__card__content"
  }, subelements.map(function (item, krindex) {
    return /*#__PURE__*/_react["default"].createElement("div", {
      className: "okr-cases-library__cardList__card__content__kr",
      key: krindex
    }, /*#__PURE__*/_react["default"].createElement("div", {
      style: {
        color: isBlue ? "#2879FF" : "#F05E5E",
        "float": "left",
        marginRight: "5px"
      }
    }, "KR", krindex + 1), /*#__PURE__*/_react["default"].createElement("div", {
      style: {
        "float": "left",
        width: "calc(100% - 36px)"
      }
    }, " ", item.name));
  })), /*#__PURE__*/_react["default"].createElement("div", {
    className: "okr-cases-library__cardList__card__footer"
  }, /*#__PURE__*/_react["default"].createElement("div", null, usedNumber, "\u6B21\u5957\u7528"), /*#__PURE__*/_react["default"].createElement("div", {
    style: {
      display: "flex",
      alignItems: "center"
    }
  }, hasLike ? /*#__PURE__*/_react["default"].createElement("img", {
    src: require("../../image/thumb-up.svg"),
    onClick: function onClick() {
      return handleLikeClick(hasLike, id);
    },
    style: {
      cursor: "pointer"
    }
  }) : /*#__PURE__*/_react["default"].createElement("span", {
    className: "tu-icon-dianzan",
    onClick: function onClick() {
      return handleLikeClick(hasLike, id);
    },
    ref: likeRef
  }), likeNum)));
}

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