"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 _reactFlowRenderer = require("@titaui/react-flow-renderer");

var _classnames = _interopRequireDefault(require("classnames"));

var _krNode = _interopRequireDefault(require("./kr-node"));

var _workNode = _interopRequireDefault(require("./work-node"));

var _context = require("../context");

var _precls = _interopRequireDefault(require("../precls"));

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 _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 ChildNode(props) {
  var data = props.data,
      targetPosition = props.targetPosition,
      sourcePosition = props.sourcePosition;
  var id = data.id,
      modelType = data.modelType,
      isChildExpand = data.isChildExpand,
      isParentExpand = data.isParentExpand,
      nodeData = data.data,
      _data$isSearchRes = data.isSearchRes,
      isSearchRes = _data$isSearchRes === void 0 ? false : _data$isSearchRes;
  var _nodeData$counter = nodeData.counter,
      counter = _nodeData$counter === void 0 ? {} : _nodeData$counter;
  var context = (0, _react.useContext)(_context.CommonContext);
  var onChildExpand = context.onChildExpand,
      onParentExpand = context.onParentExpand,
      onChildClose = context.onChildClose,
      showkr = context.showkr,
      darkTheme = context.darkTheme;
  var _counter$krTotal = counter.krTotal,
      krTotal = _counter$krTotal === void 0 ? 0 : _counter$krTotal,
      _counter$subDirectWor = counter.subDirectWorkTotal,
      subDirectWorkTotal = _counter$subDirectWor === void 0 ? 0 : _counter$subDirectWor,
      _counter$subIndirectS = counter.subIndirectSubWork,
      subIndirectSubWork = _counter$subIndirectS === void 0 ? 0 : _counter$subIndirectS,
      _counter$subWorkTotal = counter.subWorkTotal,
      subWorkTotal = _counter$subWorkTotal === void 0 ? 0 : _counter$subWorkTotal;
  var subTotal = 0;
  var hasParent = true;

  if (modelType == "work") {
    subTotal = showkr ? krTotal + subDirectWorkTotal : subDirectWorkTotal + subIndirectSubWork;
    hasParent = nodeData.parentId !== 0 || nodeData.parentKrId !== 0;
  } else {
    subTotal = subWorkTotal;
    hasParent = nodeData.workId !== 0;
  }

  var expandChild = function expandChild(e) {
    e.stopPropagation();
    isChildExpand ? onChildClose && onChildClose(id) : onChildExpand && onChildExpand(data);
  };

  var expandParent = function expandParent(e) {
    e.stopPropagation();
    onParentExpand && onParentExpand(data);
  };

  var NodeTypeComp = modelType == "kr" ? _krNode["default"] : _workNode["default"];
  return /*#__PURE__*/_react["default"].createElement("div", {
    style: {
      position: "relative"
    },
    key: id
  }, /*#__PURE__*/_react["default"].createElement(NodeTypeComp, {
    data: nodeData,
    isSearchRes: isSearchRes,
    darkTheme: darkTheme
  }), /*#__PURE__*/_react["default"].createElement(_reactFlowRenderer.Handle, {
    type: "target",
    position: targetPosition,
    className: (0, _classnames["default"])(_defineProperty({}, "handle__".concat(targetPosition), !isParentExpand && hasParent))
  }), /*#__PURE__*/_react["default"].createElement(_reactFlowRenderer.Handle, {
    type: "source",
    position: sourcePosition,
    className: (0, _classnames["default"])(_defineProperty({}, "handle__".concat(sourcePosition), subTotal !== 0))
  }), !isParentExpand && hasParent && /*#__PURE__*/_react["default"].createElement("span", {
    className: (0, _classnames["default"])("".concat(_precls["default"], "--node-counter"), "".concat(_precls["default"], "--node-counter__").concat(targetPosition)),
    onClick: expandParent
  }, "1"), subTotal !== 0 && /*#__PURE__*/_react["default"].createElement("span", {
    className: (0, _classnames["default"])("".concat(_precls["default"], "--node-counter"), "".concat(_precls["default"], "--node-counter__").concat(sourcePosition), _defineProperty({}, "".concat(_precls["default"], "--node-counter__close"), isChildExpand)),
    onClick: expandChild
  }, isChildExpand ? "-" : subTotal));
}

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