"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

var _react = _interopRequireWildcard(require("react"));

var _titaUi = require("tita-ui");

var _pop = _interopRequireDefault(require("../pop"));

var _style = require("./style");

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

function fillRef(ref, node) {
  if (typeof ref === "function") {
    ref(node);
  } else if (_typeof(ref) === "object" && ref && "current" in ref) {
    ref.current = node;
  }
}

function composeRef() {
  for (var _len = arguments.length, refs = new Array(_len), _key = 0; _key < _len; _key++) {
    refs[_key] = arguments[_key];
  }

  return function (node) {
    refs.forEach(function (ref) {
      fillRef(ref, node);
    });
  };
}

var PopConfirm = /*#__PURE__*/(0, _react.forwardRef)(function (_ref, ref) {
  var _ref$align = _ref.align,
      align = _ref$align === void 0 ? "bottom" : _ref$align,
      arrowSize = _ref.arrowSize,
      autoHide = _ref.autoHide,
      children = _ref.children,
      className = _ref.className,
      container = _ref.container,
      content = _ref.content,
      duration = _ref.duration,
      onCancel = _ref.onCancel,
      onConfirm = _ref.onConfirm,
      showArrow = _ref.showArrow,
      style = _ref.style,
      title = _ref.title,
      trigger = _ref.trigger;

  var child = _react["default"].Children.only(children);

  var _useState = (0, _react.useState)({
    top: "-4px",
    left: "50%"
  }),
      _useState2 = _slicedToArray(_useState, 2),
      position = _useState2[0],
      setPosition = _useState2[1];

  var popRef = (0, _react.useRef)();
  var triggerRef = (0, _react.useRef)();
  (0, _react.useImperativeHandle)(ref, function () {
    return {
      getRef: function getRef() {
        return popRef.current;
      }
    };
  });

  var timer = function timer() {
    setTimeout(function () {
      popRef.current.hide();
    }, duration);
  };

  var mouseLeave = function mouseLeave() {
    if (autoHide) {
      setTimeout(function () {
        timer && timer();
      }, duration);
    }
  };

  var tirggerElement = child;

  if (trigger == "click") {
    tirggerElement = /*#__PURE__*/_react["default"].cloneElement(child, {
      ref: composeRef(triggerRef, child.ref),
      onClick: function onClick(e) {
        e.stopPropagation();
        popRef.current.show(triggerRef.current);
      }
    });
  } else if (trigger == "hover") {
    tirggerElement = /*#__PURE__*/_react["default"].cloneElement(child, {
      ref: composeRef(triggerRef, child.ref),
      onMouseEnter: function onMouseEnter() {
        popRef.current.show(triggerRef.current);
      },
      onMouseLeave: mouseLeave
    });
  }

  (0, _react.useEffect)(function () {
    if (showArrow) {
      setPosition(setPlacement(align));
    }
  }, []);

  var setPlacement = function setPlacement(align) {
    var pos = {
      top: "-".concat(arrowSize / 2, "px"),
      left: "50%"
    };

    switch (align) {
      case "top":
        pos = {
          top: "calc(100% - ".concat(arrowSize / 2, "px)"),
          left: "50%"
        };
        break;

      case "top left":
        pos = {
          top: "calc(100% - ".concat(arrowSize / 2, "px)"),
          left: "15%"
        };
        break;

      case "top right":
        pos = {
          top: "calc(100% - ".concat(arrowSize / 2, "px)"),
          left: "80%"
        };
        break;

      case "bottom":
        pos = {
          top: "-".concat(arrowSize / 2, "px"),
          left: "50%"
        };
        break;

      case "bottom left":
        pos = {
          top: "-".concat(arrowSize / 2, "px"),
          left: "15%"
        };
        break;

      case "bottom right":
        pos = {
          top: "-".concat(arrowSize / 2, "px"),
          left: "80%"
        };
        break;
    }

    return pos;
  };

  var defaultContent = /*#__PURE__*/_react["default"].createElement("div", null, /*#__PURE__*/_react["default"].createElement("p", {
    style: {
      color: "#1F2329"
    }
  }, title), /*#__PURE__*/_react["default"].createElement(_style.ButtonGroup, null, /*#__PURE__*/_react["default"].createElement(_titaUi.Button, {
    onClick: function onClick() {
      onCancel && onCancel();
      popRef.current.hide();
    }
  }, (0, _getLocale.getLocale)("Mod_Cancel")), /*#__PURE__*/_react["default"].createElement(_titaUi.Button, {
    type: "primary",
    onClick: function onClick() {
      onConfirm && onConfirm();
    }
  }, (0, _getLocale.getLocale)("OKR_MyO_Butt_Determine"))));

  return /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, /*#__PURE__*/_react["default"].createElement(_pop["default"], {
    align: align,
    className: "tita-common-pop-no-shadow",
    container: container,
    cover: true,
    ref: popRef
  }, /*#__PURE__*/_react["default"].createElement(_style.Wrapper, {
    arrowSize: arrowSize,
    className: className,
    position: position,
    showArrow: showArrow,
    style: style,
    onMouseEnter: function onMouseEnter() {
      // @ts-ignore
      timer = function timer() {
        console.log("mouse enter");
      };
    },
    onMouseLeave: function onMouseLeave() {
      if (autoHide) {
        // @ts-ignore
        timer = function timer() {
          setTimeout(function () {
            popRef.current.hide();
          }, duration);
        }; // @ts-ignore


        popRef.current.hide();
      }
    }
  }, content || defaultContent)), tirggerElement);
});
PopConfirm.defaultProps = {
  align: "bottom",
  arrowSize: 8,
  autoHide: false,
  showArrow: true,
  trigger: "click"
};
var _default = PopConfirm;
exports["default"] = _default;