"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 _classnames = _interopRequireDefault(require("classnames"));

var _richEditor = require("@titaui/rich-editor");

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

var _krItemWeight = _interopRequireDefault(require("./kr-item-weight"));

var _okrNewUserSelector = _interopRequireDefault(require("../fields/okr-new-user-selector"));

var _inputWithErrPopup = _interopRequireDefault(require("../fields/input-with-err-popup"));

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

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 ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }

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

function KrItem(props) {
  var krData = props.krData,
      changeKr = props.changeKr,
      deleteKr = props.deleteKr,
      container = props.container,
      index = props.index,
      onKrInputStateChange = props.onKrInputStateChange,
      onAddKr = props.onAddKr,
      setIsInputContent = props.setIsInputContent,
      domRef = props.domRef,
      krIsValidate = props.krIsValidate;
  var setting = (0, _bsGlobal.getBSGlobal)('OkrAdvancedSetting').KRSetting;
  var openKrWeight = setting.KrWeight;
  var mentionInputRef = (0, _react.useRef)();
  var oldKrNameRef = (0, _react.useRef)('');
  var krDataRef = (0, _react.useRef)(krData);

  var onUserSelectorSubmitHandler = function onUserSelectorSubmitHandler(users) {
    changeKr(_objectSpread(_objectSpread({}, krDataRef.current), {}, {
      principalId: users[0].Id,
      principalName: users[0].Name,
      userAvatar: users[0].UserAvatar
    }));
  };

  var onKrNameInputChangeHandler = (0, _react.useCallback)(function (data) {
    var mentionUsers = data.mentionUsers,
        pureText = data.pureText,
        pureTextExcludeAt = data.pureTextExcludeAt,
        richText = data.richText;
    changeKr(_objectSpread(_objectSpread({}, krDataRef.current), {}, {
      mileStoneName: pureText,
      mileStoneNameRichText: richText,
      mileStoneNamePureTextExcludeAt: pureTextExcludeAt,
      aiteUsers: mentionUsers === null || mentionUsers === void 0 ? void 0 : mentionUsers.map(function (user) {
        return user.mentionId;
      })
    }));
  }, []);

  var onDeleteKrClickHandler = function onDeleteKrClickHandler() {
    return deleteKr(krData.uuid);
  };

  var formatUser = (0, _react.useMemo)(function () {
    return {
      Medium: krData.userAvatar.Medium,
      Color: krData.userAvatar.Color,
      Name: krData.principalName,
      Id: krData.principalId
    };
  }, [krData.userAvatar.Medium, krData.principalName, krData.principalName, krData.principalId]);
  var handleInputFocus = (0, _react.useCallback)(function () {
    onKrInputStateChange && onKrInputStateChange(true, krData);
  }, [krData]);
  var handleInputBlur = (0, _react.useCallback)(function () {
    onKrInputStateChange && onKrInputStateChange(false, krData);
  }, [krData]); // TODO 回车创建下一个kr并且自动聚焦到下一个kr
  // const handleKeyUp = useCallback((e) => {
  //   e.persist()
  //   if (13 === e.keyCode) {
  //     onAddKr && onAddKr();
  //   }
  // }, [onAddKr])

  var handleOnKeyDown = (0, _react.useCallback)(function (e) {
    setIsInputContent(true);
  }, [setIsInputContent]);
  var handleOnKeyUp = (0, _react.useCallback)(function (e) {
    e.persist(); // 解决按功能按钮的时候一直显示检测中

    if (e.target.value === oldKrNameRef.current) {
      setIsInputContent(false);
    } else {
      oldKrNameRef.current = e.target.value;
    }
  }, []);
  (0, _react.useEffect)(function () {
    krDataRef.current = krData;
  }, [krData]);
  var _krDataRef$current = krDataRef.current,
      mileStoneNamePureTextExcludeAt = _krDataRef$current.mileStoneNamePureTextExcludeAt,
      mileStoneNameRichText = _krDataRef$current.mileStoneNameRichText;
  var krNameLen = (mileStoneNamePureTextExcludeAt === null || mileStoneNamePureTextExcludeAt === void 0 ? void 0 : mileStoneNamePureTextExcludeAt.length) || 0;
  return /*#__PURE__*/_react["default"].createElement("div", {
    className: "create-kr-item-wrapper"
  }, /*#__PURE__*/_react["default"].createElement("a", {
    className: "kr-item__delete-icon tu-icon-del",
    onClick: onDeleteKrClickHandler
  }), /*#__PURE__*/_react["default"].createElement("div", {
    className: (0, _classnames["default"])('kr-item__right-block', {
      'kr-item__right-block--error': krNameLen > 500
    })
  }, /*#__PURE__*/_react["default"].createElement("span", {
    className: "kr-item__order"
  }, "KR", index), /*#__PURE__*/_react["default"].createElement("div", {
    className: "kr-item__input-wrapper"
  }, /*#__PURE__*/_react["default"].createElement(_inputWithErrPopup["default"], {
    strLength: krNameLen,
    maxLength: 500,
    position: "top"
  }, /*#__PURE__*/_react["default"].createElement(_richEditor.TitaInlineRichEditor, {
    initialState: mileStoneNameRichText,
    key: krData === null || krData === void 0 ? void 0 : krData.uuid,
    onDocChange: onKrNameInputChangeHandler,
    onFocus: handleInputFocus,
    onBlur: handleInputBlur,
    onKeyDown: handleOnKeyDown,
    onKeyUp: handleOnKeyUp,
    placeholder: (0, _getLocale.getLocale)('OKR_MyO_Pop_Keyresults'),
    ref: mentionInputRef
  })), /*#__PURE__*/_react["default"].createElement("div", {
    className: "kr-item-right-block__white-block"
  }), /*#__PURE__*/_react["default"].createElement("div", {
    className: "kr-item-right-block__text-mask"
  })), openKrWeight && /*#__PURE__*/_react["default"].createElement(_krItemWeight["default"], {
    krData: krData,
    changeKr: changeKr
  }), /*#__PURE__*/_react["default"].createElement(_okrNewUserSelector["default"], {
    user: formatUser,
    container: container,
    changeUser: onUserSelectorSubmitHandler
  })));
}

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