"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.tooltipBehaviorAction = void 0;
var _input = _interopRequireDefault(require("@alifd/next/lib/input"));
var _radio = _interopRequireDefault(require("@alifd/next/lib/radio"));
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _box = _interopRequireDefault(require("@alifd/next/lib/box"));
var React = _interopRequireWildcard(require("react"));
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { "default": e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n["default"] = e, t && t.set(e, n), n; }
var findReact = function findReact(id, traverseUp) {
  if (traverseUp === void 0) {
    traverseUp = 0;
  }
  var dom = document.querySelector("#" + id);
  if (!dom) return;
  var key = Object.keys(dom).find(function (item) {
    return item.startsWith('__reactFiber$') ||
    // react 17+
    item.startsWith('__reactInternalInstance$'); // react <17
  });
  var domFiber = dom[key];
  if (domFiber == null) return null;

  // react <16
  if (domFiber._currentElement) {
    var _compFiber = domFiber._currentElement._owner;
    for (var i = 0; i < traverseUp; i++) {
      _compFiber = _compFiber._currentElement._owner;
    }
    return _compFiber._instance;
  }

  // react 16+
  var GetCompFiber = function GetCompFiber(fiber) {
    // return fiber._debugOwner; // this also works, but is __DEV__ only
    var parentFiber = fiber["return"];
    while (typeof parentFiber.type === 'string') {
      parentFiber = parentFiber["return"];
    }
    return parentFiber;
  };
  var compFiber = GetCompFiber(domFiber);
  for (var _i = 0; _i < traverseUp; _i++) {
    compFiber = GetCompFiber(compFiber);
  }
  return compFiber.stateNode;
};
var TooltipActionContent = function TooltipActionContent(_ref) {
  var _ref$value = _ref.value,
    value = _ref$value === void 0 ? {} : _ref$value,
    _onChange = _ref.onChange;
  return /*#__PURE__*/React.createElement(_box["default"], null, /*#__PURE__*/React.createElement(_box["default"], {
    direction: "row",
    align: "center",
    className: "behavior-item"
  }, /*#__PURE__*/React.createElement(_box["default"], {
    style: {
      width: 70
    }
  }, "\u89E6\u53D1\u65B9\u5F0F"), /*#__PURE__*/React.createElement(_box["default"], {
    className: "behavior-radio"
  }, /*#__PURE__*/React.createElement(_radio["default"].Group, {
    size: "small",
    dataSource: ['click', 'hover'],
    defaultValue: "click",
    shape: "button",
    value: value.triggerType || 'click',
    onChange: function onChange(triggerType) {
      _onChange((0, _extends2["default"])({}, value, {
        triggerType: triggerType
      }));
    }
  }))), /*#__PURE__*/React.createElement(_box["default"], {
    direction: "row",
    align: "center",
    className: "behavior-item"
  }, /*#__PURE__*/React.createElement(_box["default"], {
    style: {
      width: 70
    }
  }, "\u901A\u77E5\u5185\u5BB9"), /*#__PURE__*/React.createElement(_box["default"], {
    direction: "row",
    className: "behavior-radio",
    spacing: 10
  }, /*#__PURE__*/React.createElement(_input["default"].TextArea, {
    size: "small",
    placeholder: "\u8BF7\u8F93\u5165\u5185\u5BB9",
    value: value.content,
    onChange: function onChange(content) {
      _onChange((0, _extends2["default"])({}, value, {
        content: content
      }));
    }
  }))));
};
var tooltipBehaviorAction = exports.tooltipBehaviorAction = {
  name: 'tooltip',
  title: 'Tooltip',
  render: function render(props) {
    return /*#__PURE__*/React.createElement(TooltipActionContent, props);
  },
  toActionValue: function toActionValue(value, options) {
    return {
      type: 'JSExpression',
      value: "function() {\n      const id = '" + options.id + "';\n      const findReact = " + findReact + ";\n      const node = findReact(id);\n      if (!node) return;\n      if (typeof node.enableTooltip === 'function') {\n        node.enableTooltip('" + value.triggerType + "', '" + value.content + "');\n      } else if (typeof node.toggleTip === 'function') {\n        node.toggleTip('" + value.content + "');\n      }\n    }"
    };
  }
};