"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 _progressLeftQuoto = _interopRequireDefault(require("./img/progress-left-quoto.svg"));

var _progressRightQuoto = _interopRequireDefault(require("./img/progress-right-quoto.svg"));

var _progressDelay = _interopRequireDefault(require("./img/progress-delay.svg"));

var _progressNormal = _interopRequireDefault(require("./img/progress-normal.svg"));

var _progressRisk = _interopRequireDefault(require("./img/progress-risk.svg"));

var _progressEnd = _interopRequireDefault(require("./img/progress-end.svg"));

var _utils = require("./utils");

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

var _dangerousHtml = _interopRequireDefault(require("../dangerous-html"));

require("./index.css");

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

var StateIconMapping = {
  delay: _progressDelay["default"],
  normal: _progressNormal["default"],
  risk: _progressRisk["default"],
  end: _progressEnd["default"]
};
var $prefix = "okr-progress-item";

var Detail = function Detail(_ref) {
  var id = _ref.id,
      riskLevel = _ref.riskLevel,
      _ref$oStatus = _ref.oStatus,
      oStatus = _ref$oStatus === void 0 ? 1 : _ref$oStatus,
      title = _ref.title,
      percent = _ref.percent,
      _ref$oldPercent = _ref.oldPercent,
      oldPercent = _ref$oldPercent === void 0 ? 0 : _ref$oldPercent,
      content = _ref.content,
      time = _ref.time,
      _ref$canEdit = _ref.canEdit,
      canEdit = _ref$canEdit === void 0 ? false : _ref$canEdit,
      onEditProgress = _ref.onEditProgress,
      onDeleteProgress = _ref.onDeleteProgress,
      rightTip = _ref.rightTip,
      _ref$showPercent = _ref.showPercent,
      showPercent = _ref$showPercent === void 0 ? true : _ref$showPercent,
      _ref$canDel = _ref.canDel,
      canDel = _ref$canDel === void 0 ? false : _ref$canDel;

  var _useState = (0, _react.useState)(false),
      _useState2 = _slicedToArray(_useState, 2),
      isMouseEnterDetail = _useState2[0],
      setIsMouseEnterDetail = _useState2[1];

  var nowTime = (0, _react.useMemo)(function () {
    return new Date();
  }, []);
  var renderPercentDiffer = (0, _react.useMemo)(function () {
    var progressIsAdvance = percent > oldPercent;
    return /*#__PURE__*/_react["default"].createElement("span", null, percent === oldPercent ? null : /*#__PURE__*/_react["default"].createElement("span", {
      className: "".concat($prefix, "__percent-differ-icon ").concat(progressIsAdvance ? "".concat($prefix, "__percent--advance") : "".concat($prefix, "__percent--delay"))
    }, progressIsAdvance ? /*#__PURE__*/_react["default"].createElement("i", {
      className: "tu-icon-simple-sort-up"
    }) : /*#__PURE__*/_react["default"].createElement("i", {
      className: "tu-icon-simple-sort-down"
    }), Math.abs(percent - oldPercent), "%"));
  }, [percent, oldPercent]);
  var handleEditProgress = (0, _react.useCallback)(function () {
    onEditProgress && onEditProgress(id, content);
  }, [onEditProgress]);
  var handleDeleteProgress = (0, _react.useCallback)(function () {
    onDeleteProgress && onDeleteProgress(id);
  }, [onDeleteProgress]);
  var handleMouseEnterDetail = (0, _react.useCallback)(function () {
    setIsMouseEnterDetail(true);
  }, []);
  var handleMouseLeaveDetail = (0, _react.useCallback)(function () {
    setIsMouseEnterDetail(false);
  }, []);
  return /*#__PURE__*/_react["default"].createElement("div", {
    className: "".concat($prefix, "-wrapper")
  }, /*#__PURE__*/_react["default"].createElement("div", null, /*#__PURE__*/_react["default"].createElement("div", {
    className: "".concat($prefix, "-content__header")
  }, /*#__PURE__*/_react["default"].createElement("img", {
    className: "".concat($prefix, "-content__icon-state"),
    src: StateIconMapping[(0, _utils.getProgressState)(oStatus, riskLevel)]
  }), /*#__PURE__*/_react["default"].createElement("span", null, title), showPercent ? /*#__PURE__*/_react["default"].createElement("span", {
    className: "".concat($prefix, "__percent")
  }, percent, "%", renderPercentDiffer) : null, /*#__PURE__*/_react["default"].createElement("span", {
    className: "".concat($prefix, "__right-tip")
  }, rightTip)), /*#__PURE__*/_react["default"].createElement("div", {
    className: "".concat($prefix, "-content__body-wrapper")
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: "".concat($prefix, "-content__body ").concat(canEdit ? "".concat($prefix, "-content--edit") : ""),
    onMouseEnter: handleMouseEnterDetail,
    onMouseLeave: handleMouseLeaveDetail
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: "".concat($prefix, "-content__body-top")
  }, content && content.trim() ? /*#__PURE__*/_react["default"].createElement("img", {
    className: "".concat($prefix, "-content__icon-left-quoto"),
    src: _progressLeftQuoto["default"]
  }) : null, /*#__PURE__*/_react["default"].createElement("span", {
    className: "".concat($prefix, "__content")
  }, content && /*#__PURE__*/_react["default"].createElement(_dangerousHtml["default"], {
    htmlText: content.trim()
  }))), /*#__PURE__*/_react["default"].createElement("div", {
    className: "".concat($prefix, "__content-bottom")
  }, /*#__PURE__*/_react["default"].createElement("span", null, (0, _utils.getTimeString)(time, nowTime)), canEdit && isMouseEnterDetail ? /*#__PURE__*/_react["default"].createElement("span", {
    className: "".concat($prefix, "__bottom-func-btns")
  }, /*#__PURE__*/_react["default"].createElement("span", {
    className: "".concat($prefix, "__bottom-btn--edit"),
    onClick: handleEditProgress
  }, /*#__PURE__*/_react["default"].createElement("i", {
    className: "tu-icon-bianji3 ".concat($prefix, "__bottom-btn-icon")
  }), (0, _getLocale.getLocale)("Mod_Editor")), canEdit && canDel && /*#__PURE__*/_react["default"].createElement("span", {
    className: "".concat($prefix, "__bottom-divider")
  }), canDel && /*#__PURE__*/_react["default"].createElement("span", {
    className: "".concat($prefix, "__bottom-btn--delete"),
    onClick: handleDeleteProgress
  }, /*#__PURE__*/_react["default"].createElement("i", {
    className: "tu-icon-shanchu ".concat($prefix, "__bottom-btn-icon")
  }), (0, _getLocale.getLocale)("Mod_Delete"))) : null)), /*#__PURE__*/_react["default"].createElement("div", {
    className: "".concat($prefix, "-content__right-block")
  }, content && content.trim() ? /*#__PURE__*/_react["default"].createElement("img", {
    className: "".concat($prefix, "-content__icon-right-quoto"),
    src: _progressRightQuoto["default"]
  }) : null))));
};

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