"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 _popup = _interopRequireDefault(require("../popup"));

var _button = _interopRequireDefault(require("../button"));

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

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

require("./index.css");

var _excluded = ["type", "width", "time", "children", "popupPlacement", "classnames", "content", "onClickClose", "buttonTitle", "visible", "isShowPopup"];

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 _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

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 _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

var preCls = 'titaui-pc__guide-tip';
var GuideTip = /*#__PURE__*/(0, _react.forwardRef)(function (props, ref) {
  var _props$type = props.type,
      type = _props$type === void 0 ? 'default' : _props$type,
      _props$width = props.width,
      width = _props$width === void 0 ? 304 : _props$width,
      _props$time = props.time,
      time = _props$time === void 0 ? 3000 : _props$time,
      children = props.children,
      _props$popupPlacement = props.popupPlacement,
      popupPlacement = _props$popupPlacement === void 0 ? 'top' : _props$popupPlacement,
      _props$classnames = props.classnames,
      classnames = _props$classnames === void 0 ? "".concat(preCls, "__popup") : _props$classnames,
      content = props.content,
      _props$onClickClose = props.onClickClose,
      onClickClose = _props$onClickClose === void 0 ? function () {} : _props$onClickClose,
      _props$buttonTitle = props.buttonTitle,
      buttonTitle = _props$buttonTitle === void 0 ? (0, _getLocale.getLocale)('TopMenu_button_InvitColleagues') : _props$buttonTitle,
      _props$visible = props.visible,
      visible = _props$visible === void 0 ? false : _props$visible,
      _props$isShowPopup = props.isShowPopup,
      isShowPopup = _props$isShowPopup === void 0 ? true : _props$isShowPopup,
      restProps = _objectWithoutProperties(props, _excluded);

  var popupAlign = restProps.popupAlign;
  var isTry = (0, _bsGlobal.getVersion)() == 1;

  if (isTry) {
    return children;
  }

  var popupRef = (0, _react.useRef)({
    open: function open() {},
    close: function close() {}
  });

  var handleCloseGuidePopup = function handleCloseGuidePopup() {
    onClickClose();
    popupRef.current.close();
  };

  var triangleStyle = function triangleStyle() {
    if (popupPlacement === 'left' || popupPlacement === 'right') {
      var _popupAlign$offset;

      var offsetY = (popupAlign === null || popupAlign === void 0 ? void 0 : (_popupAlign$offset = popupAlign.offset) === null || _popupAlign$offset === void 0 ? void 0 : _popupAlign$offset[1]) || 0;
      return {
        marginTop: "".concat(-offsetY - 4, "px")
      };
    }

    return {};
  };

  var renderPopupContent = function renderPopupContent() {
    return content && /*#__PURE__*/_react["default"].createElement("div", {
      className: (0, _classnames["default"])("".concat(preCls, "__popup"), _defineProperty({}, classnames, classnames), _defineProperty({}, "".concat(preCls, "__popup--button"), type === 'button')),
      style: {
        width: "".concat(width, "px")
      }
    }, content, type === 'default' && /*#__PURE__*/_react["default"].createElement("div", {
      className: "".concat(preCls, "__popup-close")
    }, /*#__PURE__*/_react["default"].createElement("i", {
      className: (0, _classnames["default"])('tu-icon-close', "".concat(preCls, "__popup-close-icon")),
      onClick: handleCloseGuidePopup
    })), type === 'button' && /*#__PURE__*/_react["default"].createElement(_button["default"].Rect, {
      text: buttonTitle,
      size: "small",
      onClick: handleCloseGuidePopup
    }), /*#__PURE__*/_react["default"].createElement("div", {
      style: triangleStyle(),
      className: (0, _classnames["default"])("".concat(preCls, "__popup-triangle"), "".concat(preCls, "__popup-triangle--").concat(popupPlacement))
    }));
  };

  (0, _react.useImperativeHandle)(ref, function () {
    return {
      showGuideTip: function showGuideTip() {
        return popupRef.current.open();
      },
      hideGuideTip: function hideGuideTip() {
        return popupRef.current.close();
      }
    };
  });
  (0, _react.useEffect)(function () {
    if (type === 'time') {
      setTimeout(function () {
        popupRef.current.close();
      }, time);
    }
  }, []);
  if (!isShowPopup) return children;
  return /*#__PURE__*/_react["default"].createElement(_popup["default"], _extends({
    ref: popupRef,
    action: ['focus'],
    maskClosable: false,
    popupPlacement: popupPlacement,
    popup: renderPopupContent(),
    popupVisible: visible
  }, restProps), children);
});
var _default = GuideTip;
exports["default"] = _default;